/logger

golang for logger

Primary LanguageGoMIT LicenseMIT

logger

Build Status Coverage Status License GoDoc

Installation

go get -u github.com/mushroomsir/logger

Feature

  • Easy to use
  • Display log FileLine
  • Output JSON format
  • Support KV syntactic sugar
  • Improve efficiency
  • Output Err automatically if err not nil
  • Standard log level Syslog
  • Flexible for custom
  • Control output by level

Usage

Easy to use

alog.Info("hello world")
alog.Infof("hello world %v", "format")
// Outout:
[2018-10-13T03:05:28.476Z] INFO {"file":"examples/main.go:16","message1":"hello world"}
[2018-10-13T03:05:28.477Z] INFO {"file":"examples/main.go:18","message":"hello world format"}

KV sugar / FileLine / JSON

alog.Info("key", "val")
// Output:
[2018-04-12T14:46:58.088Z] INFO {"file":"main.go:15","Key":"val"}

Improve efficiency

Return true value and output Err log if err!=nil
err := errors.New("x")
if alog.NotNil(err) {
    return err
}
// Output:
[2018-04-18T00:34:19.946Z] ERR {"error":"x","file":"main.go:13"}
Does not output anything if Err==nil and continue code execution
var err error
if alog.NotNil(err) {
    return err
}
// continue code execution

Standard log level Syslog

alog.Debug()
alog.Debugf()
alog.Info()
alog.Infof()
alog.Warning()
alog.Warningf()
...

Flexible for custom

var blog = pkg.New(os.Stderr, pkg.Options{
	EnableJSON:     true,
	EnableFileLine: true,
    TimeFormat: "2006-01-02T15:04:05.999Z",
    LogFormat: "[%s] %s %s",
})

Control output by level

alog.SetLevel(pkg.InfoLevel)

Licenses

All source code is licensed under the MIT License.