Documentation ¶
Overview ¶
Package zwrap provides a variety of wrappers for the core zap logger.
Example (Sample) ¶
package main import ( "time" "github.com/uber-go/zap" "github.com/uber-go/zap/zwrap" ) func main() { sampledLogger := zwrap.Sample(zap.NewJSON(), time.Second, 1, 100) // Stub the current time in tests. sampledLogger.StubTime() for i := 1; i < 110; i++ { sampledLogger.With(zap.Int("n", i)).Error("Common failure.") } sampledLogger.Error("Unusual failure.") }
Output: {"msg":"Common failure.","level":"error","ts":0,"fields":{"n":1}} {"msg":"Common failure.","level":"error","ts":0,"fields":{"n":101}} {"msg":"Unusual failure.","level":"error","ts":0,"fields":{}}
Example (Standardize) ¶
package main import ( "github.com/uber-go/zap" "github.com/uber-go/zap/zwrap" ) func main() { zapLogger := zap.NewJSON() // Stub the current time in tests. zapLogger.StubTime() // Wrap our structured logger to mimic the standard library's log.Logger. // We also specify that we want all calls to the standard logger's Print // family of methods to log at zap's Warn level. stdLogger, err := zwrap.Standardize(zapLogger, zap.WarnLevel) if err != nil { panic(err.Error()) } // The wrapped logger has the usual Print, Panic, and Fatal families of // methods. stdLogger.Printf("Encountered %d errors.", 0) }
Output: {"msg":"Encountered 0 errors.","level":"warn","ts":0,"fields":{}}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidLevel = errors.New("StandardLogger's print level must be Debug, Info, Warn, or Error")
ErrInvalidLevel indicates that the user chose an invalid Level when constructing a StandardLogger.
Functions ¶
func Sample ¶
Sample returns a sampling logger. The logger maintains a separate bucket for each message (e.g., "foo" in logger.Warn("foo")). In each tick, the sampler will emit the first N logs in each bucket and every Mth log therafter. Sampling loggers are safe for concurrent use.
Per-message counts are shared between parent and child loggers, which allows applications to more easily control global I/O load.
Types ¶
type StandardLogger ¶
type StandardLogger interface { Print(...interface{}) Printf(string, ...interface{}) Println(...interface{}) Panic(...interface{}) Panicf(string, ...interface{}) Panicln(...interface{}) Fatal(...interface{}) Fatalf(string, ...interface{}) Fatalln(...interface{}) }
StandardLogger is the interface exposed by the standard library's log.Logger.
func Standardize ¶
Standardize wraps a Logger to make it compatible with the standard library. It takes the Logger itself, and the level to use for the StandardLogger's Print family of methods. If the specified Level isn't Debug, Info, Warn, or Error, Standardize returns ErrInvalidLevel.