package zap

Import Path
	go.uber.org/zap (on go.dev)

Dependency Relation
	imports 23 packages, and imported by 7 packages

Involved Source Files array.go config.go Package zap provides fast, structured, leveled logging. For applications that log in the hot path, reflection-based serialization and string formatting are prohibitively expensive - they're CPU-intensive and make many small allocations. Put differently, using json.Marshal and fmt.Fprintf to log tons of interface{} makes your application slow. Zap takes a different approach. It includes a reflection-free, zero-allocation JSON encoder, and the base Logger strives to avoid serialization overhead and allocations wherever possible. By building the high-level SugaredLogger on that foundation, zap lets users choose when they need to count every allocation and when they'd prefer a more familiar, loosely typed API. # Choosing a Logger In contexts where performance is nice, but not critical, use the SugaredLogger. It's 4-10x faster than other structured logging packages and supports both structured and printf-style logging. Like log15 and go-kit, the SugaredLogger's structured logging APIs are loosely typed and accept a variadic number of key-value pairs. (For more advanced use cases, they also accept strongly typed fields - see the SugaredLogger.With documentation for details.) sugar := zap.NewExample().Sugar() defer sugar.Sync() sugar.Infow("failed to fetch URL", "url", "http://example.com", "attempt", 3, "backoff", time.Second, ) sugar.Infof("failed to fetch URL: %s", "http://example.com") By default, loggers are unbuffered. However, since zap's low-level APIs allow buffering, calling Sync before letting your process exit is a good habit. In the rare contexts where every microsecond and every allocation matter, use the Logger. It's even faster than the SugaredLogger and allocates far less, but it only supports strongly-typed, structured logging. logger := zap.NewExample() defer logger.Sync() logger.Info("failed to fetch URL", zap.String("url", "http://example.com"), zap.Int("attempt", 3), zap.Duration("backoff", time.Second), ) Choosing between the Logger and SugaredLogger doesn't need to be an application-wide decision: converting between the two is simple and inexpensive. logger := zap.NewExample() defer logger.Sync() sugar := logger.Sugar() plain := sugar.Desugar() # Configuring Zap The simplest way to build a Logger is to use zap's opinionated presets: NewExample, NewProduction, and NewDevelopment. These presets build a logger with a single function call: logger, err := zap.NewProduction() if err != nil { log.Fatalf("can't initialize zap logger: %v", err) } defer logger.Sync() Presets are fine for small projects, but larger projects and organizations naturally require a bit more customization. For most users, zap's Config struct strikes the right balance between flexibility and convenience. See the package-level BasicConfiguration example for sample code. More unusual configurations (splitting output between files, sending logs to a message queue, etc.) are possible, but require direct use of go.uber.org/zap/zapcore. See the package-level AdvancedConfiguration example for sample code. # Extending Zap The zap package itself is a relatively thin wrapper around the interfaces in go.uber.org/zap/zapcore. Extending zap to support a new encoding (e.g., BSON), a new log sink (e.g., Kafka), or something more exotic (perhaps an exception aggregation service, like Sentry or Rollbar) typically requires implementing the zapcore.Encoder, zapcore.WriteSyncer, or zapcore.Core interfaces. See the zapcore documentation for details. Similarly, package authors can use the high-performance Encoder and Core implementations in the zapcore package to build their own loggers. # Frequently Asked Questions An FAQ covering everything from installation errors to design decisions is available at https://github.com/uber-go/zap/blob/master/FAQ.md. encoder.go error.go field.go flag.go global.go http_handler.go level.go logger.go options.go sink.go sugar.go time.go writer.go
Code Examples package main import ( "os" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) func main() { atom := zap.NewAtomicLevel() // To keep the example deterministic, disable timestamps in the output. encoderCfg := zap.NewProductionEncoderConfig() encoderCfg.TimeKey = "" logger := zap.New(zapcore.NewCore( zapcore.NewJSONEncoder(encoderCfg), zapcore.Lock(os.Stdout), atom, )) defer logger.Sync() logger.Info("info logging enabled") atom.SetLevel(zap.ErrorLevel) logger.Info("info logging disabled") } package main import ( "encoding/json" "go.uber.org/zap" ) func main() { // The zap.Config struct includes an AtomicLevel. To use it, keep a // reference to the Config. rawJSON := []byte(`{ "level": "info", "outputPaths": ["stdout"], "errorOutputPaths": ["stderr"], "encoding": "json", "encoderConfig": { "messageKey": "message", "levelKey": "level", "levelEncoder": "lowercase" } }`) var cfg zap.Config if err := json.Unmarshal(rawJSON, &cfg); err != nil { panic(err) } logger := zap.Must(cfg.Build()) defer logger.Sync() logger.Info("info logging enabled") cfg.Level.SetLevel(zap.ErrorLevel) logger.Info("info logging disabled") } package main import ( "go.uber.org/zap" ) func main() { logger := zap.NewExample() defer logger.Sync() logger.Info("login event", zap.Dict("event", zap.Int("id", 123), zap.String("name", "jane"), zap.String("status", "pending"))) } package main import ( "go.uber.org/zap" ) func main() { logger := zap.NewExample() defer logger.Sync() if ce := logger.Check(zap.DebugLevel, "debugging"); ce != nil { // If debug-level log output isn't enabled or if zap's sampling would have // dropped this log entry, we don't allocate the slice that holds these // fields. ce.Write( zap.String("foo", "bar"), zap.String("baz", "quux"), ) } } package main import ( "go.uber.org/zap" ) func main() { logger := zap.NewExample() defer logger.Sync() // By default, Loggers are unnamed. logger.Info("no name") // The first call to Named sets the Logger name. main := logger.Named("main") main.Info("main logger") // Additional calls to Named create a period-separated path. main.Named("subpackage").Info("sub-logger") } package main import ( "go.uber.org/zap" ) func main() { logger := zap.NewExample() defer logger.Sync() logger.With( zap.Namespace("metrics"), zap.Int("counter", 1), ).Info("tracked some metrics") } package main import ( "go.uber.org/zap" ) func main() { logger := zap.NewExample() defer logger.Sync() std := zap.NewStdLog(logger) std.Print("standard logger wrapper") } package main import ( "go.uber.org/zap" "go.uber.org/zap/zapcore" ) type addr struct { IP string Port int } type request struct { URL string Listen addr Remote addr } func (a addr) MarshalLogObject(enc zapcore.ObjectEncoder) error { enc.AddString("ip", a.IP) enc.AddInt("port", a.Port) return nil } func (r *request) MarshalLogObject(enc zapcore.ObjectEncoder) error { enc.AddString("url", r.URL) zap.Inline(r.Listen).AddTo(enc) return enc.AddObject("remote", r.Remote) } func main() { logger := zap.NewExample() defer logger.Sync() req := &request{ URL: "/test", Listen: addr{"127.0.0.1", 8080}, Remote: addr{"127.0.0.1", 31200}, } logger.Info("new request, in nested object", zap.Object("req", req)) logger.Info("new request, inline", zap.Inline(req)) } package main import ( "go.uber.org/zap" "go.uber.org/zap/zapcore" ) type addr struct { IP string Port int } type request struct { URL string Listen addr Remote addr } func (a addr) MarshalLogObject(enc zapcore.ObjectEncoder) error { enc.AddString("ip", a.IP) enc.AddInt("port", a.Port) return nil } func (r *request) MarshalLogObject(enc zapcore.ObjectEncoder) error { enc.AddString("url", r.URL) zap.Inline(r.Listen).AddTo(enc) return enc.AddObject("remote", r.Remote) } func main() { logger := zap.NewExample() defer logger.Sync() // Use the ObjectValues field constructor when you have a list of // objects that do not implement zapcore.ObjectMarshaler directly, // but on their pointer receivers. logger.Debug("starting tunnels", zap.ObjectValues("addrs", []request{ { URL: "/foo", Listen: addr{"127.0.0.1", 8080}, Remote: addr{"123.45.67.89", 4040}, }, { URL: "/bar", Listen: addr{"127.0.0.1", 8080}, Remote: addr{"127.0.0.1", 31200}, }, })) } package main import ( "go.uber.org/zap" "go.uber.org/zap/zapcore" ) type addr struct { IP string Port int } func (a addr) MarshalLogObject(enc zapcore.ObjectEncoder) error { enc.AddString("ip", a.IP) enc.AddInt("port", a.Port) return nil } func main() { logger := zap.NewExample() defer logger.Sync() // Use the Objects field constructor when you have a list of objects, // all of which implement zapcore.ObjectMarshaler. logger.Debug("opening connections", zap.Objects("addrs", []addr{ {IP: "123.45.67.89", Port: 4040}, {IP: "127.0.0.1", Port: 4041}, {IP: "192.168.0.1", Port: 4042}, })) } package main import ( "log" "go.uber.org/zap" ) func main() { logger := zap.NewExample() defer logger.Sync() undo := zap.RedirectStdLog(logger) defer undo() log.Print("redirected standard library") } package main import ( "go.uber.org/zap" ) func main() { logger := zap.NewExample() defer logger.Sync() undo := zap.ReplaceGlobals(logger) defer undo() zap.L().Info("replaced zap's global loggers") } package main import ( "go.uber.org/zap" "go.uber.org/zap/zapcore" ) func main() { // Replacing a Logger's core can alter fundamental behaviors. // For example, it can convert a Logger to a no-op. nop := zap.WrapCore(func(zapcore.Core) zapcore.Core { return zapcore.NewNopCore() }) logger := zap.NewExample() defer logger.Sync() logger.Info("working") logger.WithOptions(nop).Info("no-op") logger.Info("original logger still works") } package main import ( "go.uber.org/zap" "go.uber.org/zap/zapcore" ) func main() { // Wrapping a Logger's core can extend its functionality. As a trivial // example, it can double-write all logs. doubled := zap.WrapCore(func(c zapcore.Core) zapcore.Core { return zapcore.NewTee(c, c) }) logger := zap.NewExample() defer logger.Sync() logger.Info("single") logger.WithOptions(doubled).Info("doubled") } package main import ( "io" "os" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) func main() { // The bundled Config struct only supports the most common configuration // options. More complex needs, like splitting logs between multiple files // or writing to non-file outputs, require use of the zapcore package. // // In this example, imagine we're both sending our logs to Kafka and writing // them to the console. We'd like to encode the console output and the Kafka // topics differently, and we'd also like special treatment for // high-priority logs. // First, define our level-handling logic. highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { return lvl >= zapcore.ErrorLevel }) lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { return lvl < zapcore.ErrorLevel }) // Assume that we have clients for two Kafka topics. The clients implement // zapcore.WriteSyncer and are safe for concurrent use. (If they only // implement io.Writer, we can use zapcore.AddSync to add a no-op Sync // method. If they're not safe for concurrent use, we can add a protecting // mutex with zapcore.Lock.) topicDebugging := zapcore.AddSync(io.Discard) topicErrors := zapcore.AddSync(io.Discard) // High-priority output should also go to standard error, and low-priority // output should also go to standard out. consoleDebugging := zapcore.Lock(os.Stdout) consoleErrors := zapcore.Lock(os.Stderr) // Optimize the Kafka output for machine consumption and the console output // for human operators. kafkaEncoder := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()) consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()) // Join the outputs, encoders, and level-handling functions into // zapcore.Cores, then tee the four cores together. core := zapcore.NewTee( zapcore.NewCore(kafkaEncoder, topicErrors, highPriority), zapcore.NewCore(consoleEncoder, consoleErrors, highPriority), zapcore.NewCore(kafkaEncoder, topicDebugging, lowPriority), zapcore.NewCore(consoleEncoder, consoleDebugging, lowPriority), ) // From a zapcore.Core, it's easy to construct a Logger. logger := zap.New(core) defer logger.Sync() logger.Info("constructed a logger") } package main import ( "encoding/json" "go.uber.org/zap" ) func main() { // For some users, the presets offered by the NewProduction, NewDevelopment, // and NewExample constructors won't be appropriate. For most of those // users, the bundled Config struct offers the right balance of flexibility // and convenience. (For more complex needs, see the AdvancedConfiguration // example.) // // See the documentation for Config and zapcore.EncoderConfig for all the // available options. rawJSON := []byte(`{ "level": "debug", "encoding": "json", "outputPaths": ["stdout", "/tmp/logs"], "errorOutputPaths": ["stderr"], "initialFields": {"foo": "bar"}, "encoderConfig": { "messageKey": "message", "levelKey": "level", "levelEncoder": "lowercase" } }`) var cfg zap.Config if err := json.Unmarshal(rawJSON, &cfg); err != nil { panic(err) } logger := zap.Must(cfg.Build()) defer logger.Sync() logger.Info("logger construction succeeded") } package main import ( "time" "go.uber.org/zap" ) func main() { // Using zap's preset constructors is the simplest way to get a feel for the // package, but they don't allow much customization. logger := zap.NewExample() // or NewProduction, or NewDevelopment defer logger.Sync() const url = "http://example.com" // In most circumstances, use the SugaredLogger. It's 4-10x faster than most // other structured logging packages and has a familiar, loosely-typed API. sugar := logger.Sugar() sugar.Infow("Failed to fetch URL.", // Structured context as loosely typed key-value pairs. "url", url, "attempt", 3, "backoff", time.Second, ) sugar.Infof("Failed to fetch URL: %s", url) // In the unusual situations where every microsecond matters, use the // Logger. It's even faster than the SugaredLogger, but only supports // structured logging. logger.Info("Failed to fetch URL.", // Structured context as strongly typed fields. zap.String("url", url), zap.Int("attempt", 3), zap.Duration("backoff", time.Second), ) }
Package-Level Type Names (total 44, in which 10 are exported)
/* sort exporteds by: | */
An AtomicLevel is an atomically changeable, dynamic logging level. It lets you safely change the log level of a tree of loggers (the root logger and any children created by adding context) at runtime. The AtomicLevel itself is an http.Handler that serves a JSON endpoint to alter its level. AtomicLevels must be created with the NewAtomicLevel constructor to allocate their internal atomic pointer. Enabled implements the zapcore.LevelEnabler interface, which allows the AtomicLevel to be used in place of traditional static levels. Level returns the minimum enabled log level. MarshalText marshals the AtomicLevel to a byte slice. It uses the same text representation as the static zapcore.Levels ("debug", "info", "warn", "error", "dpanic", "panic", and "fatal"). ServeHTTP is a simple JSON endpoint that can report on or change the current logging level. # GET The GET request returns a JSON description of the current logging level like: {"level":"info"} # PUT The PUT request changes the logging level. It is perfectly safe to change the logging level while a program is running. Two content types are supported: Content-Type: application/x-www-form-urlencoded With this content type, the level can be provided through the request body or a query parameter. The log level is URL encoded like: level=debug The request body takes precedence over the query parameter, if both are specified. This content type is the default for a curl PUT request. Following are two example curl requests that both set the logging level to debug. curl -X PUT localhost:8080/log/level?level=debug curl -X PUT localhost:8080/log/level -d level=debug For any other content type, the payload is expected to be JSON encoded and look like: {"level":"info"} An example curl request could look like this: curl -X PUT localhost:8080/log/level -H "Content-Type: application/json" -d '{"level":"debug"}' SetLevel alters the logging level. String returns the string representation of the underlying Level. UnmarshalText unmarshals the text to an AtomicLevel. It uses the same text representations as the static zapcore.Levels ("debug", "info", "warn", "error", "dpanic", "panic", and "fatal"). AtomicLevel : go.uber.org/zap/internal.LeveledEnabler AtomicLevel : go.uber.org/zap/zapcore.LevelEnabler AtomicLevel : encoding.TextMarshaler *AtomicLevel : encoding.TextUnmarshaler AtomicLevel : fmt.Stringer AtomicLevel : net/http.Handler func NewAtomicLevel() AtomicLevel func NewAtomicLevelAt(l zapcore.Level) AtomicLevel func ParseAtomicLevel(text string) (AtomicLevel, error)
Config offers a declarative way to construct a logger. It doesn't do anything that can't be done with New, Options, and the various zapcore.WriteSyncer and zapcore.Core wrappers, but it's a simpler way to toggle common options. Note that Config intentionally supports only the most common options. More unusual logging setups (logging to network connections or message queues, splitting output between multiple files, etc.) are possible, but require direct use of the zapcore package. For sample code, see the package-level BasicConfiguration and AdvancedConfiguration examples. For an example showing runtime log level changes, see the documentation for AtomicLevel. Development puts the logger in development mode, which changes the behavior of DPanicLevel and takes stacktraces more liberally. DisableCaller stops annotating logs with the calling function's file name and line number. By default, all logs are annotated. DisableStacktrace completely disables automatic stacktrace capturing. By default, stacktraces are captured for WarnLevel and above logs in development and ErrorLevel and above in production. EncoderConfig sets options for the chosen encoder. See zapcore.EncoderConfig for details. Encoding sets the logger's encoding. Valid values are "json" and "console", as well as any third-party encodings registered via RegisterEncoder. ErrorOutputPaths is a list of URLs to write internal logger errors to. The default is standard error. Note that this setting only affects internal errors; for sample code that sends error-level logs to a different location from info- and debug-level logs, see the package-level AdvancedConfiguration example. InitialFields is a collection of fields to add to the root logger. Level is the minimum enabled logging level. Note that this is a dynamic level, so calling Config.Level.SetLevel will atomically change the log level of all loggers descended from this config. OutputPaths is a list of URLs or file paths to write logging output to. See Open for details. Sampling sets a sampling policy. A nil SamplingConfig disables sampling. Build constructs a logger from the Config and Options. func NewDevelopmentConfig() Config func NewProductionConfig() Config
Field is an alias for Field. Aliasing this type dramatically improves the navigability of this package's API documentation.
LevelEnablerFunc is a convenient way to implement zapcore.LevelEnabler with an anonymous function. It's particularly useful when splitting log output between different outputs (e.g., standard error and standard out). For sample code, see the package-level AdvancedConfiguration example. Enabled calls the wrapped function. LevelEnablerFunc : go.uber.org/zap/zapcore.LevelEnabler
A Logger provides fast, leveled, structured logging. All methods are safe for concurrent use. The Logger is designed for contexts in which every microsecond and every allocation matters, so its API intentionally favors performance and type safety over brevity. For most applications, the SugaredLogger strikes a better balance between performance and ergonomics. Check returns a CheckedEntry if logging a message at the specified level is enabled. It's a completely optional optimization; in high-performance applications, Check can help avoid allocating a slice to hold fields. Core returns the Logger's underlying zapcore.Core. DPanic logs a message at DPanicLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger. If the logger is in development mode, it then panics (DPanic means "development panic"). This is useful for catching errors that are recoverable, but shouldn't ever happen. Debug logs a message at DebugLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger. Error logs a message at ErrorLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger. Fatal logs a message at FatalLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger. The logger then calls os.Exit(1), even if logging at FatalLevel is disabled. Info logs a message at InfoLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger. Level reports the minimum enabled level for this logger. For NopLoggers, this is [zapcore.InvalidLevel]. Log logs a message at the specified level. The message includes any fields passed at the log site, as well as any fields accumulated on the logger. Any Fields that require evaluation (such as Objects) are evaluated upon invocation of Log. Name returns the Logger's underlying name, or an empty string if the logger is unnamed. Named adds a new path segment to the logger's name. Segments are joined by periods. By default, Loggers are unnamed. Panic logs a message at PanicLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger. The logger then panics, even if logging at PanicLevel is disabled. Sugar wraps the Logger to provide a more ergonomic, but slightly slower, API. Sugaring a Logger is quite inexpensive, so it's reasonable for a single application to use both Loggers and SugaredLoggers, converting between them on the boundaries of performance-sensitive code. Sync calls the underlying Core's Sync method, flushing any buffered log entries. Applications should take care to call Sync before exiting. Warn logs a message at WarnLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger. With creates a child logger and adds structured context to it. Fields added to the child don't affect the parent, and vice versa. Any fields that require evaluation (such as Objects) are evaluated upon invocation of With. WithLazy creates a child logger and adds structured context to it lazily. The fields are evaluated only if the logger is further chained with [With] or is written to with any of the log level methods. Until that occurs, the logger may retain references to objects inside the fields, and logging will reflect the state of an object at the time of logging, not the time of WithLazy(). WithLazy provides a worthwhile performance optimization for contextual loggers when the likelihood of using the child logger is low, such as error paths and rarely taken branches. Similar to [With], fields added to the child don't affect the parent, and vice versa. WithOptions clones the current Logger, applies the supplied Options, and returns the resulting Logger. It's safe to use concurrently. func L() *Logger func Must(logger *Logger, err error) *Logger func New(core zapcore.Core, options ...Option) *Logger func NewDevelopment(options ...Option) (*Logger, error) func NewExample(options ...Option) *Logger func NewNop() *Logger func NewProduction(options ...Option) (*Logger, error) func Config.Build(opts ...Option) (*Logger, error) func (*Logger).Named(s string) *Logger func (*Logger).With(fields ...Field) *Logger func (*Logger).WithLazy(fields ...Field) *Logger func (*Logger).WithOptions(opts ...Option) *Logger func (*SugaredLogger).Desugar() *Logger func Must(logger *Logger, err error) *Logger func NewStdLog(l *Logger) *log.Logger func NewStdLogAt(l *Logger, level zapcore.Level) (*log.Logger, error) func RedirectStdLog(l *Logger) func() func RedirectStdLogAt(l *Logger, level zapcore.Level) (func(), error) func ReplaceGlobals(logger *Logger) func() func github.com/gotd/td/internal/exchange.Exchanger.WithLogger(log *Logger) exchange.Exchanger func github.com/gotd/td/internal/tdsync.NewLogGroup(parent context.Context, log *Logger) *tdsync.LogGroup
Type Parameters: T: any ObjectMarshalerPtr is a constraint that specifies that the given type implements zapcore.ObjectMarshaler on a pointer receiver. ( ObjectMarshalerPtr[T]) MarshalLogObject(zapcore.ObjectEncoder) error go.uber.org/zap/zapcore.ObjectMarshaler[T] (interface) go.uber.org/zap/zapcore.ObjectMarshalerFunc github.com/gotd/td/internal/crypto.AuthKey ObjectMarshalerPtr : go.uber.org/zap/zapcore.ObjectMarshaler[T]
An Option configures a Logger. func AddCaller() Option func AddCallerSkip(skip int) Option func AddStacktrace(lvl zapcore.LevelEnabler) Option func Development() Option func ErrorOutput(w zapcore.WriteSyncer) Option func Fields(fs ...Field) Option func Hooks(hooks ...func(zapcore.Entry) error) Option func IncreaseLevel(lvl zapcore.LevelEnabler) Option func OnFatal(action zapcore.CheckWriteAction) Option func WithCaller(enabled bool) Option func WithClock(clock zapcore.Clock) Option func WithFatalHook(hook zapcore.CheckWriteHook) Option func WrapCore(f func(zapcore.Core) zapcore.Core) Option func New(core zapcore.Core, options ...Option) *Logger func NewDevelopment(options ...Option) (*Logger, error) func NewExample(options ...Option) *Logger func NewProduction(options ...Option) (*Logger, error) func Config.Build(opts ...Option) (*Logger, error) func (*Logger).WithOptions(opts ...Option) *Logger func (*SugaredLogger).WithOptions(opts ...Option) *SugaredLogger
SamplingConfig sets a sampling strategy for the logger. Sampling caps the global CPU and I/O load that logging puts on your process while attempting to preserve a representative subset of your logs. If specified, the Sampler will invoke the Hook after each decision. Values configured here are per-second. See zapcore.NewSamplerWithOptions for details. Hook func(zapcore.Entry, zapcore.SamplingDecision) Initial int Thereafter int
Sink defines the interface to write to and close logger destinations. ( Sink) Close() error ( Sink) Sync() error ( Sink) Write([]byte) (int, error) *os.File Sink : go.uber.org/zap/zapcore.WriteSyncer Sink : internal/bisect.Writer Sink : io.Closer Sink : io.WriteCloser Sink : io.Writer
A SugaredLogger wraps the base Logger functionality in a slower, but less verbose, API. Any Logger can be converted to a SugaredLogger with its Sugar method. Unlike the Logger, the SugaredLogger doesn't insist on structured logging. For each log level, it exposes four methods: - methods named after the log level for log.Print-style logging - methods ending in "w" for loosely-typed structured logging - methods ending in "f" for log.Printf-style logging - methods ending in "ln" for log.Println-style logging For example, the methods for InfoLevel are: Info(...any) Print-style logging Infow(...any) Structured logging (read as "info with") Infof(string, ...any) Printf-style logging Infoln(...any) Println-style logging DPanic logs the provided arguments at [DPanicLevel]. In development, the logger then panics. (See [DPanicLevel] for details.) Spaces are added between arguments when neither is a string. DPanicf formats the message according to the format specifier and logs it at [DPanicLevel]. In development, the logger then panics. (See [DPanicLevel] for details.) DPanicln logs a message at [DPanicLevel]. In development, the logger then panics. (See [DPanicLevel] for details.) Spaces are always added between arguments. DPanicw logs a message with some additional context. In development, the logger then panics. (See DPanicLevel for details.) The variadic key-value pairs are treated as they are in With. Debug logs the provided arguments at [DebugLevel]. Spaces are added between arguments when neither is a string. Debugf formats the message according to the format specifier and logs it at [DebugLevel]. Debugln logs a message at [DebugLevel]. Spaces are always added between arguments. Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are in With. When debug-level logging is disabled, this is much faster than s.With(keysAndValues).Debug(msg) Desugar unwraps a SugaredLogger, exposing the original Logger. Desugaring is quite inexpensive, so it's reasonable for a single application to use both Loggers and SugaredLoggers, converting between them on the boundaries of performance-sensitive code. Error logs the provided arguments at [ErrorLevel]. Spaces are added between arguments when neither is a string. Errorf formats the message according to the format specifier and logs it at [ErrorLevel]. Errorln logs a message at [ErrorLevel]. Spaces are always added between arguments. Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With. Fatal constructs a message with the provided arguments and calls os.Exit. Spaces are added between arguments when neither is a string. Fatalf formats the message according to the format specifier and calls os.Exit. Fatalln logs a message at [FatalLevel] and calls os.Exit. Spaces are always added between arguments. Fatalw logs a message with some additional context, then calls os.Exit. The variadic key-value pairs are treated as they are in With. Info logs the provided arguments at [InfoLevel]. Spaces are added between arguments when neither is a string. Infof formats the message according to the format specifier and logs it at [InfoLevel]. Infoln logs a message at [InfoLevel]. Spaces are always added between arguments. Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With. Level reports the minimum enabled level for this logger. For NopLoggers, this is [zapcore.InvalidLevel]. Named adds a sub-scope to the logger's name. See Logger.Named for details. Panic constructs a message with the provided arguments and panics. Spaces are added between arguments when neither is a string. Panicf formats the message according to the format specifier and panics. Panicln logs a message at [PanicLevel] and panics. Spaces are always added between arguments. Panicw logs a message with some additional context, then panics. The variadic key-value pairs are treated as they are in With. Sync flushes any buffered log entries. Warn logs the provided arguments at [WarnLevel]. Spaces are added between arguments when neither is a string. Warnf formats the message according to the format specifier and logs it at [WarnLevel]. Warnln logs a message at [WarnLevel]. Spaces are always added between arguments. Warnw logs a message with some additional context. The variadic key-value pairs are treated as they are in With. With adds a variadic number of fields to the logging context. It accepts a mix of strongly-typed Field objects and loosely-typed key-value pairs. When processing pairs, the first element of the pair is used as the field key and the second as the field value. For example, sugaredLogger.With( "hello", "world", "failure", errors.New("oh no"), Stack(), "count", 42, "user", User{Name: "alice"}, ) is the equivalent of unsugared.With( String("hello", "world"), String("failure", "oh no"), Stack(), Int("count", 42), Object("user", User{Name: "alice"}), ) Note that the keys in key-value pairs should be strings. In development, passing a non-string key panics. In production, the logger is more forgiving: a separate error is logged, but the key-value pair is skipped and execution continues. Passing an orphaned key triggers similar behavior: panics in development and errors in production. WithOptions clones the current SugaredLogger, applies the supplied Options, and returns the result. It's safe to use concurrently. func S() *SugaredLogger func (*Logger).Sugar() *SugaredLogger func (*SugaredLogger).Named(name string) *SugaredLogger func (*SugaredLogger).With(args ...interface{}) *SugaredLogger func (*SugaredLogger).WithOptions(opts ...Option) *SugaredLogger
Package-Level Functions (total 129, in which 115 are exported)
AddCaller configures the Logger to annotate each message with the filename, line number, and function name of zap's caller. See also WithCaller.
AddCallerSkip increases the number of callers skipped by caller annotation (as enabled by the AddCaller option). When building wrappers around the Logger and SugaredLogger, supplying this Option prevents zap from always reporting the wrapper code as the caller.
AddStacktrace configures the Logger to record a stack trace for all messages at or above a given level.
Any takes a key and an arbitrary value and chooses the best way to represent them as a field, falling back to a reflection-based approach only if necessary. Since byte/uint8 and rune/int32 are aliases, Any can't differentiate between them. To minimize surprises, []byte values are treated as binary blobs, byte values are treated as uint8, and runes are always treated as integers.
Array constructs a field with the given key and ArrayMarshaler. It provides a flexible, but still type-safe and efficient, way to add array-like types to the logging context. The struct's MarshalLogArray method is called lazily.
Binary constructs a field that carries an opaque binary blob. Binary data is serialized in an encoding-appropriate format. For example, zap's JSON encoder base64-encodes binary blobs. To log UTF-8 encoded text, use ByteString.
Bool constructs a field that carries a bool.
Boolp constructs a field that carries a *bool. The returned Field will safely and explicitly represent `nil` when appropriate.
Bools constructs a field that carries a slice of bools.
ByteString constructs a field that carries UTF-8 encoded text as a []byte. To log opaque binary blobs (which aren't necessarily valid UTF-8), use Binary.
ByteStrings constructs a field that carries a slice of []byte, each of which must be UTF-8 encoded text.
CombineWriteSyncers is a utility that combines multiple WriteSyncers into a single, locked WriteSyncer. If no inputs are supplied, it returns a no-op WriteSyncer. It's provided purely as a convenience; the result is no different from using zapcore.NewMultiWriteSyncer and zapcore.Lock individually.
Complex128 constructs a field that carries a complex number. Unlike most numeric fields, this costs an allocation (to convert the complex128 to interface{}).
Complex128p constructs a field that carries a *complex128. The returned Field will safely and explicitly represent `nil` when appropriate.
Complex128s constructs a field that carries a slice of complex numbers.
Complex64 constructs a field that carries a complex number. Unlike most numeric fields, this costs an allocation (to convert the complex64 to interface{}).
Complex64p constructs a field that carries a *complex64. The returned Field will safely and explicitly represent `nil` when appropriate.
Complex64s constructs a field that carries a slice of complex numbers.
Development puts the logger in development mode, which makes DPanic-level logs panic instead of simply logging an error.
Dict constructs a field containing the provided key-value pairs. It acts similar to [Object], but with the fields specified as arguments.
Duration constructs a field with the given key and value. The encoder controls how the duration is serialized.
Durationp constructs a field that carries a *time.Duration. The returned Field will safely and explicitly represent `nil` when appropriate.
Durations constructs a field that carries a slice of time.Durations.
Error is shorthand for the common idiom NamedError("error", err).
ErrorOutput sets the destination for errors generated by the Logger. Note that this option only affects internal errors; for sample code that sends error-level logs to a different location from info- and debug-level logs, see the package-level AdvancedConfiguration example. The supplied WriteSyncer must be safe for concurrent use. The Open and zapcore.Lock functions are the simplest ways to protect files with a mutex.
Errors constructs a field that carries a slice of errors.
Fields adds fields to the Logger.
Float32 constructs a field that carries a float32. The way the floating-point value is represented is encoder-dependent, so marshaling is necessarily lazy.
Float32p constructs a field that carries a *float32. The returned Field will safely and explicitly represent `nil` when appropriate.
Float32s constructs a field that carries a slice of floats.
Float64 constructs a field that carries a float64. The way the floating-point value is represented is encoder-dependent, so marshaling is necessarily lazy.
Float64p constructs a field that carries a *float64. The returned Field will safely and explicitly represent `nil` when appropriate.
Float64s constructs a field that carries a slice of floats.
Hooks registers functions which will be called each time the Logger writes out an Entry. Repeated use of Hooks is additive. Hooks are useful for simple side effects, like capturing metrics for the number of emitted logs. More complex side effects, including anything that requires access to the Entry's structured fields, should be implemented as a zapcore.Core instead. See zapcore.RegisterHooks for details.
IncreaseLevel increase the level of the logger. It has no effect if the passed in level tries to decrease the level of the logger.
Inline constructs a Field that is similar to Object, but it will add the elements of the provided ObjectMarshaler to the current namespace.
Int constructs a field with the given key and value.
Int16 constructs a field with the given key and value.
Int16p constructs a field that carries a *int16. The returned Field will safely and explicitly represent `nil` when appropriate.
Int16s constructs a field that carries a slice of integers.
Int32 constructs a field with the given key and value.
Int32p constructs a field that carries a *int32. The returned Field will safely and explicitly represent `nil` when appropriate.
Int32s constructs a field that carries a slice of integers.
Int64 constructs a field with the given key and value.
Int64p constructs a field that carries a *int64. The returned Field will safely and explicitly represent `nil` when appropriate.
Int64s constructs a field that carries a slice of integers.
Int8 constructs a field with the given key and value.
Int8p constructs a field that carries a *int8. The returned Field will safely and explicitly represent `nil` when appropriate.
Int8s constructs a field that carries a slice of integers.
Intp constructs a field that carries a *int. The returned Field will safely and explicitly represent `nil` when appropriate.
Ints constructs a field that carries a slice of integers.
L returns the global Logger, which can be reconfigured with ReplaceGlobals. It's safe for concurrent use.
LevelFlag uses the standard library's flag.Var to declare a global flag with the specified name, default, and usage guidance. The returned value is a pointer to the value of the flag. If you don't want to use the flag package's global state, you can use any non-nil *Level as a flag.Value with your own *flag.FlagSet.
Must is a helper that wraps a call to a function returning (*Logger, error) and panics if the error is non-nil. It is intended for use in variable initialization such as: var logger = zap.Must(zap.NewProduction())
NamedError constructs a field that lazily stores err.Error() under the provided key. Errors which also implement fmt.Formatter (like those produced by github.com/pkg/errors) will also have their verbose representation stored under key+"Verbose". If passed a nil error, the field is a no-op. For the common case in which the key is simply "error", the Error function is shorter and less repetitive.
Namespace creates a named, isolated scope within the logger's context. All subsequent fields will be added to the new namespace. This helps prevent key collisions when injecting loggers into sub-components or third-party libraries.
New constructs a new Logger from the provided zapcore.Core and Options. If the passed zapcore.Core is nil, it falls back to using a no-op implementation. This is the most flexible way to construct a Logger, but also the most verbose. For typical use cases, the highly-opinionated presets (NewProduction, NewDevelopment, and NewExample) or the Config struct are more convenient. For sample code, see the package-level AdvancedConfiguration example.
NewAtomicLevel creates an AtomicLevel with InfoLevel and above logging enabled.
NewAtomicLevelAt is a convenience function that creates an AtomicLevel and then calls SetLevel with the given level.
NewDevelopment builds a development Logger that writes DebugLevel and above logs to standard error in a human-friendly format. It's a shortcut for NewDevelopmentConfig().Build(...Option).
NewDevelopmentConfig builds a reasonable default development logging configuration. Logging is enabled at DebugLevel and above, and uses a console encoder. Logs are written to standard error. Stacktraces are included on logs of WarnLevel and above. DPanicLevel logs will panic. See [NewDevelopmentEncoderConfig] for information on the default encoder configuration.
NewDevelopmentEncoderConfig returns an opinionated EncoderConfig for development environments. Messages encoded with this configuration will use Zap's console encoder intended to print human-readable output. It will print log messages with the following information: - The log level (e.g. "INFO", "ERROR"). - The time in ISO8601 format (e.g. "2017-01-01T12:00:00Z"). - The message passed to the log statement. - If available, a short path to the file and line number where the log statement was issued. The logger configuration determines whether this field is captured. - If available, a stacktrace from the line where the log statement was issued. The logger configuration determines whether this field is captured. By default, the following formats are used for different types: - Time is formatted in ISO8601 format (e.g. "2017-01-01T12:00:00Z"). - Duration is formatted as a string (e.g. "1.234s"). You may change these by setting the appropriate fields in the returned object. For example, use the following to change the time encoding format: cfg := zap.NewDevelopmentEncoderConfig() cfg.EncodeTime = zapcore.ISO8601TimeEncoder
NewExample builds a Logger that's designed for use in zap's testable examples. It writes DebugLevel and above logs to standard out as JSON, but omits the timestamp and calling function to keep example output short and deterministic.
NewNop returns a no-op Logger. It never writes out logs or internal errors, and it never runs user-defined hooks. Using WithOptions to replace the Core or error output of a no-op Logger can re-enable logging.
NewProduction builds a sensible production Logger that writes InfoLevel and above logs to standard error as JSON. It's a shortcut for NewProductionConfig().Build(...Option).
NewProductionConfig builds a reasonable default production logging configuration. Logging is enabled at InfoLevel and above, and uses a JSON encoder. Logs are written to standard error. Stacktraces are included on logs of ErrorLevel and above. DPanicLevel logs will not panic, but will write a stacktrace. Sampling is enabled at 100:100 by default, meaning that after the first 100 log entries with the same level and message in the same second, it will log every 100th entry with the same level and message in the same second. You may disable this behavior by setting Sampling to nil. See [NewProductionEncoderConfig] for information on the default encoder configuration.
NewProductionEncoderConfig returns an opinionated EncoderConfig for production environments. Messages encoded with this configuration will be JSON-formatted and will have the following keys by default: - "level": The logging level (e.g. "info", "error"). - "ts": The current time in number of seconds since the Unix epoch. - "msg": The message passed to the log statement. - "caller": If available, a short path to the file and line number where the log statement was issued. The logger configuration determines whether this field is captured. - "stacktrace": If available, a stack trace from the line where the log statement was issued. The logger configuration determines whether this field is captured. By default, the following formats are used for different types: - Time is formatted as floating-point number of seconds since the Unix epoch. - Duration is formatted as floating-point number of seconds. You may change these by setting the appropriate fields in the returned object. For example, use the following to change the time encoding format: cfg := zap.NewProductionEncoderConfig() cfg.EncodeTime = zapcore.ISO8601TimeEncoder
NewStdLog returns a *log.Logger which writes to the supplied zap Logger at InfoLevel. To redirect the standard library's package-global logging functions, use RedirectStdLog instead.
NewStdLogAt returns *log.Logger which writes to supplied zap logger at required level.
Object constructs a field with the given key and ObjectMarshaler. It provides a flexible, but still type-safe and efficient, way to add map- or struct-like user-defined types to the logging context. The struct's MarshalLogObject method is called lazily.
Type Parameters: T: zapcore.ObjectMarshaler Objects constructs a field with the given key, holding a list of the provided objects that can be marshaled by Zap. Note that these objects must implement zapcore.ObjectMarshaler directly. That is, if you're trying to marshal a []Request, the MarshalLogObject method must be declared on the Request type, not its pointer (*Request). If it's on the pointer, use ObjectValues. Given an object that implements MarshalLogObject on the value receiver, you can log a slice of those objects with Objects like so: type Author struct{ ... } func (a Author) MarshalLogObject(enc zapcore.ObjectEncoder) error var authors []Author = ... logger.Info("loading article", zap.Objects("authors", authors)) Similarly, given a type that implements MarshalLogObject on its pointer receiver, you can log a slice of pointers to that object with Objects like so: type Request struct{ ... } func (r *Request) MarshalLogObject(enc zapcore.ObjectEncoder) error var requests []*Request = ... logger.Info("sending requests", zap.Objects("requests", requests)) If instead, you have a slice of values of such an object, use the ObjectValues constructor. var requests []Request = ... logger.Info("sending requests", zap.ObjectValues("requests", requests))
Type Parameters: T: any P: ObjectMarshalerPtr[T] ObjectValues constructs a field with the given key, holding a list of the provided objects, where pointers to these objects can be marshaled by Zap. Note that pointers to these objects must implement zapcore.ObjectMarshaler. That is, if you're trying to marshal a []Request, the MarshalLogObject method must be declared on the *Request type, not the value (Request). If it's on the value, use Objects. Given an object that implements MarshalLogObject on the pointer receiver, you can log a slice of those objects with ObjectValues like so: type Request struct{ ... } func (r *Request) MarshalLogObject(enc zapcore.ObjectEncoder) error var requests []Request = ... logger.Info("sending requests", zap.ObjectValues("requests", requests)) If instead, you have a slice of pointers of such an object, use the Objects field constructor. var requests []*Request = ... logger.Info("sending requests", zap.Objects("requests", requests))
OnFatal sets the action to take on fatal logs. Deprecated: Use [WithFatalHook] instead.
Open is a high-level wrapper that takes a variadic number of URLs, opens or creates each of the specified resources, and combines them into a locked WriteSyncer. It also returns any error encountered and a function to close any opened files. Passing no URLs returns a no-op WriteSyncer. Zap handles URLs without a scheme and URLs with the "file" scheme. Third-party code may register factories for other schemes using RegisterSink. URLs with the "file" scheme must use absolute paths on the local filesystem. No user, password, port, fragments, or query parameters are allowed, and the hostname must be empty or "localhost". Since it's common to write logs to the local filesystem, URLs without a scheme (e.g., "/var/log/foo.log") are treated as local file paths. Without a scheme, the special paths "stdout" and "stderr" are interpreted as os.Stdout and os.Stderr. When specified without a scheme, relative file paths also work.
ParseAtomicLevel parses an AtomicLevel based on a lowercase or all-caps ASCII representation of the log level. If the provided ASCII representation is invalid an error is returned. This is particularly useful when dealing with text input to configure log levels.
RedirectStdLog redirects output from the standard library's package-global logger to the supplied logger at InfoLevel. Since zap already handles caller annotations, timestamps, etc., it automatically disables the standard library's annotations and prefixing. It returns a function to restore the original prefix and flags and reset the standard library's output to os.Stderr.
RedirectStdLogAt redirects output from the standard library's package-global logger to the supplied logger at the specified level. Since zap already handles caller annotations, timestamps, etc., it automatically disables the standard library's annotations and prefixing. It returns a function to restore the original prefix and flags and reset the standard library's output to os.Stderr.
Reflect constructs a field with the given key and an arbitrary object. It uses an encoding-appropriate, reflection-based function to lazily serialize nearly any object into the logging context, but it's relatively slow and allocation-heavy. Outside tests, Any is always a better choice. If encoding fails (e.g., trying to serialize a map[int]string to JSON), Reflect includes the error message in the final log output.
RegisterEncoder registers an encoder constructor, which the Config struct can then reference. By default, the "json" and "console" encoders are registered. Attempting to register an encoder whose name is already taken returns an error.
RegisterSink registers a user-supplied factory for all sinks with a particular scheme. All schemes must be ASCII, valid under section 0.1 of RFC 3986 (https://tools.ietf.org/html/rfc3983#section-3.1), and must not already have a factory registered. Zap automatically registers a factory for the "file" scheme.
ReplaceGlobals replaces the global Logger and SugaredLogger, and returns a function to restore the original values. It's safe for concurrent use.
S returns the global SugaredLogger, which can be reconfigured with ReplaceGlobals. It's safe for concurrent use.
Skip constructs a no-op field, which is often useful when handling invalid inputs in other Field constructors.
Stack constructs a field that stores a stacktrace of the current goroutine under provided key. Keep in mind that taking a stacktrace is eager and expensive (relatively speaking); this function both makes an allocation and takes about two microseconds.
StackSkip constructs a field similarly to Stack, but also skips the given number of frames from the top of the stacktrace.
String constructs a field with the given key and value.
Stringer constructs a field with the given key and the output of the value's String method. The Stringer's String method is called lazily.
Type Parameters: T: fmt.Stringer Stringers constructs a field with the given key, holding a list of the output provided by the value's String method Given an object that implements String on the value receiver, you can log a slice of those objects with Objects like so: type Request struct{ ... } func (a Request) String() string var requests []Request = ... logger.Info("sending requests", zap.Stringers("requests", requests)) Note that these objects must implement fmt.Stringer directly. That is, if you're trying to marshal a []Request, the String method must be declared on the Request type, not its pointer (*Request).
Stringp constructs a field that carries a *string. The returned Field will safely and explicitly represent `nil` when appropriate.
Strings constructs a field that carries a slice of strings.
Time constructs a Field with the given key and value. The encoder controls how the time is serialized.
Timep constructs a field that carries a *time.Time. The returned Field will safely and explicitly represent `nil` when appropriate.
Times constructs a field that carries a slice of time.Times.
Uint constructs a field with the given key and value.
Uint16 constructs a field with the given key and value.
Uint16p constructs a field that carries a *uint16. The returned Field will safely and explicitly represent `nil` when appropriate.
Uint16s constructs a field that carries a slice of unsigned integers.
Uint32 constructs a field with the given key and value.
Uint32p constructs a field that carries a *uint32. The returned Field will safely and explicitly represent `nil` when appropriate.
Uint32s constructs a field that carries a slice of unsigned integers.
Uint64 constructs a field with the given key and value.
Uint64p constructs a field that carries a *uint64. The returned Field will safely and explicitly represent `nil` when appropriate.
Uint64s constructs a field that carries a slice of unsigned integers.
Uint8 constructs a field with the given key and value.
Uint8p constructs a field that carries a *uint8. The returned Field will safely and explicitly represent `nil` when appropriate.
Uint8s constructs a field that carries a slice of unsigned integers.
Uintp constructs a field that carries a *uint. The returned Field will safely and explicitly represent `nil` when appropriate.
Uintptr constructs a field with the given key and value.
Uintptrp constructs a field that carries a *uintptr. The returned Field will safely and explicitly represent `nil` when appropriate.
Uintptrs constructs a field that carries a slice of pointer addresses.
Uints constructs a field that carries a slice of unsigned integers.
WithCaller configures the Logger to annotate each message with the filename, line number, and function name of zap's caller, or not, depending on the value of enabled. This is a generalized form of AddCaller.
WithClock specifies the clock used by the logger to determine the current time for logged entries. Defaults to the system clock with time.Now.
WithFatalHook sets a CheckWriteHook to run on fatal logs. Zap will call this hook after writing a log statement with a Fatal level. For example, the following builds a logger that will exit the current goroutine after writing a fatal log message, but it will not exit the program. zap.New(core, zap.WithFatalHook(zapcore.WriteThenGoexit)) It is important that the provided CheckWriteHook stops the control flow at the current statement to meet expectations of callers of the logger. We recommend calling os.Exit or runtime.Goexit inside custom hooks at minimum.
WrapCore wraps or replaces the Logger's underlying zapcore.Core.
Package-Level Constants (total 14, in which 7 are exported)
DebugLevel logs are typically voluminous, and are usually disabled in production.
DPanicLevel logs are particularly important errors. In development the logger panics after writing the message.
ErrorLevel logs are high-priority. If an application is running smoothly, it shouldn't generate any error-level logs.
FatalLevel logs a message, then calls os.Exit(1).
InfoLevel is the default logging priority.
PanicLevel logs a message, then panics.
WarnLevel logs are more important than Info, but don't need individual human review.