Documentation ¶
Index ¶
- Constants
- Variables
- func AppendToIncomingContext(ctx context.Context, fields ...zap.Field)
- func DPanic(args ...any)
- func DPanicf(template string, args ...any)
- func Debug(args ...any)
- func Debugf(template string, args ...any)
- func DefaultTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)
- func Error(args ...any)
- func Errorf(template string, args ...any)
- func Fatal(args ...any)
- func Fatalf(template string, args ...any)
- func Info(args ...any)
- func Infof(template string, args ...any)
- func NewIncomingContext(ctx context.Context, carrier *FieldCarrier, fields ...zap.Field) context.Context
- func NewTextEncoder(cfg *Config) zapcore.Encoder
- func Panic(args ...any)
- func Panicf(template string, args ...any)
- func PrintLogo()
- func ShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder)
- func StdPrintf(format string, v ...any)
- func StdPrintln(v ...any)
- func Sync() error
- func Warn(args ...any)
- func Warnf(template string, args ...any)
- type ComponentLogger
- type Config
- type ContextLogger
- type DefaultContextLogger
- type FieldCarrier
- type GetComponentLoggerOption
- type GetComponentLoggerOptions
- type Logger
- func (l *Logger) Apply(cfg *conf.Configuration)
- func (l *Logger) AsGlobal() *Logger
- func (l *Logger) ContextLogger() ContextLogger
- func (l *Logger) Ctx(ctx context.Context) *LoggerWithCtx
- func (l *Logger) Operator() *zap.Logger
- func (l *Logger) SetContextLogger(f ContextLogger)
- func (l *Logger) With(fields ...zap.Field) *Logger
- func (l *Logger) WithOptions(opts ...zap.Option) *Logger
- type LoggerWithCtx
- func (c *LoggerWithCtx) DPanic(msg string, fields ...zap.Field)
- func (c *LoggerWithCtx) Debug(msg string, fields ...zap.Field)
- func (c *LoggerWithCtx) Error(msg string, fields ...zap.Field)
- func (c *LoggerWithCtx) Fatal(msg string, fields ...zap.Field)
- func (c *LoggerWithCtx) Info(msg string, fields ...zap.Field)
- func (c *LoggerWithCtx) Log(lvl zapcore.Level, msg string, fields []zap.Field)
- func (c *LoggerWithCtx) Panic(msg string, fields ...zap.Field)
- func (c *LoggerWithCtx) Warn(msg string, fields ...zap.Field)
- func (c *LoggerWithCtx) WithOptions(opts ...zap.Option) *LoggerWithCtx
Constants ¶
const ( StacktraceKey = "stacktrace" CallerSkip = 1 TraceIDKey = "trace_id" ComponentKey = "component" WebComponentName = "web" GrpcComponentName = "grpc" )
Variables ¶
var ( GetLoggerWithCtx = func(ctx context.Context, l *Logger) *LoggerWithCtx { lc := loggerWithCtxPool.Get().(*LoggerWithCtx) lc.ctx = ctx lc.l = l return lc } PutLoggerWithCtx = func(lc *LoggerWithCtx) { lc.ctx = nil lc.l = nil loggerWithCtxPool.Put(lc) } )
loggerWithCtxPool
var (
// Get retrieves a buffer from the pool, creating one if necessary.
Get = _pool.Get
)
Functions ¶
func AppendToIncomingContext ¶ added in v0.1.0
AppendToIncomingContext appends zap field to context logger
func DPanic ¶
func DPanic(args ...any)
DPanic uses fmt.Sprint to construct and log a message. In development, the logger then panics. (See DPanicLevel for details.)
func DPanicf ¶
DPanicf uses fmt.Sprintf to log a templated message. In development, the logger then panics. (See DPanicLevel for details.)
func DefaultTimeEncoder ¶ added in v0.0.3
func DefaultTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)
DefaultTimeEncoder serializes time.Time to a human-readable formatted string
func Fatal ¶
func Fatal(args ...any)
Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.
func NewIncomingContext ¶ added in v0.1.0
func NewIncomingContext(ctx context.Context, carrier *FieldCarrier, fields ...zap.Field) context.Context
NewIncomingContext creates a new context with logger carrier.
func NewTextEncoder ¶
NewTextEncoder creates a fast, low-allocation Text encoder. The encoder appropriately escapes all field keys and values. log format see https://github.com/tikv/rfcs/blob/master/text/0018-unified-log-format.md#log-fields-section
func Panic ¶
func Panic(args ...any)
Panic uses fmt.Sprint to construct and log a message, then panics.
func ShortCallerEncoder ¶ added in v0.0.3
func ShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder)
ShortCallerEncoder serializes a caller in file:line format.
Types ¶
type ComponentLogger ¶ added in v0.0.3
type ComponentLogger interface { // Logger return component's logger,if withFields is true,return logger with build in fields Logger(opts ...GetComponentLoggerOption) *Logger SetLogger(logger *Logger) Debug(msg string, fields ...zap.Field) Info(msg string, fields ...zap.Field) Warn(msg string, fields ...zap.Field) Error(msg string, fields ...zap.Field) DPanic(msg string, fields ...zap.Field) Panic(msg string, fields ...zap.Field) Fatal(msg string, fields ...zap.Field) Ctx(ctx context.Context) *LoggerWithCtx }
ComponentLogger is sample and base using for component that also carries a context.Context. It uses the global logger.
type Config ¶
type Config struct { // ZapConfigs is for initial zap multi core ZapConfigs []zap.Config `json:"cores" yaml:"cores"` // Rotate is for log rotate Rotate *rotate `json:"rotate" yaml:"rotate"` // Disable automatic timestamps in output if use textEncoder DisableTimestamp bool `json:"disableTimestamp" yaml:"disableTimestamp"` // DisableErrorVerbose stops annotating logs with the full verbose error // message. DisableErrorVerbose bool `json:"disableErrorVerbose" yaml:"disableErrorVerbose"` // WithTraceID configures the logger to add `trace_id` field to structured log messages. WithTraceID bool `json:"withTraceID" yaml:"withTraceID"` // TraceIDKey is the key used to store the trace ID. defaults to "trace_id". TraceIDKey string `json:"traceIDKey" yaml:"traceIDKey"` // contains filtered or unexported fields }
Config is logger schema ZapConfigs use as zap advance,such as zapcore.NewTee() Sole use as one zap logger core
type ContextLogger ¶ added in v0.1.0
type ContextLogger interface { // LogFields defined how to log field with context LogFields(logger *Logger, ctx context.Context, lvl zapcore.Level, msg string, fields []zap.Field) }
ContextLogger is functions to help ContextLogger logging,the functions are called each ComponentLogger call the logging method
type DefaultContextLogger ¶ added in v0.1.0
type DefaultContextLogger struct { }
DefaultContextLogger is hold a nothing
type FieldCarrier ¶ added in v0.1.0
FieldCarrier sample to carry context log, the carrier's fields will log by demand.
func FromIncomingContext ¶ added in v0.1.0
func FromIncomingContext(ctx context.Context) (*FieldCarrier, bool)
FromIncomingContext returns the logger stored in ctx, if any.
func NewCarrier ¶ added in v0.1.0
func NewCarrier() *FieldCarrier
NewCarrier create a new logger carrier
type GetComponentLoggerOption ¶ added in v0.1.0
type GetComponentLoggerOption func(*GetComponentLoggerOptions)
ComponentLogger is sample and base using for component that also carries a context.Context. It uses the global logger.
func WithContextLogger ¶ added in v0.1.0
func WithContextLogger() GetComponentLoggerOption
WithContextLogger returns the logger with context in the ComponentLogger.
func WithOriginalLogger ¶ added in v0.1.0
func WithOriginalLogger() GetComponentLoggerOption
WithOriginalLogger returns the original logger in the ComponentLogger.
type GetComponentLoggerOptions ¶ added in v0.1.0
type GetComponentLoggerOptions struct {
// contains filtered or unexported fields
}
ComponentLogger is sample and base using for component that also carries a context.Context. It uses the global logger.
type Logger ¶
type Logger struct { *zap.Logger WithTraceID bool TraceIDKey string // contains filtered or unexported fields }
Logger integrate the Uber Zap library to use in woocoo
if you prefer to golang builtin log style,use log.Info or log.Infof, if zap style,you should use log.Operator().Info() if you want to clone Logger,you can call WithOption WithTraceID indicate whether to add trace_id field to the log.
- web: from X-Request-Id header
- grpc: from metadata key "trace_id"
func InitGlobalLogger ¶ added in v0.1.0
func InitGlobalLogger() *Logger
func NewBuiltIn ¶
func NewBuiltIn() *Logger
NewBuiltIn create a logger by configuration path "log", it will be set as global logger but run only once.
func (*Logger) Apply ¶
func (l *Logger) Apply(cfg *conf.Configuration)
Apply implement Configurable interface which can initial from a file used in JSON,YAML Logger init trough Apply method will be set as Global.
func (*Logger) ContextLogger ¶ added in v0.1.0
func (l *Logger) ContextLogger() ContextLogger
ContextLogger return contextLogger field
func (*Logger) Ctx ¶ added in v0.1.0
func (l *Logger) Ctx(ctx context.Context) *LoggerWithCtx
Ctx returns a new logger with the context.
func (*Logger) SetContextLogger ¶ added in v0.1.0
func (l *Logger) SetContextLogger(f ContextLogger)
SetContextLogger set contextLogger field,if you use the contextLogger,can set or override it.
type LoggerWithCtx ¶ added in v0.1.0
type LoggerWithCtx struct {
// contains filtered or unexported fields
}
LoggerWithCtx is a wrapper for Logger that also carries a context.Context.
func NewLoggerWithCtx ¶ added in v0.1.0
func NewLoggerWithCtx(ctx context.Context, l *Logger) *LoggerWithCtx
NewLoggerWithCtx get a logger with context from pool
func (*LoggerWithCtx) DPanic ¶ added in v0.1.0
func (c *LoggerWithCtx) DPanic(msg string, fields ...zap.Field)
func (*LoggerWithCtx) Debug ¶ added in v0.1.0
func (c *LoggerWithCtx) Debug(msg string, fields ...zap.Field)
func (*LoggerWithCtx) Error ¶ added in v0.1.0
func (c *LoggerWithCtx) Error(msg string, fields ...zap.Field)
func (*LoggerWithCtx) Fatal ¶ added in v0.1.0
func (c *LoggerWithCtx) Fatal(msg string, fields ...zap.Field)
func (*LoggerWithCtx) Info ¶ added in v0.1.0
func (c *LoggerWithCtx) Info(msg string, fields ...zap.Field)
func (*LoggerWithCtx) Panic ¶ added in v0.1.0
func (c *LoggerWithCtx) Panic(msg string, fields ...zap.Field)
func (*LoggerWithCtx) Warn ¶ added in v0.1.0
func (c *LoggerWithCtx) Warn(msg string, fields ...zap.Field)
func (*LoggerWithCtx) WithOptions ¶ added in v0.1.0
func (c *LoggerWithCtx) WithOptions(opts ...zap.Option) *LoggerWithCtx
WithOptions reset the logger with options.