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 Panic(args ...any)
- func Panicf(template string, args ...any)
- func PrintLogo()
- func Printf(format string, v ...any)
- func Println(v ...any)
- func ShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder)
- 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) IOWriter(level zapcore.Level) io.Writer
- 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
- type TextEncoder
- func (enc *TextEncoder) AddArray(key string, arr zapcore.ArrayMarshaler) error
- func (enc *TextEncoder) AddBinary(key string, val []byte)
- func (enc *TextEncoder) AddBool(key string, val bool)
- func (enc *TextEncoder) AddByteString(key string, val []byte)
- func (enc *TextEncoder) AddComplex128(key string, val complex128)
- func (enc *TextEncoder) AddComplex64(k string, v complex64)
- func (enc *TextEncoder) AddDuration(key string, val time.Duration)
- func (enc *TextEncoder) AddFloat32(k string, v float32)
- func (enc *TextEncoder) AddFloat64(key string, val float64)
- func (enc *TextEncoder) AddInt(k string, v int)
- func (enc *TextEncoder) AddInt16(k string, v int16)
- func (enc *TextEncoder) AddInt32(k string, v int32)
- func (enc *TextEncoder) AddInt64(key string, val int64)
- func (enc *TextEncoder) AddInt8(k string, v int8)
- func (enc *TextEncoder) AddObject(key string, obj zapcore.ObjectMarshaler) error
- func (enc *TextEncoder) AddReflected(key string, obj any) error
- func (enc *TextEncoder) AddString(key, val string)
- func (enc *TextEncoder) AddTime(key string, val time.Time)
- func (enc *TextEncoder) AddUint(k string, v uint)
- func (enc *TextEncoder) AddUint16(k string, v uint16)
- func (enc *TextEncoder) AddUint32(k string, v uint32)
- func (enc *TextEncoder) AddUint64(key string, val uint64)
- func (enc *TextEncoder) AddUint8(k string, v uint8)
- func (enc *TextEncoder) AddUintptr(k string, v uintptr)
- func (enc *TextEncoder) AppendArray(arr zapcore.ArrayMarshaler) error
- func (enc *TextEncoder) AppendBool(val bool)
- func (enc *TextEncoder) AppendByteString(val []byte)
- func (enc *TextEncoder) AppendComplex128(val complex128)
- func (enc *TextEncoder) AppendComplex64(v complex64)
- func (enc *TextEncoder) AppendDuration(val time.Duration)
- func (enc *TextEncoder) AppendFloat32(v float32)
- func (enc *TextEncoder) AppendFloat64(v float64)
- func (enc *TextEncoder) AppendInt(v int)
- func (enc *TextEncoder) AppendInt16(v int16)
- func (enc *TextEncoder) AppendInt32(v int32)
- func (enc *TextEncoder) AppendInt64(val int64)
- func (enc *TextEncoder) AppendInt8(v int8)
- func (enc *TextEncoder) AppendObject(obj zapcore.ObjectMarshaler) error
- func (enc *TextEncoder) AppendReflected(val any) error
- func (enc *TextEncoder) AppendString(val string)
- func (enc *TextEncoder) AppendTime(val time.Time)
- func (enc *TextEncoder) AppendUint(v uint)
- func (enc *TextEncoder) AppendUint16(v uint16)
- func (enc *TextEncoder) AppendUint32(v uint32)
- func (enc *TextEncoder) AppendUint64(val uint64)
- func (enc *TextEncoder) AppendUint8(v uint8)
- func (enc *TextEncoder) AppendUintptr(v uintptr)
- func (enc *TextEncoder) Clone() zapcore.Encoder
- func (enc *TextEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error)
- func (enc *TextEncoder) OpenNamespace(key string)
- func (enc *TextEncoder) String() string
- func (enc *TextEncoder) Truncate()
- type Writer
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
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 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 and init it if not exist. // The logger will be lazy init before logger SetLogger or use AsGlobal, // so you can call Logger() to init yourself in some scene,it will use global logger by default. // Notice GetComponentLoggerOption // if you want to get the original logger, you can use WithOriginalLogger() option. // If you want to get the logger with context, you can use WithContextLogger() option. // otherwise, you will get the 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.
func Component ¶ added in v0.0.3
func Component(name string, fields ...zap.Field) ComponentLogger
Component return a logger with name option base on a logger.
The logger will be lazy set up,Using the global logger by default.
func Global ¶
func Global() ComponentLogger
Global return struct logger if you want to use zap style logging.
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"` // DisableSampling disables sampling for all the loggers DisableSampling bool `json:"disableSampling" yaml:"disableSampling"` // 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 NewFromConf ¶ added in v0.5.0
func NewFromConf(cfg *conf.Configuration) *Logger
NewFromConf 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) IOWriter ¶ added in v0.4.0
IOWriter wrap to Io.Writer which can be used in golang builtin log. Level is the log level which will be written.
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.
type TextEncoder ¶
type TextEncoder struct { *zapcore.EncoderConfig // contains filtered or unexported fields }
func NewTextEncoder ¶
func NewTextEncoder(config zapcore.EncoderConfig, disableErrorVerbose, disableTimestamp, needQuotes bool) *TextEncoder
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. in same scenarios, you can use needQuotes=false to remove quotes if no need followed log-format.
func (*TextEncoder) AddArray ¶
func (enc *TextEncoder) AddArray(key string, arr zapcore.ArrayMarshaler) error
func (*TextEncoder) AddBinary ¶
func (enc *TextEncoder) AddBinary(key string, val []byte)
func (*TextEncoder) AddBool ¶
func (enc *TextEncoder) AddBool(key string, val bool)
func (*TextEncoder) AddByteString ¶
func (enc *TextEncoder) AddByteString(key string, val []byte)
func (*TextEncoder) AddComplex128 ¶
func (enc *TextEncoder) AddComplex128(key string, val complex128)
func (*TextEncoder) AddComplex64 ¶
func (enc *TextEncoder) AddComplex64(k string, v complex64)
func (*TextEncoder) AddDuration ¶
func (enc *TextEncoder) AddDuration(key string, val time.Duration)
func (*TextEncoder) AddFloat32 ¶
func (enc *TextEncoder) AddFloat32(k string, v float32)
func (*TextEncoder) AddFloat64 ¶
func (enc *TextEncoder) AddFloat64(key string, val float64)
func (*TextEncoder) AddInt ¶
func (enc *TextEncoder) AddInt(k string, v int)
func (*TextEncoder) AddInt16 ¶
func (enc *TextEncoder) AddInt16(k string, v int16)
func (*TextEncoder) AddInt32 ¶
func (enc *TextEncoder) AddInt32(k string, v int32)
func (*TextEncoder) AddInt64 ¶
func (enc *TextEncoder) AddInt64(key string, val int64)
func (*TextEncoder) AddInt8 ¶
func (enc *TextEncoder) AddInt8(k string, v int8)
func (*TextEncoder) AddObject ¶
func (enc *TextEncoder) AddObject(key string, obj zapcore.ObjectMarshaler) error
func (*TextEncoder) AddReflected ¶
func (enc *TextEncoder) AddReflected(key string, obj any) error
func (*TextEncoder) AddString ¶
func (enc *TextEncoder) AddString(key, val string)
func (*TextEncoder) AddUint ¶
func (enc *TextEncoder) AddUint(k string, v uint)
func (*TextEncoder) AddUint16 ¶
func (enc *TextEncoder) AddUint16(k string, v uint16)
func (*TextEncoder) AddUint32 ¶
func (enc *TextEncoder) AddUint32(k string, v uint32)
func (*TextEncoder) AddUint64 ¶
func (enc *TextEncoder) AddUint64(key string, val uint64)
func (*TextEncoder) AddUint8 ¶
func (enc *TextEncoder) AddUint8(k string, v uint8)
func (*TextEncoder) AddUintptr ¶
func (enc *TextEncoder) AddUintptr(k string, v uintptr)
func (*TextEncoder) AppendArray ¶
func (enc *TextEncoder) AppendArray(arr zapcore.ArrayMarshaler) error
func (*TextEncoder) AppendBool ¶
func (enc *TextEncoder) AppendBool(val bool)
func (*TextEncoder) AppendByteString ¶
func (enc *TextEncoder) AppendByteString(val []byte)
func (*TextEncoder) AppendComplex128 ¶
func (enc *TextEncoder) AppendComplex128(val complex128)
func (*TextEncoder) AppendComplex64 ¶
func (enc *TextEncoder) AppendComplex64(v complex64)
func (*TextEncoder) AppendDuration ¶
func (enc *TextEncoder) AppendDuration(val time.Duration)
func (*TextEncoder) AppendFloat32 ¶
func (enc *TextEncoder) AppendFloat32(v float32)
func (*TextEncoder) AppendFloat64 ¶
func (enc *TextEncoder) AppendFloat64(v float64)
func (*TextEncoder) AppendInt ¶
func (enc *TextEncoder) AppendInt(v int)
func (*TextEncoder) AppendInt16 ¶
func (enc *TextEncoder) AppendInt16(v int16)
func (*TextEncoder) AppendInt32 ¶
func (enc *TextEncoder) AppendInt32(v int32)
func (*TextEncoder) AppendInt64 ¶
func (enc *TextEncoder) AppendInt64(val int64)
func (*TextEncoder) AppendInt8 ¶
func (enc *TextEncoder) AppendInt8(v int8)
func (*TextEncoder) AppendObject ¶
func (enc *TextEncoder) AppendObject(obj zapcore.ObjectMarshaler) error
func (*TextEncoder) AppendReflected ¶
func (enc *TextEncoder) AppendReflected(val any) error
func (*TextEncoder) AppendString ¶
func (enc *TextEncoder) AppendString(val string)
func (*TextEncoder) AppendTime ¶
func (enc *TextEncoder) AppendTime(val time.Time)
func (*TextEncoder) AppendUint ¶
func (enc *TextEncoder) AppendUint(v uint)
func (*TextEncoder) AppendUint16 ¶
func (enc *TextEncoder) AppendUint16(v uint16)
func (*TextEncoder) AppendUint32 ¶
func (enc *TextEncoder) AppendUint32(v uint32)
func (*TextEncoder) AppendUint64 ¶
func (enc *TextEncoder) AppendUint64(val uint64)
func (*TextEncoder) AppendUint8 ¶
func (enc *TextEncoder) AppendUint8(v uint8)
func (*TextEncoder) AppendUintptr ¶
func (enc *TextEncoder) AppendUintptr(v uintptr)
func (*TextEncoder) Clone ¶
func (enc *TextEncoder) Clone() zapcore.Encoder
func (*TextEncoder) EncodeEntry ¶
func (*TextEncoder) OpenNamespace ¶
func (enc *TextEncoder) OpenNamespace(key string)
func (*TextEncoder) String ¶ added in v0.3.0
func (enc *TextEncoder) String() string
String returns the encoded log message.
func (*TextEncoder) Truncate ¶ added in v0.3.0
func (enc *TextEncoder) Truncate()
type Writer ¶ added in v0.4.0
type Writer struct { Log *zap.Logger // Default log level for the messages which can't inspect a level. // // If unspecified, defaults to Info. Level zapcore.Level // contains filtered or unexported fields }
Writer is an io.Writer that writes to the provided Zap logger. It is inspirited zapio.Writer
func (*Writer) Sync ¶ added in v0.4.0
Sync flushes buffered data to the logger as a new log entry even if it doesn't contain a newline. This stage use default level.