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() { zapLogger := zap.New(zap.NewJSONEncoder( zap.NoTime(), // discard timestamps in tests )) sampledLogger := zwrap.Sample(zapLogger, time.Second, 1, 100) for i := 1; i < 110; i++ { sampledLogger.With(zap.Int("n", i)).Error("Common failure.") } sampledLogger.Error("Unusual failure.") }
Output: {"level":"error","msg":"Common failure.","n":1} {"level":"error","msg":"Common failure.","n":101} {"level":"error","msg":"Unusual failure."}
Example (Standardize) ¶
package main import ( "github.com/uber-go/zap" "github.com/uber-go/zap/zwrap" ) func main() { zapLogger := zap.New(zap.NewJSONEncoder( zap.NoTime(), // discard timestamps in tests )) // 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: {"level":"warn","msg":"Encountered 0 errors."}
Index ¶
- Variables
- func Sample(zl zap.Logger, tick time.Duration, first, thereafter int) zap.Logger
- type KeyValueMap
- func (m KeyValueMap) AddBool(k string, v bool)
- func (m KeyValueMap) AddFloat64(k string, v float64)
- func (m KeyValueMap) AddInt(k string, v int)
- func (m KeyValueMap) AddInt64(k string, v int64)
- func (m KeyValueMap) AddMarshaler(k string, v zap.LogMarshaler) error
- func (m KeyValueMap) AddObject(k string, v interface{}) error
- func (m KeyValueMap) AddString(k string, v string)
- func (m KeyValueMap) AddUint(k string, v uint)
- func (m KeyValueMap) AddUint64(k string, v uint64)
- func (m KeyValueMap) AddUintptr(k string, v uintptr)
- func (m KeyValueMap) Nest(k string, f func(zap.KeyValue) error) error
- type StandardLogger
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.
Panic and Fatal logging are NOT sampled, and will always call the underlying logger to panic() or terminate the process. HOWEVER Log-ing at PanicLevel or FatalLevel, if it happens is sampled and will call the underlying logger Log method, which should NOT panic() or terminate.
NOTE: logging at DPanicLevel currently IS sampled, but calls to DPanic and Check(DPanicLevvel) are NOT sampled.
Per-message counts are shared between parent and child loggers, which allows applications to more easily control global I/O load.
Types ¶
type KeyValueMap ¶
type KeyValueMap map[string]interface{}
KeyValueMap implements zap.KeyValue backed by a map.
func (KeyValueMap) AddBool ¶
func (m KeyValueMap) AddBool(k string, v bool)
AddBool adds the value under the specified key to the map.
func (KeyValueMap) AddFloat64 ¶
func (m KeyValueMap) AddFloat64(k string, v float64)
AddFloat64 adds the value under the specified key to the map.
func (KeyValueMap) AddInt ¶
func (m KeyValueMap) AddInt(k string, v int)
AddInt adds the value under the specified key to the map.
func (KeyValueMap) AddInt64 ¶
func (m KeyValueMap) AddInt64(k string, v int64)
AddInt64 adds the value under the specified key to the map.
func (KeyValueMap) AddMarshaler ¶
func (m KeyValueMap) AddMarshaler(k string, v zap.LogMarshaler) error
AddMarshaler adds the value under the specified key to the map.
func (KeyValueMap) AddObject ¶
func (m KeyValueMap) AddObject(k string, v interface{}) error
AddObject adds the value under the specified key to the map.
func (KeyValueMap) AddString ¶
func (m KeyValueMap) AddString(k string, v string)
AddString adds the value under the specified key to the map.
func (KeyValueMap) AddUint ¶
func (m KeyValueMap) AddUint(k string, v uint)
AddUint adds the value under the specified key to the map.
func (KeyValueMap) AddUint64 ¶
func (m KeyValueMap) AddUint64(k string, v uint64)
AddUint64 adds the value under the specified key to the map.
func (KeyValueMap) AddUintptr ¶
func (m KeyValueMap) AddUintptr(k string, v uintptr)
AddUintptr adds the value under the specified key to the map.
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.