zap_logger

package module
v0.0.0-...-4457299 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2022 License: MIT Imports: 16 Imported by: 0

README

zap-logger

Wrapper of uber-go/zap logger for personal project

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDevelopmentEncoderConfig

func NewDevelopmentEncoderConfig() zapcore.EncoderConfig

NewDevelopmentEncoderConfig returns an opinionated EncoderConfig for development environments.

func NewProductionEncoderConfig

func NewProductionEncoderConfig() zapcore.EncoderConfig

NewProductionEncoderConfig returns an opinionated EncoderConfig for production environments.

Types

type Config

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 zap.AtomicLevel
	// Development puts the logger in development mode, which changes the
	// behavior of DPanicLevel and takes stack traces more liberally.
	Development bool
	// EncoderConfig sets options for the chosen encoder. See
	// zapcore.EncoderConfig for details.
	EncoderConfig zapcore.EncoderConfig
	// DisableCaller stops annotating logs with the calling function's file
	// name and line number. By default, all logs are annotated.
	DisableCaller bool
	// Encoding sets the logger's encoding. Valid values are "json" and
	// "console" and "all", as well as any third-party encodings registered via
	// RegisterEncoder.
	Encoding string
	// MaxSize is the maximum size in megabytes of the log file before it gets
	// rotated. It defaults to 100 megabytes.
	MaxSize int
	// MaxBackups is the maximum number of old log files to retain.  The default
	// is to retain all old log files (though MaxAge may still cause them to get
	// deleted.)
	MaxBackups int
	// LocalTime determines if the time used for formatting the timestamps in
	// backup files is the computer's local time.  The default is to use UTC
	// time.
	LocalTime bool

	// Compress determines if the rotated log files should be compressed
	// using gzip. The default is not to perform compression.
	Compress bool

	// Filename is the file to write logs to.  Backup log files will be retained
	// in the same directory.  It uses <processname>-lumberjack.log in
	// os.TempDir() if empty.
	Filename string

	// MaxAge is the maximum number of days to retain old log files based on the
	// timestamp encoded in their filename.  Note that a day is defined as 24
	// hours and may not exactly correspond to calendar days due to daylight
	// savings, leap seconds, etc. The default is not to remove old log files
	// based on age.
	MaxAge int

	Interval time.Duration
}

func NewDevelopmentConfig

func NewDevelopmentConfig() Config

NewDevelopmentConfig is a reasonable development logging configuration. Logging is enabled at DebugLevel and above.

It enables development mode (which makes DPanicLevel logs panic), uses a console encoder, writes to standard error, and disables sampling. Stack traces are automatically included on logs of WarnLevel and above.

func NewProductionConfig

func NewProductionConfig() Config

NewProductionConfig is a reasonable production logging configuration. Logging is enabled at InfoLevel and above.

It uses a JSON encoder, writes to standard error, and enables sampling. Stack traces are automatically included on logs of ErrorLevel and above.

type Discarder

type Discarder struct{ Syncer }

A Discarder sends all writes to io.Discard.

func (*Discarder) Write

func (d *Discarder) Write(b []byte) (int, error)

Write implements io.Writer.

type Logger

type Logger = ZapLogger

Logger logger

func NewNop

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 (*Logger) Check

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

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

Level reports the minimum enabled level for this logger.

For NopLoggers, this is zapcore.InvalidLevel.

func (*Logger) Log

func (log *Logger) Log(lvl zapcore.Level, msg string, fields ...zap.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.

func (*Logger) WithOptions

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 LoggerI

type LoggerI interface {
	Debug(msg string, fields ...zap.Field)
	Info(msg string, fields ...zap.Field)
	Warn(msg string, fields ...zap.Field)
	Error(msg string, fields ...zap.Field)
	Fatal(msg string, fields ...zap.Field)
	Panic(msg string, fields ...zap.Field)
	DPanic(msg string, fields ...zap.Field)
	DebugCtx(ctx context.Context, msg string, fields ...zap.Field)
	InfoCtx(ctx context.Context, msg string, fields ...zap.Field)
	WarnCtx(ctx context.Context, msg string, fields ...zap.Field)
	ErrorCtx(ctx context.Context, msg string, fields ...zap.Field)
	FatalCtx(ctx context.Context, msg string, fields ...zap.Field)
	PanicCtx(ctx context.Context, msg string, fields ...zap.Field)
	DPanicCtx(ctx context.Context, msg string, fields ...zap.Field)
}

type Option

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

An Option configures a Logger.

func AddCaller

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

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 AddContext

func AddContext(contextFunc func(ctx context.Context, log *ZapLogger)) Option

AddContext it is used to decide which of the values in the context be used. After deciding the context value will call the set function, used to read the entire contents of the context value on the key

func AddStacktrace

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

func Development() Option

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

func Fields

func Fields(fs ...zap.Field) Option

Fields adds fields to the Logger.

func WithCaller

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.

type Syncer

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

A Syncer is a spy for the Sync portion of zapcore.WriteSyncer.

func (*Syncer) Called

func (s *Syncer) Called() bool

Called reports whether the Sync method was called.

func (*Syncer) SetError

func (s *Syncer) SetError(err error)

SetError sets the error that the Sync method will return.

func (*Syncer) Sync

func (s *Syncer) Sync() error

Sync records that it was called, then returns the user-supplied error (if any).

type ZapLogger

type ZapLogger struct {
	Ctx *inmemCtx
	// contains filtered or unexported fields
}

A ZapLogger provides fast, leveled, structured logging. All methods are safe for concurrent use.

func New

func New(core zapcore.Core, config Config, opts ...Option) *ZapLogger

New constructs a new ZapLogger from the provided zapcore.Core and Options. If the passed zapcore.Core is nil, it falls back to using a no-op implementation.

func NewLogger

func NewLogger(config Config, opts ...Option) *ZapLogger

func (*ZapLogger) Core

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

func (*ZapLogger) DPanic

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

DPanic logs a message at level DPanic on the ZapLogger.

func (*ZapLogger) DPanicCtx

func (log *ZapLogger) DPanicCtx(ctx context.Context, msg string, fields ...zap.Field)

DPanicCtx with context logs a message at level DPanic on the ZapLogger.

func (*ZapLogger) Debug

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

Debug logs a message at level DebugMode on the ZapLogger.

func (*ZapLogger) DebugCtx

func (log *ZapLogger) DebugCtx(ctx context.Context, msg string, fields ...zap.Field)

DebugCtx with context logs a message at level DebugMode on the ZapLogger.

func (*ZapLogger) Error

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

Error logs a message at level Error on the ZapLogger.

func (*ZapLogger) ErrorCtx

func (log *ZapLogger) ErrorCtx(ctx context.Context, msg string, fields ...zap.Field)

ErrorCtx with context logs a message at level Error on the ZapLogger.

func (*ZapLogger) Fatal

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

Fatal logs a message at level Fatal on the ZapLogger.

func (*ZapLogger) FatalCtx

func (log *ZapLogger) FatalCtx(ctx context.Context, msg string, fields ...zap.Field)

FatalCtx with context logs a message at level Fatal on the ZapLogger.

func (*ZapLogger) Info

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

Info logs a message at level Info on the ZapLogger.

func (*ZapLogger) InfoCtx

func (log *ZapLogger) InfoCtx(ctx context.Context, msg string, fields ...zap.Field)

InfoCtx with context logs a message at level Info on the ZapLogger.

func (*ZapLogger) Named

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

Named name logger

func (*ZapLogger) Panic

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

Panic logs a message at level Panic on the ZapLogger.

func (*ZapLogger) PanicCtx

func (log *ZapLogger) PanicCtx(ctx context.Context, msg string, fields ...zap.Field)

PanicCtx with context logs a message at level Panic on the ZapLogger.

func (*ZapLogger) Sync

func (log *ZapLogger) Sync() error

Sync wrap sync

func (*ZapLogger) Warn

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

Warn logs a message at level Warn on the ZapLogger.

func (*ZapLogger) WarnCtx

func (log *ZapLogger) WarnCtx(ctx context.Context, msg string, fields ...zap.Field)

WarnCtx with context logs a message at level Warn on the ZapLogger.

func (*ZapLogger) With

func (log *ZapLogger) With(fields ...zap.Field) *ZapLogger

func (*ZapLogger) WithField

func (log *ZapLogger) WithField(k string, v interface{}) *ZapLogger

WithField return a log with an extra field.

func (*ZapLogger) WithFields

func (log *ZapLogger) WithFields(fields map[string]interface{}) *ZapLogger

WithFields return a log with extra fields.

Directories

Path Synopsis
Package buffer provides a thin wrapper around a byte slice.
Package buffer provides a thin wrapper around a byte slice.
pkg
diode
Package diode provides a thread-safe, lock-free, non-blocking io.Writer wrapper.
Package diode provides a thread-safe, lock-free, non-blocking io.Writer wrapper.

Jump to

Keyboard shortcuts

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