Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
- Closes apsdehal#11
  • Loading branch information
inaneverb authored and apsdehal committed Feb 21, 2018
1 parent 7a6b3f6 commit 7f202ac
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
56 changes: 53 additions & 3 deletions README.md
@@ -1,15 +1,57 @@

# go-logger

[![Build Status](https://travis-ci.org/apsdehal/go-logger.svg?branch=master)](https://travis-ci.org/apsdehal/go-logger)
[![GoDoc](https://godoc.org/github.com/apsdehal/go-logger?status.svg)](http://godoc.org/github.com/apsdehal/go-logger)

A simple go logger for easy logging and debugging of your programs

# Preview
[![Example Output](examples/example.png)](examples/example.go)

# Formatting
By default all log messages have format that you can see above (on pic).
But you can override the default format and set format that you want.

You can do it for Logger instance (after creating logger) ...
```go
log, _ := logger.New("pkgname", 1)
log.SetFormat(format)
```
... or for package
```go
logger.SetDefaultFormat(format)
```
If you do it for package, all existing loggers will print log messages with format that these used already.
But all newest loggers (which will be created after changing format for package) will use your specified format.

But anyway after this, you can still set format of message for specific Logger instance.

Format of log message must contains verbs that represent some info about current log entry.
Ofc, format can contain not only verbs but also something else (for example text, digits, symbols, etc)

### Format verbs:
You can use the following verbs:
```
%{id} - means number of current log message
%{module} - means module name (that you passed to func New())
%{time} - means current time in format "2006-01-02 15:04:05"
%{time:format} - means current time in format that you want
(supports all formats supported by go package "time")
%{level} - means level name (upper case) of log message ("ERROR", "DEBUG", etc)
%{lvl} - means first 3 letters of level name (upper case) of log message ("ERR", "DEB", etc)
%{file} - means name of file in what you wanna write log
%{filename} - means the same as %{file}
%{line} - means line number of file in what you wanna write log
%{message} - means your log message
```
Non-existent verbs (like ```%{nonex-verb}``` or ```%{}```) will be replaced by an empty string.
Invalid verbs (like ```%{inv-verb```) will be treated as plain text.

# Example

Example [program](examples/example.go) demonstrates how to use the logger.

[![Example Output](examples/example.png)](examples/example.go)

```go
package main
Expand All @@ -20,8 +62,8 @@ import (
)

func main () {
// Get the instance for logger class, "test" is the module name, 1 is used to
// state if we want coloring
// Get the instance for logger class, "test" is the module name, 1 is used to
// state if we want coloring
// Third option is optional and is instance of type io.Writer, defaults to os.Stderr
log, err := logger.New("test", 1, os.Stdout)
if err != nil {
Expand All @@ -48,6 +90,14 @@ func main () {
log.InfoF("This is %s!", "Info")

log.StackAsError("Message before printing stack");

// Show warning with format
log.SetFormat("[%{module}] [%{level}] %{message}")
log.Warning("This is Warning!") // output: "[test] [WARNING] This is Warning!"
// Also you can set your format as default format for all new loggers
logger.SetDefaultFormat("%{message}")
log2, _ := logger.New("pkg", 1, os.Stdout)
log2.Error("This is Error!") // output: "This is Error!"
}
```

Expand Down
12 changes: 0 additions & 12 deletions examples/example.go
Expand Up @@ -27,18 +27,6 @@ func main () {
log.Info("This is Info!")

// Show warning with format message
// Verbs:
// %{id} - number of msg
// %{time} - time with format 2006-01-02 15:04:05
// %{time:format} - time with specified format
// %{module} - module name
// %{file} or %{filename} - filename
// %{line} - line number in file
// %{level} or %{lvl} - log level
// %{lvl} - print only 3 first letters of log level
// for example: DEB, WAR, etc
// %{level} - print full name of log level
// %{message} - log message
log.SetFormat("[%{module}] [%{level}] %{message}")
log.Warning("This is Warning!")
// Also you can set your format as default format for all new loggers
Expand Down

0 comments on commit 7f202ac

Please sign in to comment.