zdpgo_zap

package module
v1.3.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2024 License: Apache-2.0 Imports: 24 Imported by: 3

README

zdpgo_zap

基于zap二次开发,主要用于学习和研究

用法

基本使用
package main

import (
	zap "github.com/zhangdapeng520/zdpgo_zap"
	"time"
)

func main() {
	logger, _ := zap.NewProduction()
	defer logger.Sync()

	url := "http://github.com/zhangdapeng520"
	logger.Info("failed to fetch URL",
		// Structured context as strongly typed Field values.
		zap.String("url", url),
		zap.Int("attempt", 3),
		zap.Duration("backoff", time.Second),
	)
}

实现日志切割功能
package main

import (
	"fmt"
	zap "github.com/zhangdapeng520/zdpgo_zap"
	"net/http"
	"time"
)

func main() {
	logger, err := zap.NewLumberjackLogger(&zap.LumberjackLoggerConfig{
		Level:      "debug",
		FileName:   fmt.Sprintf("./log/%v.log", time.Now().Unix()),
		MaxSize:    1,
		MaxBackups: 5,
		MaxAge:     30,
		IsConsole:  true,
	})
	if err != nil {
		panic(err)
	}
	// 调用内核的Sync方法,刷新所有缓冲的日志条目。
	// 应用程序应该注意在退出之前调用Sync。
	defer logger.Sync()
	simpleHttpGet(logger, "www.sogo.com")
	simpleHttpGet(logger, "http://www.sogo.com")
}

func simpleHttpGet(logger *zap.Logger, url string) {
	sugarLogger := logger.Sugar()
	sugarLogger.Debugf("Trying to hit GET request for %s", url)
	resp, err := http.Get(url)
	if err != nil {
		sugarLogger.Errorf("Error fetching URL %s : Error = %s", url, err)
	} else {
		sugarLogger.Infof("Success! statusCode = %s for URL %s", resp.Status, url)
		resp.Body.Close()
	}
}

版本历史

v0.1.0
  • 基础代码
v0.1.1
  • 整合lumberjack

Documentation

Index

Constants

View Source
const (
	// DebugLevel logs are typically voluminous, and are usually disabled in
	// production.
	DebugLevel = zapcore.DebugLevel
	// InfoLevel is the default logging priority.
	InfoLevel = zapcore.InfoLevel
	// WarnLevel logs are more important than Info, but don't need individual
	// human review.
	WarnLevel = zapcore.WarnLevel
	// ErrorLevel logs are high-priority. If an application is running smoothly,
	// it shouldn't generate any error-level logs.
	ErrorLevel = zapcore.ErrorLevel
	// DPanicLevel logs are particularly important errors. In development the
	// logger panics after writing the message.
	DPanicLevel = zapcore.DPanicLevel
	// PanicLevel logs a message, then panics.
	PanicLevel = zapcore.PanicLevel
	// FatalLevel logs a message, then calls os.Exit(1).
	FatalLevel = zapcore.FatalLevel
)

Variables

This section is empty.

Functions

func CombineWriteSyncers added in v1.3.9

func CombineWriteSyncers(writers ...zapcore.WriteSyncer) zapcore.WriteSyncer

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.

func LevelFlag added in v1.3.9

func LevelFlag(name string, defaultLevel zapcore.Level, usage string) *zapcore.Level

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.

func NewDevelopmentEncoderConfig added in v1.3.9

func NewDevelopmentEncoderConfig() zapcore.EncoderConfig

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

func NewProductionEncoderConfig added in v1.3.9

func NewProductionEncoderConfig() zapcore.EncoderConfig

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

func NewStdLog added in v1.3.9

func NewStdLog(l *Logger) *log.Logger

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.

func NewStdLogAt added in v1.3.9

func NewStdLogAt(l *Logger, level zapcore.Level) (*log.Logger, error)

NewStdLogAt returns *log.Logger which writes to supplied zap logger at required level.

func Open added in v1.3.9

func Open(paths ...string) (zapcore.WriteSyncer, func(), error)

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.

func RedirectStdLog added in v1.3.9

func RedirectStdLog(l *Logger) func()

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.

func RedirectStdLogAt added in v1.3.9

func RedirectStdLogAt(l *Logger, level zapcore.Level) (func(), error)

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.

func RegisterEncoder added in v1.3.9

func RegisterEncoder(name string, constructor func(zapcore.EncoderConfig) (zapcore.Encoder, error)) error

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.

func RegisterSink added in v1.3.9

func RegisterSink(scheme string, factory func(*url.URL) (Sink, error)) 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.

func ReplaceGlobals added in v1.3.9

func ReplaceGlobals(logger *Logger) func()

ReplaceGlobals replaces the global Logger and SugaredLogger, and returns a function to restore the original values. It's safe for concurrent use.

Types

type AtomicLevel added in v1.3.9

type AtomicLevel struct {
	// contains filtered or unexported fields
}

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.

func NewAtomicLevel added in v1.3.9

func NewAtomicLevel() AtomicLevel

NewAtomicLevel creates an AtomicLevel with InfoLevel and above logging enabled.

func NewAtomicLevelAt added in v1.3.9

func NewAtomicLevelAt(l zapcore.Level) AtomicLevel

NewAtomicLevelAt is a convenience function that creates an AtomicLevel and then calls SetLevel with the given level.

func ParseAtomicLevel added in v1.3.9

func ParseAtomicLevel(text string) (AtomicLevel, error)

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.

func (AtomicLevel) Enabled added in v1.3.9

func (lvl AtomicLevel) Enabled(l zapcore.Level) bool

Enabled implements the zapcore.LevelEnabler interface, which allows the AtomicLevel to be used in place of traditional static levels.

func (AtomicLevel) Level added in v1.3.9

func (lvl AtomicLevel) Level() zapcore.Level

Level returns the minimum enabled log level.

func (AtomicLevel) MarshalText added in v1.3.9

func (lvl AtomicLevel) MarshalText() (text []byte, err error)

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").

func (AtomicLevel) ServeHTTP added in v1.3.9

func (lvl AtomicLevel) ServeHTTP(w http.ResponseWriter, r *http.Request)

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"}'

func (AtomicLevel) SetLevel added in v1.3.9

func (lvl AtomicLevel) SetLevel(l zapcore.Level)

SetLevel alters the logging level.

func (AtomicLevel) String added in v1.3.9

func (lvl AtomicLevel) String() string

String returns the string representation of the underlying Level.

func (*AtomicLevel) UnmarshalText added in v1.3.9

func (lvl *AtomicLevel) UnmarshalText(text []byte) error

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").

type Config added in v1.3.9

type Config struct {
	// 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.
	Level AtomicLevel `json:"level" yaml:"level"`
	// Development puts the logger in development mode, which changes the
	// behavior of DPanicLevel and takes stacktraces more liberally.
	Development bool `json:"development" yaml:"development"`
	// DisableCaller stops annotating logs with the calling function's file
	// name and line number. By default, all logs are annotated.
	DisableCaller bool `json:"disableCaller" yaml:"disableCaller"`
	// DisableStacktrace completely disables automatic stacktrace capturing. By
	// default, stacktraces are captured for WarnLevel and above logs in
	// development and ErrorLevel and above in production.
	DisableStacktrace bool `json:"disableStacktrace" yaml:"disableStacktrace"`
	// Sampling sets a sampling policy. A nil SamplingConfig disables sampling.
	Sampling *SamplingConfig `json:"sampling" yaml:"sampling"`
	// Encoding sets the logger's encoding. Valid values are "json" and
	// "console", as well as any third-party encodings registered via
	// RegisterEncoder.
	Encoding string `json:"encoding" yaml:"encoding"`
	// EncoderConfig sets options for the chosen encoder. See
	// zapcore.EncoderConfig for details.
	EncoderConfig zapcore.EncoderConfig `json:"encoderConfig" yaml:"encoderConfig"`
	// OutputPaths is a list of URLs or file paths to write logging output to.
	// See Open for details.
	OutputPaths []string `json:"outputPaths" yaml:"outputPaths"`
	// 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.
	ErrorOutputPaths []string `json:"errorOutputPaths" yaml:"errorOutputPaths"`
	// InitialFields is a collection of fields to add to the root logger.
	InitialFields map[string]interface{} `json:"initialFields" yaml:"initialFields"`
}

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.

func NewDevelopmentConfig added in v1.3.9

func NewDevelopmentConfig() Config

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.

func NewProductionConfig added in v1.3.9

func NewProductionConfig() Config

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.

func (Config) Build added in v1.3.9

func (cfg Config) Build(opts ...Option) (*Logger, error)

Build constructs a logger from the Config and Options.

type Field added in v1.3.9

type Field = zapcore.Field

Field is an alias for Field. Aliasing this type dramatically improves the navigability of this package's API documentation.

func Any added in v1.3.9

func Any(key string, value interface{}) Field

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.

func Array added in v1.3.9

func Array(key string, val zapcore.ArrayMarshaler) Field

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.

func Binary added in v1.3.9

func Binary(key string, val []byte) Field

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.

func Bool added in v1.3.9

func Bool(key string, val bool) Field

Bool constructs a field that carries a bool.

func Boolp added in v1.3.9

func Boolp(key string, val *bool) Field

Boolp constructs a field that carries a *bool. The returned Field will safely and explicitly represent `nil` when appropriate.

func Bools added in v1.3.9

func Bools(key string, bs []bool) Field

Bools constructs a field that carries a slice of bools.

func ByteString added in v1.3.9

func ByteString(key string, val []byte) Field

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.

func ByteStrings added in v1.3.9

func ByteStrings(key string, bss [][]byte) Field

ByteStrings constructs a field that carries a slice of []byte, each of which must be UTF-8 encoded text.

func Complex64 added in v1.3.9

func Complex64(key string, val complex64) Field

Complex64 constructs a field that carries a complex number. Unlike most numeric fields, this costs an allocation (to convert the complex64 to interface{}).

func Complex64p added in v1.3.9

func Complex64p(key string, val *complex64) Field

Complex64p constructs a field that carries a *complex64. The returned Field will safely and explicitly represent `nil` when appropriate.

func Complex64s added in v1.3.9

func Complex64s(key string, nums []complex64) Field

Complex64s constructs a field that carries a slice of complex numbers.

func Complex128 added in v1.3.9

func Complex128(key string, val complex128) Field

Complex128 constructs a field that carries a complex number. Unlike most numeric fields, this costs an allocation (to convert the complex128 to interface{}).

func Complex128p added in v1.3.9

func Complex128p(key string, val *complex128) Field

Complex128p constructs a field that carries a *complex128. The returned Field will safely and explicitly represent `nil` when appropriate.

func Complex128s added in v1.3.9

func Complex128s(key string, nums []complex128) Field

Complex128s constructs a field that carries a slice of complex numbers.

func Dict added in v1.3.9

func Dict(key string, val ...Field) Field

Dict constructs a field containing the provided key-value pairs. It acts similar to Object, but with the fields specified as arguments.

func Duration added in v1.3.9

func Duration(key string, val time.Duration) Field

Duration constructs a field with the given key and value. The encoder controls how the duration is serialized.

func Durationp added in v1.3.9

func Durationp(key string, val *time.Duration) Field

Durationp constructs a field that carries a *time.Duration. The returned Field will safely and explicitly represent `nil` when appropriate.

func Durations added in v1.3.9

func Durations(key string, ds []time.Duration) Field

Durations constructs a field that carries a slice of time.Durations.

func Error

func Error(err error) Field

Error is shorthand for the common idiom NamedError("error", err).

func Errors added in v1.3.9

func Errors(key string, errs []error) Field

Errors constructs a field that carries a slice of errors.

func Float32 added in v1.3.9

func Float32(key string, val float32) Field

Float32 constructs a field that carries a float32. The way the floating-point value is represented is encoder-dependent, so marshaling is necessarily lazy.

func Float32p added in v1.3.9

func Float32p(key string, val *float32) Field

Float32p constructs a field that carries a *float32. The returned Field will safely and explicitly represent `nil` when appropriate.

func Float32s added in v1.3.9

func Float32s(key string, nums []float32) Field

Float32s constructs a field that carries a slice of floats.

func Float64 added in v1.3.9

func Float64(key string, val float64) Field

Float64 constructs a field that carries a float64. The way the floating-point value is represented is encoder-dependent, so marshaling is necessarily lazy.

func Float64p added in v1.3.9

func Float64p(key string, val *float64) Field

Float64p constructs a field that carries a *float64. The returned Field will safely and explicitly represent `nil` when appropriate.

func Float64s added in v1.3.9

func Float64s(key string, nums []float64) Field

Float64s constructs a field that carries a slice of floats.

func Inline added in v1.3.9

func Inline(val zapcore.ObjectMarshaler) Field

Inline constructs a Field that is similar to Object, but it will add the elements of the provided ObjectMarshaler to the current namespace.

func Int added in v1.3.9

func Int(key string, val int) Field

Int constructs a field with the given key and value.

func Int8 added in v1.3.9

func Int8(key string, val int8) Field

Int8 constructs a field with the given key and value.

func Int8p added in v1.3.9

func Int8p(key string, val *int8) Field

Int8p constructs a field that carries a *int8. The returned Field will safely and explicitly represent `nil` when appropriate.

func Int8s added in v1.3.9

func Int8s(key string, nums []int8) Field

Int8s constructs a field that carries a slice of integers.

func Int16 added in v1.3.9

func Int16(key string, val int16) Field

Int16 constructs a field with the given key and value.

func Int16p added in v1.3.9

func Int16p(key string, val *int16) Field

Int16p constructs a field that carries a *int16. The returned Field will safely and explicitly represent `nil` when appropriate.

func Int16s added in v1.3.9

func Int16s(key string, nums []int16) Field

Int16s constructs a field that carries a slice of integers.

func Int32 added in v1.3.9

func Int32(key string, val int32) Field

Int32 constructs a field with the given key and value.

func Int32p added in v1.3.9

func Int32p(key string, val *int32) Field

Int32p constructs a field that carries a *int32. The returned Field will safely and explicitly represent `nil` when appropriate.

func Int32s added in v1.3.9

func Int32s(key string, nums []int32) Field

Int32s constructs a field that carries a slice of integers.

func Int64 added in v1.3.9

func Int64(key string, val int64) Field

Int64 constructs a field with the given key and value.

func Int64p added in v1.3.9

func Int64p(key string, val *int64) Field

Int64p constructs a field that carries a *int64. The returned Field will safely and explicitly represent `nil` when appropriate.

func Int64s added in v1.3.9

func Int64s(key string, nums []int64) Field

Int64s constructs a field that carries a slice of integers.

func Intp added in v1.3.9

func Intp(key string, val *int) Field

Intp constructs a field that carries a *int. The returned Field will safely and explicitly represent `nil` when appropriate.

func Ints added in v1.3.9

func Ints(key string, nums []int) Field

Ints constructs a field that carries a slice of integers.

func NamedError added in v1.3.9

func NamedError(key string, err error) Field

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.

func Namespace added in v1.3.9

func Namespace(key string) Field

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.

func Object added in v1.3.9

func Object(key string, val zapcore.ObjectMarshaler) Field

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.

func ObjectValues added in v1.3.9

func ObjectValues[T any, P ObjectMarshalerPtr[T]](key string, values []T) Field

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))

func Objects added in v1.3.9

func Objects[T zapcore.ObjectMarshaler](key string, values []T) Field

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))

func Reflect added in v1.3.9

func Reflect(key string, val interface{}) Field

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.

func Skip added in v1.3.9

func Skip() Field

Skip constructs a no-op field, which is often useful when handling invalid inputs in other Field constructors.

func Stack added in v1.3.9

func Stack(key string) Field

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.

func StackSkip added in v1.3.9

func StackSkip(key string, skip int) Field

StackSkip constructs a field similarly to Stack, but also skips the given number of frames from the top of the stacktrace.

func String added in v1.3.9

func String(key string, val string) Field

String constructs a field with the given key and value.

func Stringer added in v1.3.9

func Stringer(key string, val fmt.Stringer) Field

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.

func Stringers added in v1.3.9

func Stringers[T fmt.Stringer](key string, values []T) Field

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).

func Stringp added in v1.3.9

func Stringp(key string, val *string) Field

Stringp constructs a field that carries a *string. The returned Field will safely and explicitly represent `nil` when appropriate.

func Strings added in v1.3.9

func Strings(key string, ss []string) Field

Strings constructs a field that carries a slice of strings.

func Time added in v1.3.9

func Time(key string, val time.Time) Field

Time constructs a Field with the given key and value. The encoder controls how the time is serialized.

func Timep added in v1.3.9

func Timep(key string, val *time.Time) Field

Timep constructs a field that carries a *time.Time. The returned Field will safely and explicitly represent `nil` when appropriate.

func Times added in v1.3.9

func Times(key string, ts []time.Time) Field

Times constructs a field that carries a slice of time.Times.

func Uint added in v1.3.9

func Uint(key string, val uint) Field

Uint constructs a field with the given key and value.

func Uint8 added in v1.3.9

func Uint8(key string, val uint8) Field

Uint8 constructs a field with the given key and value.

func Uint8p added in v1.3.9

func Uint8p(key string, val *uint8) Field

Uint8p constructs a field that carries a *uint8. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uint8s added in v1.3.9

func Uint8s(key string, nums []uint8) Field

Uint8s constructs a field that carries a slice of unsigned integers.

func Uint16 added in v1.3.9

func Uint16(key string, val uint16) Field

Uint16 constructs a field with the given key and value.

func Uint16p added in v1.3.9

func Uint16p(key string, val *uint16) Field

Uint16p constructs a field that carries a *uint16. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uint16s added in v1.3.9

func Uint16s(key string, nums []uint16) Field

Uint16s constructs a field that carries a slice of unsigned integers.

func Uint32 added in v1.3.9

func Uint32(key string, val uint32) Field

Uint32 constructs a field with the given key and value.

func Uint32p added in v1.3.9

func Uint32p(key string, val *uint32) Field

Uint32p constructs a field that carries a *uint32. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uint32s added in v1.3.9

func Uint32s(key string, nums []uint32) Field

Uint32s constructs a field that carries a slice of unsigned integers.

func Uint64 added in v1.3.9

func Uint64(key string, val uint64) Field

Uint64 constructs a field with the given key and value.

func Uint64p added in v1.3.9

func Uint64p(key string, val *uint64) Field

Uint64p constructs a field that carries a *uint64. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uint64s added in v1.3.9

func Uint64s(key string, nums []uint64) Field

Uint64s constructs a field that carries a slice of unsigned integers.

func Uintp added in v1.3.9

func Uintp(key string, val *uint) Field

Uintp constructs a field that carries a *uint. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uintptr added in v1.3.9

func Uintptr(key string, val uintptr) Field

Uintptr constructs a field with the given key and value.

func Uintptrp added in v1.3.9

func Uintptrp(key string, val *uintptr) Field

Uintptrp constructs a field that carries a *uintptr. The returned Field will safely and explicitly represent `nil` when appropriate.

func Uintptrs added in v1.3.9

func Uintptrs(key string, us []uintptr) Field

Uintptrs constructs a field that carries a slice of pointer addresses.

func Uints added in v1.3.9

func Uints(key string, nums []uint) Field

Uints constructs a field that carries a slice of unsigned integers.

type LevelEnablerFunc added in v1.3.9

type LevelEnablerFunc func(zapcore.Level) bool

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.

func (LevelEnablerFunc) Enabled added in v1.3.9

func (f LevelEnablerFunc) Enabled(lvl zapcore.Level) bool

Enabled calls the wrapped function.

type Logger added in v1.3.9

type Logger struct {
	// contains filtered or unexported fields
}

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.

func L added in v1.3.9

func L() *Logger

L returns the global Logger, which can be reconfigured with ReplaceGlobals. It's safe for concurrent use.

func Must added in v1.3.9

func Must(logger *Logger, err error) *Logger

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())

func New

func New(core zapcore.Core, options ...Option) *Logger

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.

func NewDevelopment added in v1.3.9

func NewDevelopment(options ...Option) (*Logger, error)

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).

func NewExample added in v1.3.9

func NewExample(options ...Option) *Logger

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.

func NewLumberjackLogger added in v1.3.9

func NewLumberjackLogger(config *LumberjackLoggerConfig) (logger *Logger, err error)

func NewNop added in v1.3.9

func NewNop() *Logger

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.

func NewProduction added in v1.3.9

func NewProduction(options ...Option) (*Logger, error)

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).

func (*Logger) Check added in v1.3.9

func (log *Logger) Check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry

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.

func (*Logger) Core added in v1.3.9

func (log *Logger) Core() zapcore.Core

Core returns the Logger's underlying zapcore.Core.

func (*Logger) DPanic added in v1.3.9

func (log *Logger) DPanic(msg string, fields ...Field)

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.

func (*Logger) Debug added in v1.3.9

func (log *Logger) Debug(msg string, fields ...Field)

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.

func (*Logger) Error added in v1.3.9

func (log *Logger) Error(msg string, fields ...Field)

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.

func (*Logger) Fatal added in v1.3.9

func (log *Logger) Fatal(msg string, fields ...Field)

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.

func (*Logger) Info added in v1.3.9

func (log *Logger) Info(msg string, fields ...Field)

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.

func (*Logger) Level added in v1.3.9

func (log *Logger) Level() zapcore.Level

Level reports the minimum enabled level for this logger.

For NopLoggers, this is zapcore.InvalidLevel.

func (*Logger) Log added in v1.3.9

func (log *Logger) Log(lvl zapcore.Level, msg string, fields ...Field)

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.

func (*Logger) Name added in v1.3.9

func (log *Logger) Name() string

Name returns the Logger's underlying name, or an empty string if the logger is unnamed.

func (*Logger) Named added in v1.3.9

func (log *Logger) Named(s string) *Logger

Named adds a new path segment to the logger's name. Segments are joined by periods. By default, Loggers are unnamed.

func (*Logger) Panic added in v1.3.9

func (log *Logger) Panic(msg string, fields ...Field)

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.

func (*Logger) Sugar added in v1.3.9

func (log *Logger) Sugar() *SugaredLogger

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.

func (*Logger) Sync added in v1.3.9

func (log *Logger) Sync() error

Sync calls the underlying Core's Sync method, flushing any buffered log entries. Applications should take care to call Sync before exiting.

func (*Logger) Warn added in v1.3.9

func (log *Logger) Warn(msg string, fields ...Field)

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.

func (*Logger) With added in v1.3.9

func (log *Logger) With(fields ...Field) *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.

func (*Logger) WithLazy added in v1.3.9

func (log *Logger) WithLazy(fields ...Field) *Logger

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.

func (*Logger) WithOptions added in v1.3.9

func (log *Logger) WithOptions(opts ...Option) *Logger

WithOptions clones the current Logger, applies the supplied Options, and returns the resulting Logger. It's safe to use concurrently.

type LumberjackLoggerConfig added in v1.3.9

type LumberjackLoggerConfig struct {
	Level      string `json:"level"`       // Level 最低日志等级,DEBUG<INFO<WARN<ERROR<FATAL 例如:info-->收集info等级以上的日志
	FileName   string `json:"file_name"`   // FileName 日志文件位置
	MaxSize    int    `json:"max_size"`    // MaxSize 进行切割之前,日志文件的最大大小(MB为单位),默认为100MB
	MaxAge     int    `json:"max_age"`     // MaxAge 是根据文件名中编码的时间戳保留旧日志文件的最大天数。
	MaxBackups int    `json:"max_backups"` // MaxBackups 是要保留的旧日志文件的最大数量。默认是保留所有旧的日志文件(尽管 MaxAge 可能仍会导致它们被删除。)
	IsConsole  bool   `json:"is_console"`  // 是否写入到控制台
}

type ObjectMarshalerPtr added in v1.3.9

type ObjectMarshalerPtr[T any] interface {
	*T
	zapcore.ObjectMarshaler
}

ObjectMarshalerPtr is a constraint that specifies that the given type implements zapcore.ObjectMarshaler on a pointer receiver.

type Option added in v1.3.9

type Option interface {
	// contains filtered or unexported methods
}

An Option configures a Logger.

func AddCaller added in v1.3.9

func AddCaller() Option

AddCaller configures the Logger to annotate each message with the filename, line number, and function name of zap's caller. See also WithCaller.

func AddCallerSkip added in v1.3.9

func AddCallerSkip(skip int) Option

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.

func AddStacktrace added in v1.3.9

func AddStacktrace(lvl zapcore.LevelEnabler) Option

AddStacktrace configures the Logger to record a stack trace for all messages at or above a given level.

func Development added in v1.3.9

func Development() Option

Development puts the logger in development mode, which makes DPanic-level logs panic instead of simply logging an error.

func ErrorOutput added in v1.3.9

func ErrorOutput(w zapcore.WriteSyncer) Option

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.

func Fields added in v1.3.9

func Fields(fs ...Field) Option

Fields adds fields to the Logger.

func Hooks added in v1.3.9

func Hooks(hooks ...func(zapcore.Entry) error) Option

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.

func IncreaseLevel added in v1.3.9

func IncreaseLevel(lvl zapcore.LevelEnabler) Option

IncreaseLevel increase the level of the logger. It has no effect if the passed in level tries to decrease the level of the logger.

func OnFatal deprecated added in v1.3.9

func OnFatal(action zapcore.CheckWriteAction) Option

OnFatal sets the action to take on fatal logs.

Deprecated: Use WithFatalHook instead.

func WithCaller added in v1.3.9

func WithCaller(enabled bool) Option

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.

func WithClock added in v1.3.9

func WithClock(clock zapcore.Clock) Option

WithClock specifies the clock used by the logger to determine the current time for logged entries. Defaults to the system clock with time.Now.

func WithFatalHook added in v1.3.9

func WithFatalHook(hook zapcore.CheckWriteHook) Option

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.

func WithPanicHook added in v1.3.9

func WithPanicHook(hook zapcore.CheckWriteHook) Option

WithPanicHook sets a CheckWriteHook to run on Panic/DPanic logs. Zap will call this hook after writing a log statement with a Panic/DPanic level.

For example, the following builds a logger that will exit the current goroutine after writing a Panic/DPanic log message, but it will not start a panic.

zap.New(core, zap.WithPanicHook(zapcore.WriteThenGoexit))

This is useful for testing Panic/DPanic log output.

func WrapCore added in v1.3.9

func WrapCore(f func(zapcore.Core) zapcore.Core) Option

WrapCore wraps or replaces the Logger's underlying zapcore.Core.

type SamplingConfig added in v1.3.9

type SamplingConfig struct {
	Initial    int                                           `json:"initial" yaml:"initial"`
	Thereafter int                                           `json:"thereafter" yaml:"thereafter"`
	Hook       func(zapcore.Entry, zapcore.SamplingDecision) `json:"-" yaml:"-"`
}

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.

type Sink added in v1.3.9

type Sink interface {
	zapcore.WriteSyncer
	io.Closer
}

Sink defines the interface to write to and close logger destinations.

type SugaredLogger added in v1.3.9

type SugaredLogger struct {
	// contains filtered or unexported fields
}

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

func S added in v1.3.9

func S() *SugaredLogger

S returns the global SugaredLogger, which can be reconfigured with ReplaceGlobals. It's safe for concurrent use.

func (*SugaredLogger) DPanic added in v1.3.9

func (s *SugaredLogger) DPanic(args ...interface{})

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.

func (*SugaredLogger) DPanicf added in v1.3.9

func (s *SugaredLogger) DPanicf(template string, args ...interface{})

DPanicf formats the message according to the format specifier and logs it at DPanicLevel. In development, the logger then panics. (See DPanicLevel for details.)

func (*SugaredLogger) DPanicln added in v1.3.9

func (s *SugaredLogger) DPanicln(args ...interface{})

DPanicln logs a message at DPanicLevel. In development, the logger then panics. (See DPanicLevel for details.) Spaces are always added between arguments.

func (*SugaredLogger) DPanicw added in v1.3.9

func (s *SugaredLogger) DPanicw(msg string, keysAndValues ...interface{})

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.

func (*SugaredLogger) Debug added in v1.3.9

func (s *SugaredLogger) Debug(args ...interface{})

Debug logs the provided arguments at DebugLevel. Spaces are added between arguments when neither is a string.

func (*SugaredLogger) Debugf added in v1.3.9

func (s *SugaredLogger) Debugf(template string, args ...interface{})

Debugf formats the message according to the format specifier and logs it at DebugLevel.

func (*SugaredLogger) Debugln added in v1.3.9

func (s *SugaredLogger) Debugln(args ...interface{})

Debugln logs a message at DebugLevel. Spaces are always added between arguments.

func (*SugaredLogger) Debugw added in v1.3.9

func (s *SugaredLogger) Debugw(msg string, keysAndValues ...interface{})

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)

func (*SugaredLogger) Desugar added in v1.3.9

func (s *SugaredLogger) Desugar() *Logger

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.

func (*SugaredLogger) Error added in v1.3.9

func (s *SugaredLogger) Error(args ...interface{})

Error logs the provided arguments at ErrorLevel. Spaces are added between arguments when neither is a string.

func (*SugaredLogger) Errorf added in v1.3.9

func (s *SugaredLogger) Errorf(template string, args ...interface{})

Errorf formats the message according to the format specifier and logs it at ErrorLevel.

func (*SugaredLogger) Errorln added in v1.3.9

func (s *SugaredLogger) Errorln(args ...interface{})

Errorln logs a message at ErrorLevel. Spaces are always added between arguments.

func (*SugaredLogger) Errorw added in v1.3.9

func (s *SugaredLogger) Errorw(msg string, keysAndValues ...interface{})

Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func (*SugaredLogger) Fatal added in v1.3.9

func (s *SugaredLogger) Fatal(args ...interface{})

Fatal constructs a message with the provided arguments and calls os.Exit. Spaces are added between arguments when neither is a string.

func (*SugaredLogger) Fatalf added in v1.3.9

func (s *SugaredLogger) Fatalf(template string, args ...interface{})

Fatalf formats the message according to the format specifier and calls os.Exit.

func (*SugaredLogger) Fatalln added in v1.3.9

func (s *SugaredLogger) Fatalln(args ...interface{})

Fatalln logs a message at FatalLevel and calls os.Exit. Spaces are always added between arguments.

func (*SugaredLogger) Fatalw added in v1.3.9

func (s *SugaredLogger) Fatalw(msg string, keysAndValues ...interface{})

Fatalw logs a message with some additional context, then calls os.Exit. The variadic key-value pairs are treated as they are in With.

func (*SugaredLogger) Info added in v1.3.9

func (s *SugaredLogger) Info(args ...interface{})

Info logs the provided arguments at InfoLevel. Spaces are added between arguments when neither is a string.

func (*SugaredLogger) Infof added in v1.3.9

func (s *SugaredLogger) Infof(template string, args ...interface{})

Infof formats the message according to the format specifier and logs it at InfoLevel.

func (*SugaredLogger) Infoln added in v1.3.9

func (s *SugaredLogger) Infoln(args ...interface{})

Infoln logs a message at InfoLevel. Spaces are always added between arguments.

func (*SugaredLogger) Infow added in v1.3.9

func (s *SugaredLogger) Infow(msg string, keysAndValues ...interface{})

Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func (*SugaredLogger) Level added in v1.3.9

func (s *SugaredLogger) Level() zapcore.Level

Level reports the minimum enabled level for this logger.

For NopLoggers, this is zapcore.InvalidLevel.

func (*SugaredLogger) Log added in v1.3.9

func (s *SugaredLogger) Log(lvl zapcore.Level, args ...interface{})

Log logs the provided arguments at provided level. Spaces are added between arguments when neither is a string.

func (*SugaredLogger) Logf added in v1.3.9

func (s *SugaredLogger) Logf(lvl zapcore.Level, template string, args ...interface{})

Logf formats the message according to the format specifier and logs it at provided level.

func (*SugaredLogger) Logln added in v1.3.9

func (s *SugaredLogger) Logln(lvl zapcore.Level, args ...interface{})

Logln logs a message at provided level. Spaces are always added between arguments.

func (*SugaredLogger) Logw added in v1.3.9

func (s *SugaredLogger) Logw(lvl zapcore.Level, msg string, keysAndValues ...interface{})

Logw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func (*SugaredLogger) Named added in v1.3.9

func (s *SugaredLogger) Named(name string) *SugaredLogger

Named adds a sub-scope to the logger's name. See Logger.Named for details.

func (*SugaredLogger) Panic added in v1.3.9

func (s *SugaredLogger) Panic(args ...interface{})

Panic constructs a message with the provided arguments and panics. Spaces are added between arguments when neither is a string.

func (*SugaredLogger) Panicf added in v1.3.9

func (s *SugaredLogger) Panicf(template string, args ...interface{})

Panicf formats the message according to the format specifier and panics.

func (*SugaredLogger) Panicln added in v1.3.9

func (s *SugaredLogger) Panicln(args ...interface{})

Panicln logs a message at PanicLevel and panics. Spaces are always added between arguments.

func (*SugaredLogger) Panicw added in v1.3.9

func (s *SugaredLogger) Panicw(msg string, keysAndValues ...interface{})

Panicw logs a message with some additional context, then panics. The variadic key-value pairs are treated as they are in With.

func (*SugaredLogger) Sync added in v1.3.9

func (s *SugaredLogger) Sync() error

Sync flushes any buffered log entries.

func (*SugaredLogger) Warn added in v1.3.9

func (s *SugaredLogger) Warn(args ...interface{})

Warn logs the provided arguments at WarnLevel. Spaces are added between arguments when neither is a string.

func (*SugaredLogger) Warnf added in v1.3.9

func (s *SugaredLogger) Warnf(template string, args ...interface{})

Warnf formats the message according to the format specifier and logs it at WarnLevel.

func (*SugaredLogger) Warnln added in v1.3.9

func (s *SugaredLogger) Warnln(args ...interface{})

Warnln logs a message at WarnLevel. Spaces are always added between arguments.

func (*SugaredLogger) Warnw added in v1.3.9

func (s *SugaredLogger) Warnw(msg string, keysAndValues ...interface{})

Warnw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func (*SugaredLogger) With added in v1.3.9

func (s *SugaredLogger) With(args ...interface{}) *SugaredLogger

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.

func (*SugaredLogger) WithLazy added in v1.3.9

func (s *SugaredLogger) WithLazy(args ...interface{}) *SugaredLogger

WithLazy adds a variadic number of fields to the logging context 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().

Similar to [With], fields added to the child don't affect the parent, and vice versa. Also, the keys in key-value pairs should be strings. In development, passing a non-string key panics, while in production it logs an error and skips the pair. Passing an orphaned key has the same behavior.

func (*SugaredLogger) WithOptions added in v1.3.9

func (s *SugaredLogger) WithOptions(opts ...Option) *SugaredLogger

WithOptions clones the current SugaredLogger, applies the supplied Options, and returns the result. It's safe to use concurrently.

Directories

Path Synopsis
Package buffer provides a thin wrapper around a byte slice.
Package buffer provides a thin wrapper around a byte slice.
examples
c01_hello command
c02_production command
c03_lumberjack command
c03_lumberjack2 command
Package internal and its subpackages hold types and functionality that are not part of Zap's public API.
Package internal and its subpackages hold types and functionality that are not part of Zap's public API.
bufferpool
Package bufferpool houses zap's shared internal buffer pool.
Package bufferpool houses zap's shared internal buffer pool.
color
Package color adds coloring functionality for TTY output.
Package color adds coloring functionality for TTY output.
exit
Package exit provides stubs so that unit tests can exercise code that calls os.Exit(1).
Package exit provides stubs so that unit tests can exercise code that calls os.Exit(1).
pool
Package pool provides internal pool utilities.
Package pool provides internal pool utilities.
readme command
readme generates Zap's README from a template.
readme generates Zap's README from a template.
stacktrace
Package stacktrace provides support for gathering stack traces efficiently.
Package stacktrace provides support for gathering stack traces efficiently.
Package lumberjack provides a rolling logger.
Package lumberjack provides a rolling logger.
Package multierr allows combining one or more errors together.
Package multierr allows combining one or more errors together.
Package zapcore defines and implements the low-level interfaces upon which zap is built.
Package zapcore defines and implements the low-level interfaces upon which zap is built.
Package zapio provides tools for interacting with IO streams through Zap.
Package zapio provides tools for interacting with IO streams through Zap.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL