zap

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package zap provides a zap logger implementation for the logx package. It supports high-performance logging with features like log rotation, buffering, and structured logging.

Package zap provides configuration structures and utilities for the zap logger.

Index

Constants

View Source
const (
	// ZapFormatJSON specifies JSON format for log output.
	ZapFormatJSON = "json"
	// ZapFormatText specifies plain text format for log output.
	ZapFormatText = "text"

	// ZapEncodeLevelLower encodes log levels in lowercase.
	ZapEncodeLevelLower = "lower"
	// ZapEncodeLevelLowerColor encodes log levels in lowercase with color.
	ZapEncodeLevelLowerColor = "lowerColor"
	// ZapEncodeLevelCap encodes log levels in uppercase.
	ZapEncodeLevelCap = "cap"
	// ZapEncodeLevelCapColor encodes log levels in uppercase with color.
	ZapEncodeLevelCapColor = "capColor"
)
View Source
const LoggerKey loggerKey = "zapLogger"

LoggerKey is the context key for storing the logger.

Variables

This section is empty.

Functions

func GetWriter

func GetWriter(c *ZapConfig, level string) (zapcore.WriteSyncer, error)

GetWriter get io.Writer

Types

type BufferedWriteSyncer

type BufferedWriteSyncer struct {
	WriteSyncer   zapcore.WriteSyncer
	Size          int
	FlushInterval int
	// contains filtered or unexported fields
}

BufferedWriteSyncer implements a buffered WriteSyncer

func (*BufferedWriteSyncer) Sync

func (bws *BufferedWriteSyncer) Sync() error

Sync implements the zapcore.WriteSyncer interface

func (*BufferedWriteSyncer) Write

func (bws *BufferedWriteSyncer) Write(p []byte) (n int, err error)

Write implements the zapcore.WriteSyncer interface

type Logger

type Logger struct {
	*zap.SugaredLogger
}

Logger wraps zap.SugaredLogger to implement the logx.Logger interface.

func NewLog

func NewLog(c *ZapConfig) (*Logger, error)

NewLog creates a new zap logger instance with the given configuration.

func (*Logger) NewContext

func (l *Logger) NewContext(ctx context.Context, fields ...any) context.Context

NewContext add fields to the specified context

func (*Logger) WithContext

func (l *Logger) WithContext(ctx context.Context) *Logger

WithContext return a zap instance from the specified context

type ZapConfig

type ZapConfig struct {
	// Level specifies the minimum log level (debug, info, warn, error, fatal).
	Level string `mapstructure:"level" yaml:"level"`
	// Prefix specifies the log message prefix.
	Prefix string `mapstructure:"prefix" yaml:"prefix"`
	// Format specifies the log output format (json or text).
	Format string `mapstructure:"format" yaml:"format"`
	// Director specifies the directory where log files are stored.
	Director string `mapstructure:"director" yaml:"director"`
	// EncodeLevel specifies how to encode log levels.
	EncodeLevel string `mapstructure:"encode-level" yaml:"encode-level"`
	// StacktraceKey specifies the key for stacktrace in log output.
	StacktraceKey string `mapstructure:"stacktrace-key" yaml:"stacktrace-key"`
	// MaxAge specifies the maximum number of days to retain log files.
	MaxAge int `mapstructure:"max-age" yaml:"max-age"`
	// MaxSize specifies the maximum size of a log file in MB.
	MaxSize int `mapstructure:"max-size" yaml:"max-size"`
	// MaxBackups specifies the maximum number of log files to keep.
	MaxBackups int `mapstructure:"max-backups" yaml:"max-backups"`
	// ShowCaller determines whether to show the caller information.
	ShowCaller bool `mapstructure:"show-caller" yaml:"show-caller"`
	// LogInConsole determines whether to output logs to console.
	LogInConsole bool `mapstructure:"log-in-console" yaml:"log-in-console"`
	// LocalTime determines whether to use local time (true) or UTC time (false).
	LocalTime bool `mapstructure:"local-time" yaml:"local-time"`
	// Compress determines whether to compress rotated log files.
	Compress bool `mapstructure:"compress" yaml:"compress"`
	// BufferSize specifies the buffer size for performance optimization.
	BufferSize int `mapstructure:"buffer-size" yaml:"buffer-size"`
	// FlushInterval specifies the interval in seconds to flush the buffer.
	FlushInterval int `mapstructure:"flush-interval" yaml:"flush-interval"`
}

ZapConfig holds the configuration for the zap logger.

func (*ZapConfig) TransportLevel

func (z *ZapConfig) TransportLevel() zapcore.Level

TransportLevel get zapcore.Level

func (*ZapConfig) ZapEncodeLevel

func (z *ZapConfig) ZapEncodeLevel() zapcore.LevelEncoder

ZapEncodeLevel get zapcore.LevelEncoder

Jump to

Keyboard shortcuts

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