// Copyright (c) 2016 Uber Technologies, Inc.1//2// Permission is hereby granted, free of charge, to any person obtaining a copy3// of this software and associated documentation files (the "Software"), to deal4// in the Software without restriction, including without limitation the rights5// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell6// copies of the Software, and to permit persons to whom the Software is7// furnished to do so, subject to the following conditions:8//9// The above copyright notice and this permission notice shall be included in10// all copies or substantial portions of the Software.11//12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR13// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,14// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE15// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER16// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,17// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN18// THE SOFTWARE.1920package zap2122import (23"fmt"24"io"25"os"26"strings"2728"go.uber.org/zap/internal/bufferpool"29"go.uber.org/zap/internal/stacktrace"30"go.uber.org/zap/zapcore"31)3233// A Logger provides fast, leveled, structured logging. All methods are safe34// for concurrent use.35//36// The Logger is designed for contexts in which every microsecond and every37// allocation matters, so its API intentionally favors performance and type38// safety over brevity. For most applications, the SugaredLogger strikes a39// better balance between performance and ergonomics.40type Logger struct {41core zapcore.Core4243development bool44addCaller bool45onPanic zapcore.CheckWriteHook // default is WriteThenPanic46onFatal zapcore.CheckWriteHook // default is WriteThenFatal4748name string49errorOutput zapcore.WriteSyncer5051addStack zapcore.LevelEnabler5253callerSkip int5455clock zapcore.Clock56}5758// New constructs a new Logger from the provided zapcore.Core and Options. If59// the passed zapcore.Core is nil, it falls back to using a no-op60// implementation.61//62// This is the most flexible way to construct a Logger, but also the most63// verbose. For typical use cases, the highly-opinionated presets64// (NewProduction, NewDevelopment, and NewExample) or the Config struct are65// more convenient.66//67// For sample code, see the package-level AdvancedConfiguration example.68func New(core zapcore.Core, options ...Option) *Logger {69if core == nil {70return NewNop()71}72log := &Logger{73core: core,74errorOutput: zapcore.Lock(os.Stderr),75addStack: zapcore.FatalLevel + 1,76clock: zapcore.DefaultClock,77}78return log.WithOptions(options...)79}8081// NewNop returns a no-op Logger. It never writes out logs or internal errors,82// and it never runs user-defined hooks.83//84// Using WithOptions to replace the Core or error output of a no-op Logger can85// re-enable logging.86func NewNop() *Logger {87return &Logger{88core: zapcore.NewNopCore(),89errorOutput: zapcore.AddSync(io.Discard),90addStack: zapcore.FatalLevel + 1,91clock: zapcore.DefaultClock,92}93}9495// NewProduction builds a sensible production Logger that writes InfoLevel and96// above logs to standard error as JSON.97//98// It's a shortcut for NewProductionConfig().Build(...Option).99func NewProduction(options ...Option) (*Logger, error) {100return NewProductionConfig().Build(options...)101}102103// NewDevelopment builds a development Logger that writes DebugLevel and above104// logs to standard error in a human-friendly format.105//106// It's a shortcut for NewDevelopmentConfig().Build(...Option).107func NewDevelopment(options ...Option) (*Logger, error) {108return NewDevelopmentConfig().Build(options...)109}110111// Must is a helper that wraps a call to a function returning (*Logger, error)112// and panics if the error is non-nil. It is intended for use in variable113// initialization such as:114//115// var logger = zap.Must(zap.NewProduction())116func Must(logger *Logger, err error) *Logger {117if err != nil {118panic(err)119}120121return logger122}123124// NewExample builds a Logger that's designed for use in zap's testable125// examples. It writes DebugLevel and above logs to standard out as JSON, but126// omits the timestamp and calling function to keep example output127// short and deterministic.128func NewExample(options ...Option) *Logger {129encoderCfg := zapcore.EncoderConfig{130MessageKey: "msg",131LevelKey: "level",132NameKey: "logger",133EncodeLevel: zapcore.LowercaseLevelEncoder,134EncodeTime: zapcore.ISO8601TimeEncoder,135EncodeDuration: zapcore.StringDurationEncoder,136}137core := zapcore.NewCore(zapcore.NewJSONEncoder(encoderCfg), os.Stdout, DebugLevel)138return New(core).WithOptions(options...)139}140141// Sugar wraps the Logger to provide a more ergonomic, but slightly slower,142// API. Sugaring a Logger is quite inexpensive, so it's reasonable for a143// single application to use both Loggers and SugaredLoggers, converting144// between them on the boundaries of performance-sensitive code.145func (log *Logger) Sugar() *SugaredLogger {146core := log.clone()147core.callerSkip += 2148return &SugaredLogger{core}149}150151// Named adds a new path segment to the logger's name. Segments are joined by152// periods. By default, Loggers are unnamed.153func (log *Logger) Named(s string) *Logger {154if s == "" {155return log156}157l := log.clone()158if log.name == "" {159l.name = s160} else {161l.name = strings.Join([]string{l.name, s}, ".")162}163return l164}165166// WithOptions clones the current Logger, applies the supplied Options, and167// returns the resulting Logger. It's safe to use concurrently.168func (log *Logger) WithOptions(opts ...Option) *Logger {169c := log.clone()170for _, opt := range opts {171opt.apply(c)172}173return c174}175176// With creates a child logger and adds structured context to it. Fields added177// to the child don't affect the parent, and vice versa. Any fields that178// require evaluation (such as Objects) are evaluated upon invocation of With.179func (log *Logger) With(fields ...Field) *Logger {180if len(fields) == 0 {181return log182}183l := log.clone()184l.core = l.core.With(fields)185return l186}187188// WithLazy creates a child logger and adds structured context to it lazily.189//190// The fields are evaluated only if the logger is further chained with [With]191// or is written to with any of the log level methods.192// Until that occurs, the logger may retain references to objects inside the fields,193// and logging will reflect the state of an object at the time of logging,194// not the time of WithLazy().195//196// WithLazy provides a worthwhile performance optimization for contextual loggers197// when the likelihood of using the child logger is low,198// such as error paths and rarely taken branches.199//200// Similar to [With], fields added to the child don't affect the parent, and vice versa.201func (log *Logger) WithLazy(fields ...Field) *Logger {202if len(fields) == 0 {203return log204}205return log.WithOptions(WrapCore(func(core zapcore.Core) zapcore.Core {206return zapcore.NewLazyWith(core, fields)207}))208}209210// Level reports the minimum enabled level for this logger.211//212// For NopLoggers, this is [zapcore.InvalidLevel].213func (log *Logger) Level() zapcore.Level {214return zapcore.LevelOf(log.core)215}216217// Check returns a CheckedEntry if logging a message at the specified level218// is enabled. It's a completely optional optimization; in high-performance219// applications, Check can help avoid allocating a slice to hold fields.220func (log *Logger) Check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry {221return log.check(lvl, msg)222}223224// Log logs a message at the specified level. The message includes any fields225// passed at the log site, as well as any fields accumulated on the logger.226// Any Fields that require evaluation (such as Objects) are evaluated upon227// invocation of Log.228func (log *Logger) Log(lvl zapcore.Level, msg string, fields ...Field) {229if ce := log.check(lvl, msg); ce != nil {230ce.Write(fields...)231}232}233234// Debug logs a message at DebugLevel. The message includes any fields passed235// at the log site, as well as any fields accumulated on the logger.236func (log *Logger) Debug(msg string, fields ...Field) {237if ce := log.check(DebugLevel, msg); ce != nil {238ce.Write(fields...)239}240}241242// Info logs a message at InfoLevel. The message includes any fields passed243// at the log site, as well as any fields accumulated on the logger.244func (log *Logger) Info(msg string, fields ...Field) {245if ce := log.check(InfoLevel, msg); ce != nil {246ce.Write(fields...)247}248}249250// Warn logs a message at WarnLevel. The message includes any fields passed251// at the log site, as well as any fields accumulated on the logger.252func (log *Logger) Warn(msg string, fields ...Field) {253if ce := log.check(WarnLevel, msg); ce != nil {254ce.Write(fields...)255}256}257258// Error logs a message at ErrorLevel. The message includes any fields passed259// at the log site, as well as any fields accumulated on the logger.260func (log *Logger) Error(msg string, fields ...Field) {261if ce := log.check(ErrorLevel, msg); ce != nil {262ce.Write(fields...)263}264}265266// DPanic logs a message at DPanicLevel. The message includes any fields267// passed at the log site, as well as any fields accumulated on the logger.268//269// If the logger is in development mode, it then panics (DPanic means270// "development panic"). This is useful for catching errors that are271// recoverable, but shouldn't ever happen.272func (log *Logger) DPanic(msg string, fields ...Field) {273if ce := log.check(DPanicLevel, msg); ce != nil {274ce.Write(fields...)275}276}277278// Panic logs a message at PanicLevel. The message includes any fields passed279// at the log site, as well as any fields accumulated on the logger.280//281// The logger then panics, even if logging at PanicLevel is disabled.282func (log *Logger) Panic(msg string, fields ...Field) {283if ce := log.check(PanicLevel, msg); ce != nil {284ce.Write(fields...)285}286}287288// Fatal logs a message at FatalLevel. The message includes any fields passed289// at the log site, as well as any fields accumulated on the logger.290//291// The logger then calls os.Exit(1), even if logging at FatalLevel is292// disabled.293func (log *Logger) Fatal(msg string, fields ...Field) {294if ce := log.check(FatalLevel, msg); ce != nil {295ce.Write(fields...)296}297}298299// Sync calls the underlying Core's Sync method, flushing any buffered log300// entries. Applications should take care to call Sync before exiting.301func (log *Logger) Sync() error {302return log.core.Sync()303}304305// Core returns the Logger's underlying zapcore.Core.306func (log *Logger) Core() zapcore.Core {307return log.core308}309310// Name returns the Logger's underlying name,311// or an empty string if the logger is unnamed.312func (log *Logger) Name() string {313return log.name314}315316func (log *Logger) clone() *Logger {317clone := *log318return &clone319}320321func (log *Logger) check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry {322// Logger.check must always be called directly by a method in the323// Logger interface (e.g., Check, Info, Fatal).324// This skips Logger.check and the Info/Fatal/Check/etc. method that325// called it.326const callerSkipOffset = 2327328// Check the level first to reduce the cost of disabled log calls.329// Since Panic and higher may exit, we skip the optimization for those levels.330if lvl < zapcore.DPanicLevel && !log.core.Enabled(lvl) {331return nil332}333334// Create basic checked entry thru the core; this will be non-nil if the335// log message will actually be written somewhere.336ent := zapcore.Entry{337LoggerName: log.name,338Time: log.clock.Now(),339Level: lvl,340Message: msg,341}342ce := log.core.Check(ent, nil)343willWrite := ce != nil344345// Set up any required terminal behavior.346switch ent.Level {347case zapcore.PanicLevel:348ce = ce.After(ent, terminalHookOverride(zapcore.WriteThenPanic, log.onPanic))349case zapcore.FatalLevel:350ce = ce.After(ent, terminalHookOverride(zapcore.WriteThenFatal, log.onFatal))351case zapcore.DPanicLevel:352if log.development {353ce = ce.After(ent, terminalHookOverride(zapcore.WriteThenPanic, log.onPanic))354}355}356357// Only do further annotation if we're going to write this message; checked358// entries that exist only for terminal behavior don't benefit from359// annotation.360if !willWrite {361return ce362}363364// Thread the error output through to the CheckedEntry.365ce.ErrorOutput = log.errorOutput366367addStack := log.addStack.Enabled(ce.Level)368if !log.addCaller && !addStack {369return ce370}371372// Adding the caller or stack trace requires capturing the callers of373// this function. We'll share information between these two.374stackDepth := stacktrace.First375if addStack {376stackDepth = stacktrace.Full377}378stack := stacktrace.Capture(log.callerSkip+callerSkipOffset, stackDepth)379defer stack.Free()380381if stack.Count() == 0 {382if log.addCaller {383fmt.Fprintf(log.errorOutput, "%v Logger.check error: failed to get caller\n", ent.Time.UTC())384_ = log.errorOutput.Sync()385}386return ce387}388389frame, more := stack.Next()390391if log.addCaller {392ce.Caller = zapcore.EntryCaller{393Defined: frame.PC != 0,394PC: frame.PC,395File: frame.File,396Line: frame.Line,397Function: frame.Function,398}399}400401if addStack {402buffer := bufferpool.Get()403defer buffer.Free()404405stackfmt := stacktrace.NewFormatter(buffer)406407// We've already extracted the first frame, so format that408// separately and defer to stackfmt for the rest.409stackfmt.FormatFrame(frame)410if more {411stackfmt.FormatStack(stack)412}413ce.Stack = buffer.String()414}415416return ce417}418419func terminalHookOverride(defaultHook, override zapcore.CheckWriteHook) zapcore.CheckWriteHook {420// A nil or WriteThenNoop hook will lead to continued execution after421// a Panic or Fatal log entry, which is unexpected. For example,422//423// f, err := os.Open(..)424// if err != nil {425// log.Fatal("cannot open", zap.Error(err))426// }427// fmt.Println(f.Name())428//429// The f.Name() will panic if we continue execution after the log.Fatal.430if override == nil || override == zapcore.WriteThenNoop {431return defaultHook432}433return override434}435436437