Documentation
¶
Overview ¶
Package logger wrapped zap as a basic logging implement.
Index ¶
- Constants
- Variables
- func Close()
- func InitCustomizeRoot(opt *Option) (*zap.Logger, error)
- func InitRoot(opt *Option) error
- func NewLoggerCtx(opts ...CtxOption) *loggerCtx
- func Reset()
- func SetGlobalRootLogger(fpath, level string, options int) error
- func TotalSLoggers() int64
- type CtxOption
- type ExtractTrace
- type Logger
- func (l *Logger) Debug(args ...any)
- func (l *Logger) Debugf(fmt string, args ...any)
- func (l *Logger) Debugw(fmt string, args ...any)
- func (l *Logger) Error(args ...any)
- func (l *Logger) Errorf(fmt string, args ...any)
- func (l *Logger) Errorw(fmt string, args ...any)
- func (l *Logger) Fatal(args ...any)
- func (l *Logger) Fatalf(fmt string, args ...any)
- func (l *Logger) Fatalw(fmt string, args ...any)
- func (l *Logger) Info(args ...any)
- func (l *Logger) Infof(fmt string, args ...any)
- func (l *Logger) Infow(fmt string, args ...any)
- func (l *Logger) Level() zapcore.Level
- func (l *Logger) Name() string
- func (l *Logger) Panic(args ...any)
- func (l *Logger) Panicf(fmt string, args ...any)
- func (l *Logger) Panicw(fmt string, args ...any)
- func (l *Logger) RLDebug(r float64, args ...any)
- func (l *Logger) RLDebugf(r float64, fmt string, args ...any)
- func (l *Logger) RLError(r float64, args ...any)
- func (l *Logger) RLErrorf(r float64, fmt string, args ...any)
- func (l *Logger) RLInfo(r float64, args ...any)
- func (l *Logger) RLInfof(r float64, fmt string, args ...any)
- func (l *Logger) RLWarn(r float64, args ...any)
- func (l *Logger) RLWarnf(r float64, fmt string, args ...any)
- func (l *Logger) SetSugar(sugar *zap.SugaredLogger)
- func (l *Logger) Sugar() *zap.SugaredLogger
- func (l *Logger) Warn(args ...any)
- func (l *Logger) Warnf(fmt string, args ...any)
- func (l *Logger) Warnw(fmt string, args ...any)
- type LoggerCtx
- type Option
- type SLogerOpt
- type Trace
Constants ¶
View Source
const ( // 禁用 JSON 形式输出. OPT_ENC_CONSOLE = 1 //nolint:golint,stylecheck // 显示代码路径时,不显示全路径. OPT_SHORT_CALLER = 2 //nolint:stylecheck,golint // 日志写到 stdout. OPT_STDOUT = 4 //nolint:stylecheck,golint // 日志内容中追加颜色. OPT_COLOR = 8 //nolint:stylecheck,golint // 日志自动切割. OPT_ROTATE = 32 //nolint:stylecheck,golint // 默认日志 flags. OPT_DEFAULT = OPT_ENC_CONSOLE | OPT_SHORT_CALLER | OPT_ROTATE //nolint:stylecheck,golint DEBUG = "debug" INFO = "info" WARN = "warn" ERROR = "error" PANIC = "panic" DPANIC = "dpanic" FATAL = "fatal" )
View Source
const ( NameKeyMod = "mod" NameKeyMsg = "msg" NameKeyLevel = "lev" NameKeyTime = "ts" NameKeyPos = "pos" )
View Source
const (
STDOUT = "stdout" // log output to stdout
)
Variables ¶
View Source
var ( MaxSize = 32 // MB MaxBackups = 5 MaxAge = 30 // day )
View Source
var ( SchemeTCP = "tcp" SchemeUDP = "udp" )
View Source
var ( StdoutColor bool StdoutLevel = DEBUG )
Functions ¶
func InitRoot ¶
InitRoot used to setup global root logger, include
- log level
- log path
- set to disk file(with or without rotate)
- set to some remtoe TCP/UDP server
- a bounch of other OPT_XXXs
func NewLoggerCtx ¶
func NewLoggerCtx(opts ...CtxOption) *loggerCtx
func SetGlobalRootLogger ¶
SetGlobalRootLogger deprecated, use InitRoot() instead.
func TotalSLoggers ¶
func TotalSLoggers() int64
Types ¶
type CtxOption ¶
type CtxOption interface {
// contains filtered or unexported methods
}
func EnableTrace ¶
func EnableTrace() CtxOption
func WithParseTrace ¶
func WithParseTrace(f ExtractTrace) CtxOption
func WithTraceKey ¶
func WithZapCore ¶
func WithZapOpts ¶
type ExtractTrace ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
func DefaultSLogger ¶
func DefaultSloggerSkipN ¶
func (*Logger) SetSugar ¶
func (l *Logger) SetSugar(sugar *zap.SugaredLogger)
func (*Logger) Sugar ¶
func (l *Logger) Sugar() *zap.SugaredLogger
type LoggerCtx ¶
type LoggerCtx interface {
Debugf(template string, args ...interface{})
Infof(template string, args ...interface{})
Warnf(template string, args ...interface{})
Errorf(template string, args ...interface{})
Debug(args ...interface{})
Info(args ...interface{})
Warn(args ...interface{})
Error(args ...interface{})
DebugfCtx(ctx context.Context, template string, args ...interface{})
InfofCtx(ctx context.Context, template string, args ...interface{})
WarnfCtx(ctx context.Context, template string, args ...interface{})
ErrorfCtx(ctx context.Context, template string, args ...interface{})
DebugCtx(ctx context.Context, template string, args ...interface{})
InfoCtx(ctx context.Context, template string, args ...interface{})
WarnCtx(ctx context.Context, template string, args ...interface{})
ErrorCtx(ctx context.Context, template string, args ...interface{})
Named(name string) LoggerCtx
With(fields ...zap.Field) LoggerCtx
}
type SLogerOpt ¶
type SLogerOpt func(*Logger)
func WithRateLimiter ¶
WithRateLimiter set rate limit on current sloger, n limits the max logs can be written per second. The hint will injected to the log message like this:
2025-11-13T11:11:40.168+0800 INFO basic logger/logger_test.go:44 [<your-hint-here>] <your-origin-log-message>
If no hint set, the default seems like this(1 log/sec rate limited):
2025-11-13T11:11:40.168+0800 INFO basic logger/logger_test.go:44 [1.0-rl] <your-origin-log-message>
Click to show internal directories.
Click to hide internal directories.