github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/log/README.md (about) 1 ![obligatory xkcd](http://imgs.xkcd.com/comics/standards.png) 2 3 # log15 [![godoc reference](https://godoc.org/github.com/inconshreveable/log15?status.png)](https://godoc.org/github.com/inconshreveable/log15) [![Build Status](https://travis-ci.org/inconshreveable/log15.svg?branch=master)](https://travis-ci.org/inconshreveable/log15) 4 5 Package log15 provides an opinionated, simple toolkit for best-practice logging in Go (golang) that is both human and machine readable. It is modeled after the Go standard library's [`io`](http://golang.org/pkg/io/) and [`net/http`](http://golang.org/pkg/net/http/) packages and is an alternative to the standard library's [`log`](http://golang.org/pkg/log/) package. 6 7 ## Features 8 - A simple, easy-to-understand API 9 - Promotes structured logging by encouraging use of key/value pairs 10 - Side loggers which inherit and add their own private context 11 - Lazy evaluation of expensive operations 12 - Simple Handler interface allowing for construction of flexible, custom logging configurations with a tiny API. 13 - Color terminal support 14 - Built-in support for logging to files, streams, syslog, and the network 15 - Support for forking records to multiple handlers, buffering records for output, failing over from failed handler writes, + more 16 17 ## Versioning 18 The API of the master branch of log15 should always be considered unstable. If you want to rely on a stable API, 19 you must vendor the library. 20 21 ## Importing 22 23 ```go 24 import log "github.com/inconshreveable/log15" 25 ``` 26 27 ## Examples 28 29 ```go 30 srvlog := log.New("module", "app/server") 31 32 srvlog.Warn("abnormal conn rate", "rate", curRate, "low", lowRate, "high", highRate) 33 34 connlog := srvlog.New("raddr", c.RemoteAddr()) 35 connlog.Info("connection open") 36 37 connlog.Debug("ping remote", "latency", log.Lazy{pingRemote}) 38 39 srvlog.SetHandler(log.MultiHandler( 40 log.StreamHandler(os.Stderr, log.LogfmtFormat()), 41 log.LvlFilterHandler( 42 log.LvlError, 43 log.Must.FileHandler("errors.json", log.JSONFormat())))) 44 ``` 45 46 Will result in output that looks like this: 47 48 ``` 49 WARN[06-17|21:58:10] abnormal conn rate module=app/server rate=0.500 low=0.100 high=0.800 50 INFO[06-17|21:58:10] connection open module=app/server raddr=10.0.0.1 51 ``` 52 53 ## Breaking API Changes 54 The following commits broke API stability. This reference is intended to help you understand the consequences of updating to a newer version 55 of log15. 56 57 - 57a084d014d4150152b19e4e531399a7145d1540 - Added a `Get()` method to the `Logger` interface to retrieve the current handler 58 - 93404652ee366648fa622b64d1e2b67d75a3094a - `Record` field `Call` changed to `stack.Call` with switch to `github.com/go-stack/stack` 59 - a5e7613673c73281f58e15a87d2cf0cf111e8152 - Restored `syslog.Priority` argument to the `SyslogXxx` handler constructors 60 61 ## FAQ 62 63 ### The varargs style is brittle and error prone! Can I have type safety please? 64 Yes. Use `log.Ctx`: 65 66 ```go 67 srvlog := log.New(log.Ctx{"module": "app/server"}) 68 srvlog.Warn("abnormal conn rate", log.Ctx{"rate": curRate, "low": lowRate, "high": highRate}) 69 ``` 70 71 ## License 72 Apache