log

package
v2.6.1 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2025 License: Apache-2.0 Imports: 23 Imported by: 10

Documentation

Index

Constants

View Source
const (
	FieldNameModule    = "module"
	FieldNameComponent = "component"
)

Variables

View Source
var CtxLogKey = ctxLogKeyType{}
View Source
var (

	// Get retrieves a buffer from the pool, creating one if necessary.
	Get = _pool.Get
)

Functions

func Debug

func Debug(msg string, fields ...zap.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. Deprecated: Use Ctx(ctx).Debug instead.

func DefaultTimeEncoder

func DefaultTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)

DefaultTimeEncoder serializes time.Time to a human-readable formatted string

func Error

func Error(msg string, fields ...zap.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. Deprecated: Use Ctx(ctx).Error instead.

func Fatal

func Fatal(msg string, fields ...zap.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. Deprecated: Use Ctx(ctx).Fatal instead.

func FieldComponent

func FieldComponent(component string) zap.Field

FieldComponent returns a zap field with the component name.

func FieldMessage added in v2.6.0

func FieldMessage(msg zapcore.ObjectMarshaler) zap.Field

FieldMessage returns a zap field with the message object.

func FieldModule

func FieldModule(module string) zap.Field

FieldModule returns a zap field with the module name.

func GetLevel

func GetLevel() zapcore.Level

GetLevel gets the logging level.

func Info

func Info(msg string, fields ...zap.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. Deprecated: Use Ctx(ctx).Info instead.

func L

func L() *zap.Logger

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

func Level

func Level() zap.AtomicLevel

func NewIntentContext

func NewIntentContext(name string, intent string) (context.Context, trace.Span)

NewIntentContext creates a new context with intent information and returns it along with a span.

func NewLazyWith

func NewLazyWith(core zapcore.Core, fields []zapcore.Field) zapcore.Core

func NewTextCore

NewTextCore creates a Core that writes logs to a WriteSyncer.

func NewTextEncoder

func NewTextEncoder(encoderConfig *zapcore.EncoderConfig, spaced bool, disableErrorVerbose bool) zapcore.Encoder

func NewTextEncoderByConfig

func NewTextEncoderByConfig(cfg *Config) zapcore.Encoder

NewTextEncoderByConfig creates a fast, low-allocation Text encoder with config. The encoder appropriately escapes all field keys and values.

func Panic

func Panic(msg string, fields ...zap.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. Deprecated: Use Ctx(ctx).Panic instead.

func R

R returns utils.ReconfigurableRateLimiter.

func RatedDebug

func RatedDebug(cost float64, msg string, fields ...zap.Field) bool

RatedDebug print logs at debug level it limit log print to avoid too many logs return true if log successfully Deprecated: Use Ctx(ctx).RatedDebug instead.

func RatedInfo

func RatedInfo(cost float64, msg string, fields ...zap.Field) bool

RatedInfo print logs at info level it limit log print to avoid too many logs return true if log successfully Deprecated: Use Ctx(ctx).RatedInfo instead.

func RatedWarn

func RatedWarn(cost float64, msg string, fields ...zap.Field) bool

RatedWarn print logs at warn level it limit log print to avoid too many logs return true if log successfully Deprecated: Use Ctx(ctx).RatedWarn instead.

func ReplaceGlobals

func ReplaceGlobals(logger *zap.Logger, props *ZapProperties)

ReplaceGlobals replaces the global Logger and SugaredLogger. It's safe for concurrent use.

func S

func S() *zap.SugaredLogger

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

func SetLevel

func SetLevel(l zapcore.Level)

SetLevel alters the logging level.

func ShortCallerEncoder

func ShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder)

ShortCallerEncoder serializes a caller in file:line format.

func Sync

func Sync() error

Sync flushes any buffered log entries.

func Warn

func Warn(msg string, fields ...zap.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. Deprecated: Use Ctx(ctx).Warn instead.

func WithDebugLevel

func WithDebugLevel(ctx context.Context) context.Context

WithDebugLevel returns context with a debug level enabled logger. Notes that it will overwrite previous attached logger within context

func WithErrorLevel

func WithErrorLevel(ctx context.Context) context.Context

WithErrorLevel returns context with a error level enabled logger. Notes that it will overwrite previous attached logger within context

func WithFatalLevel

func WithFatalLevel(ctx context.Context) context.Context

WithFatalLevel returns context with a fatal level enabled logger. Notes that it will overwrite previous attached logger within context

func WithFields

func WithFields(ctx context.Context, fields ...zap.Field) context.Context

WithFields returns a context with fields attached

func WithInfoLevel

func WithInfoLevel(ctx context.Context) context.Context

WithInfoLevel returns context with a info level enabled logger. Notes that it will overwrite previous attached logger within context

func WithModule

func WithModule(ctx context.Context, module string) context.Context

WithModule adds given module field to the logger in ctx

func WithReqID

func WithReqID(ctx context.Context, reqID int64) context.Context

WithReqID adds given reqID field to the logger in ctx

func WithTraceID

func WithTraceID(ctx context.Context, traceID string) context.Context

WithTraceID returns a context with trace_id attached

func WithWarnLevel

func WithWarnLevel(ctx context.Context) context.Context

WithWarnLevel returns context with a warning level enabled logger. Notes that it will overwrite previous attached logger within context

Types

type Binder

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

Binder is a embedding type to help access local logger.

func (*Binder) Logger

func (w *Binder) Logger() *MLogger

Logger returns the logger of Binder.

func (*Binder) SetLogger

func (w *Binder) SetLogger(logger *MLogger)

SetLogger sets logger to Binder.

type Config

type Config struct {
	// Log level.
	Level string `toml:"level" json:"level"`
	// grpc log level
	GrpcLevel string `toml:"grpc-level" json:"grpc-level"`
	// Log format. one of json, text, or console.
	Format string `toml:"format" json:"format"`
	// Disable automatic timestamps in output.
	DisableTimestamp bool `toml:"disable-timestamp" json:"disable-timestamp"`
	// Stdout enable or not.
	Stdout bool `toml:"stdout" json:"stdout"`
	// File log config.
	File FileLogConfig `toml:"file" json:"file"`
	// Development puts the logger in development mode, which changes the
	// behavior of DPanicLevel and takes stacktraces more liberally.
	Development bool `toml:"development" json:"development"`
	// DisableCaller stops annotating logs with the calling function's file
	// name and line number. By default, all logs are annotated.
	DisableCaller bool `toml:"disable-caller" json:"disable-caller"`
	// 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 `toml:"disable-stacktrace" json:"disable-stacktrace"`
	// DisableErrorVerbose stops annotating logs with the full verbose error
	// message.
	DisableErrorVerbose bool `toml:"disable-error-verbose" json:"disable-error-verbose"`
	// 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.
	//
	// Values configured here are per-second. See zapcore.NewSampler for details.
	Sampling *zap.SamplingConfig `toml:"sampling" json:"sampling"`
}

Config serializes log related config in toml/json.

type FileLogConfig

type FileLogConfig struct {
	// Log rootpath
	RootPath string `toml:"rootpath" json:"rootpath"`
	// Log filename, leave empty to disable file log.
	Filename string `toml:"filename" json:"filename"`
	// Max size for a single file, in MB.
	MaxSize int `toml:"max-size" json:"max-size"`
	// Max log keep days, default is never deleting.
	MaxDays int `toml:"max-days" json:"max-days"`
	// Maximum number of old log files to retain.
	MaxBackups int `toml:"max-backups" json:"max-backups"`
}

FileLogConfig serializes file log related config in toml/json.

type LoggerBinder

type LoggerBinder interface {
	SetLogger(logger *MLogger)
}

LoggerBinder is an interface to help set logger.

type MLogger

type MLogger struct {
	*zap.Logger
	// contains filtered or unexported fields
}

MLogger is a wrapper type of zap.Logger.

func Ctx

func Ctx(ctx context.Context) *MLogger

Ctx returns a logger which will log contextual messages attached in ctx

func With

func With(fields ...zap.Field) *MLogger

With creates a child logger and adds structured context to it. Fields added to the child don't affect the parent, and vice versa. Deprecated: Use Ctx(ctx).With instead.

func (*MLogger) RatedDebug

func (l *MLogger) RatedDebug(cost float64, msg string, fields ...zap.Field) bool

RatedDebug calls log.Debug with RateLimiter.

func (*MLogger) RatedInfo

func (l *MLogger) RatedInfo(cost float64, msg string, fields ...zap.Field) bool

RatedInfo calls log.Info with RateLimiter.

func (*MLogger) RatedWarn

func (l *MLogger) RatedWarn(cost float64, msg string, fields ...zap.Field) bool

RatedWarn calls log.Warn with RateLimiter.

func (*MLogger) With

func (l *MLogger) With(fields ...zap.Field) *MLogger

With encapsulates zap.Logger With method to return MLogger instance.

func (*MLogger) WithRateGroup

func (l *MLogger) WithRateGroup(groupName string, creditPerSecond, maxBalance float64) *MLogger

WithRateGroup uses named RateLimiter for this logger.

type WithLogger

type WithLogger interface {
	Logger() *MLogger
}

WithLogger is an interface to help access local logger.

type ZapProperties

type ZapProperties struct {
	Core   zapcore.Core
	Syncer zapcore.WriteSyncer
	Level  zap.AtomicLevel
}

ZapProperties records some information about zap.

func InitLogger

func InitLogger(cfg *Config, opts ...zap.Option) (*zap.Logger, *ZapProperties, error)

InitLogger initializes a zap logger.

func InitLoggerWithWriteSyncer

func InitLoggerWithWriteSyncer(cfg *Config, output zapcore.WriteSyncer, opts ...zap.Option) (*zap.Logger, *ZapProperties, error)

InitLoggerWithWriteSyncer initializes a zap logger with specified write syncer.

func InitTestLogger

func InitTestLogger(t zaptest.TestingT, cfg *Config, opts ...zap.Option) (*zap.Logger, *ZapProperties, error)

InitTestLogger initializes a logger for unit tests

Jump to

Keyboard shortcuts

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