Documentation ¶
Index ¶
- Constants
- func Configure(config Config)
- func ConfigureTraffic(config TrafficConfig)
- func ConfigureTrafficWithOpts(opts ...TrafficConfigOption)
- func ConfigureWithOpts(opts ...ConfigOption)
- func CopyToContext(srcCtx, dstCtx context.Context) context.Context
- func CopyTrafficToContext(srcCtx context.Context, dstCtx context.Context) context.Context
- func Data(tc *Traffic)
- func DataWith(tc *Traffic, fields Fields)
- func Debug(msg string)
- func DebugWith(msg string, fields Fields)
- func Debugf(format string, args ...any)
- func Enabled(level Level) bool
- func Error(msg string)
- func ErrorWith(msg string, fields Fields)
- func Errorf(format string, args ...any)
- func Info(msg string)
- func InfoWith(msg string, fields Fields)
- func Infof(format string, args ...any)
- func ObjectTrimWithOpts(obj any, opts ...TrimOption) any
- func SetLevel(l Level)
- func SetupDefaultTrimmer(opts ...TrimOption)
- func Warn(msg string)
- func WarnWith(msg string, fields Fields)
- func Warnf(format string, args ...any)
- func WithLogger(ctx context.Context, entry Entry) context.Context
- func WithTrafficEntry(ctx context.Context, te TrafficEntry) context.Context
- type AllowAllPolicy
- type Config
- type ConfigOption
- func WithCallerEnabled(enabled bool) ConfigOption
- func WithCallerSkip(skip int) ConfigOption
- func WithConsoleEnabled(enabled bool) ConfigOption
- func WithDirectory(dir string) ConfigOption
- func WithFileEnabled(enabled bool) ConfigOption
- func WithFilename(filename string) ConfigOption
- func WithLoggerLevel(level Level) ConfigOption
- func WithMaxAge(age int) ConfigOption
- func WithMaxBackups(backups int) ConfigOption
- func WithMaxSize(size int) ConfigOption
- func WithSeparator(separator string) ConfigOption
- func WithSetAsDefaultLvl(set bool) ConfigOption
- func WithStream(stream WriterSyncer) ConfigOption
- type Entry
- func FromContext(ctx context.Context) Entry
- func NewEntry(config Config) Entry
- func NewEntryWithOpts(opts ...ConfigOption) Entry
- func With(data any) Entry
- func WithError(err error) Entry
- func WithField(k string, v any) Entry
- func WithFields(fields Fields) Entry
- func WithTracing(requestId string) Entry
- type Fields
- type Level
- type LogEntry
- func (le *LogEntry) Debug(msg string)
- func (le *LogEntry) DebugWith(msg string, fields Fields)
- func (le *LogEntry) Debugf(format string, args ...any)
- func (le *LogEntry) Enabled(level Level) bool
- func (le *LogEntry) Error(msg string)
- func (le *LogEntry) ErrorWith(msg string, fields Fields)
- func (le *LogEntry) Errorf(format string, args ...any)
- func (le *LogEntry) Info(msg string)
- func (le *LogEntry) InfoWith(msg string, fields Fields)
- func (le *LogEntry) Infof(format string, args ...any)
- func (le *LogEntry) Warn(msg string)
- func (le *LogEntry) WarnWith(msg string, fields Fields)
- func (le *LogEntry) Warnf(format string, args ...any)
- func (le *LogEntry) With(data any) Entry
- func (le *LogEntry) WithError(err error) Entry
- func (le *LogEntry) WithField(k string, v any) Entry
- func (le *LogEntry) WithFields(fields Fields) Entry
- func (le *LogEntry) WithTracing(requestId string) Entry
- type LogTrafficEntry
- func (te *LogTrafficEntry) Data(tc *Traffic)
- func (te *LogTrafficEntry) DataWith(tc *Traffic, fields Fields)
- func (te *LogTrafficEntry) Start(req *ReqEntity) *TrafficRec
- func (te *LogTrafficEntry) WithFields(fields Fields) TrafficEntry
- func (te *LogTrafficEntry) WithIgnores(ignores ...string) TrafficEntry
- func (te *LogTrafficEntry) WithPolicy(policy Policy) TrafficEntry
- func (te *LogTrafficEntry) WithTracing(requestId string) TrafficEntry
- type NoopSyncer
- type OutputTrimmer
- type Policy
- type RateLimitPolicy
- type RejectAllPolicy
- type ReqEntity
- type RespEntity
- type SamplingPolicy
- type Traffic
- type TrafficConfig
- type TrafficConfigOption
- func WithTrafficArrLimit(arrLimit int) TrafficConfigOption
- func WithTrafficDeepLimit(deepLimit int) TrafficConfigOption
- func WithTrafficDirectory(directory string) TrafficConfigOption
- func WithTrafficEnabled(enabled bool) TrafficConfigOption
- func WithTrafficFilename(filename string) TrafficConfigOption
- func WithTrafficIgnoresOpt(ignores ...string) TrafficConfigOption
- func WithTrafficMaxAge(maxAge int) TrafficConfigOption
- func WithTrafficMaxBackups(maxBackups int) TrafficConfigOption
- func WithTrafficMaxSize(maxSize int) TrafficConfigOption
- func WithTrafficStrLimit(strLimit int) TrafficConfigOption
- func WithTrafficStream(stream WriterSyncer) TrafficConfigOption
- type TrafficEntry
- func NewTrafficEntry(config TrafficConfig) TrafficEntry
- func NewTrafficEntryWithOpts(opts ...TrafficConfigOption) TrafficEntry
- func TrafficEntryFromContext(ctx context.Context) TrafficEntry
- func WithTrafficFields(ctx context.Context, fields Fields) TrafficEntry
- func WithTrafficIgnores(ctx context.Context, ignores ...string) TrafficEntry
- func WithTrafficTracing(ctx context.Context, requestId string) TrafficEntry
- type TrafficRec
- type TrafficTyp
- type TrimOption
- type WriterSyncer
Constants ¶
const ( // DebugLevel enum -1: logs are typically voluminous, and are usually disabled in // production. DebugLevel = Level(zapcore.DebugLevel) // InfoLevel enum 0: is the default logging priority. InfoLevel = Level(zapcore.InfoLevel) // WarnLevel enum 1: logs are more important than Info, but don't need individual // human review. WarnLevel = Level(zapcore.WarnLevel) // ErrorLevel enum 2: logs are high-priority. If an application is running smoothly, // it shouldn't generate any error-defaultLevel logs. ErrorLevel = Level(zapcore.ErrorLevel) )
Variables ¶
This section is empty.
Functions ¶
func ConfigureTraffic ¶
func ConfigureTraffic(config TrafficConfig)
ConfigureTraffic sets up traffic logging globally
func ConfigureTrafficWithOpts ¶
func ConfigureTrafficWithOpts(opts ...TrafficConfigOption)
ConfigureTrafficWithOpts sets up traffic logging with options globally
func ConfigureWithOpts ¶
func ConfigureWithOpts(opts ...ConfigOption)
func CopyToContext ¶
CopyToContext copy logger from srcCtx to dstCtx
func CopyTrafficToContext ¶
CopyTrafficToContext copies the traffic logger from the current context to the new context
func ObjectTrimWithOpts ¶ added in v1.1.1
func ObjectTrimWithOpts(obj any, opts ...TrimOption) any
func SetupDefaultTrimmer ¶ added in v1.1.1
func SetupDefaultTrimmer(opts ...TrimOption)
func WithLogger ¶
WithLogger set given LogEntry to context and return new context, if ctx or entry is nil, return ctx
func WithTrafficEntry ¶
func WithTrafficEntry(ctx context.Context, te TrafficEntry) context.Context
WithTrafficEntry set given LogTrafficEntry to context by using trafficLogCtxKey
Types ¶
type AllowAllPolicy ¶
type AllowAllPolicy struct{}
AllowAllPolicy allow all log print
func (*AllowAllPolicy) Allow ¶
func (ap *AllowAllPolicy) Allow() bool
type Config ¶
type Config struct { // LoggerLevel set log defaultLevel LoggerLevel Level // SetAsDefaultLvl set the default level SetAsDefaultLvl bool // FileEnabled makes the framework log to a file // the fields below can be skipped if this value is false! FileEnabled bool // ConsoleEnabled makes the framework log to console ConsoleEnabled bool // Separator is the separator for log messages Separator string // CallerEnabled makes the caller log to a file CallerEnabled bool // CallerSkip increases the number of callers skipped by caller CallerSkip int // Directory to log to to when FileEnabled is enabled Directory string // Filename is the name of the logfile which will be placed inside the directory Filename string // MaxSize the max size in MB of the logfile before it's rolled MaxSize int // MaxBackups the max number of rolled files to keep MaxBackups int // MaxAge the max age in days to keep a logfile MaxAge int // Stream Stream WriterSyncer }
Config for logging
type ConfigOption ¶
type ConfigOption func(*Config)
ConfigOption is a function that configures the logger
func WithCallerEnabled ¶
func WithCallerEnabled(enabled bool) ConfigOption
func WithCallerSkip ¶
func WithCallerSkip(skip int) ConfigOption
func WithConsoleEnabled ¶
func WithConsoleEnabled(enabled bool) ConfigOption
func WithDirectory ¶
func WithDirectory(dir string) ConfigOption
func WithFileEnabled ¶
func WithFileEnabled(enabled bool) ConfigOption
func WithFilename ¶
func WithFilename(filename string) ConfigOption
func WithLoggerLevel ¶
func WithLoggerLevel(level Level) ConfigOption
func WithMaxAge ¶
func WithMaxAge(age int) ConfigOption
func WithMaxBackups ¶
func WithMaxBackups(backups int) ConfigOption
func WithMaxSize ¶
func WithMaxSize(size int) ConfigOption
func WithSeparator ¶
func WithSeparator(separator string) ConfigOption
func WithSetAsDefaultLvl ¶
func WithSetAsDefaultLvl(set bool) ConfigOption
func WithStream ¶ added in v1.5.0
func WithStream(stream WriterSyncer) ConfigOption
type Entry ¶
type Entry interface { // Debug logs a message at DebugLevel. Debug(msg string) // Debugf logs a message at DebugLevel. Debugf(format string, args ...any) // DebugWith logs a message with fields at DebugLevel. DebugWith(msg string, fields Fields) // Info logs a message at InfoLevel. Info(msg string) // Infof logs a message at InfoLevel. Infof(format string, args ...any) // InfoWith logs a message with fields at InfoLevel. InfoWith(msg string, fields Fields) // Warn logs a message at WarnLevel. Warn(msg string) // Warnf logs a message at WarnLevel. Warnf(format string, args ...any) // WarnWith logs a message with fields at WarnLevel. WarnWith(msg string, fields Fields) // Error logs a message at ErrorLevel. Error(msg string) // Errorf logs a message at ErrorLevel. Errorf(format string, args ...any) // ErrorWith logs a message with fields at ErrorLevel. ErrorWith(msg string, fields Fields) // WithFields returns a new entry with after adding fields WithFields(fields Fields) Entry // WithField returns a new entry with after adding field WithField(k string, v any) Entry // With returns a new entry with after adding data with default field name With(data any) Entry // WithError returns a new entry with after adding error WithError(err error) Entry // WithTracing returns a new entry with after adding requestId WithTracing(requestId string) Entry // Enabled is entry enabled at level Enabled(level Level) bool }
func FromContext ¶
FromContext get Entry from context, if not found, return default logger
func NewEntryWithOpts ¶
func NewEntryWithOpts(opts ...ConfigOption) Entry
NewEntryWithOpts create a new LogEntry with options
func WithFields ¶
WithFields binds a set of fields to a log message
func WithTracing ¶
WithTracing create copy of LogEntry with tracing.Span
type LogEntry ¶
type LogEntry struct {
// contains filtered or unexported fields
}
func (*LogEntry) WithFields ¶
WithFields Add a map of fields to the Entry.
func (*LogEntry) WithTracing ¶
WithTracing create copy of LogEntry with tracing.Span
type LogTrafficEntry ¶
type LogTrafficEntry struct {
// contains filtered or unexported fields
}
func (*LogTrafficEntry) DataWith ¶
func (te *LogTrafficEntry) DataWith(tc *Traffic, fields Fields)
DataWith Log a request with fields
func (*LogTrafficEntry) Start ¶
func (te *LogTrafficEntry) Start(req *ReqEntity) *TrafficRec
func (*LogTrafficEntry) WithFields ¶
func (te *LogTrafficEntry) WithFields(fields Fields) TrafficEntry
WithFields modifies an existing dataLogger with new fields (cannot be removed)
func (*LogTrafficEntry) WithIgnores ¶
func (te *LogTrafficEntry) WithIgnores(ignores ...string) TrafficEntry
func (*LogTrafficEntry) WithPolicy ¶
func (te *LogTrafficEntry) WithPolicy(policy Policy) TrafficEntry
WithPolicy create copy of LogEntry with policy disable: true: disable policy, false: enable policy
func (*LogTrafficEntry) WithTracing ¶
func (te *LogTrafficEntry) WithTracing(requestId string) TrafficEntry
WithTracing create copy of LogEntry with tracing.Span
type NoopSyncer ¶ added in v1.5.0
type NoopSyncer struct{}
func (*NoopSyncer) Sync ¶ added in v1.5.0
func (ns *NoopSyncer) Sync() error
type OutputTrimmer ¶ added in v1.1.1
type OutputTrimmer struct {
// contains filtered or unexported fields
}
func NewOutputTrimmer ¶ added in v1.1.1
func NewOutputTrimmer(opts ...TrimOption) *OutputTrimmer
func (*OutputTrimmer) TrimObject ¶ added in v1.1.1
func (ot *OutputTrimmer) TrimObject(obj any) (ret any)
type Policy ¶
type Policy interface {
Allow() bool
}
func NewAllowAllPolicy ¶
func NewAllowAllPolicy() Policy
func NewRateLimitPolicy ¶
NewRateLimitPolicy create a rate limit policy r: rate, b: burst example: NewRateLimitPolicy(10, 1) means 10 logs per second
func NewRejectAllPolicy ¶
func NewRejectAllPolicy() Policy
func NewSamplingPolicy ¶
type RateLimitPolicy ¶
type RateLimitPolicy struct {
// contains filtered or unexported fields
}
RateLimitPolicy rate limit to control log print r: rate, b: burst
func (*RateLimitPolicy) Allow ¶
func (rp *RateLimitPolicy) Allow() bool
type RejectAllPolicy ¶
type RejectAllPolicy struct{}
RejectAllPolicy reject all log print
func (*RejectAllPolicy) Allow ¶
func (rp *RejectAllPolicy) Allow() bool
type ReqEntity ¶
type ReqEntity struct { Typ TrafficTyp // Typ: type of traffic, receive request or send request Cmd string // Cmd: command Req any Fields Fields // Fields: request additional fields }
ReqEntity is provided by user when logging
type RespEntity ¶
type SamplingPolicy ¶
type SamplingPolicy struct {
// contains filtered or unexported fields
}
SamplingPolicy log print sampling with ratio
func (*SamplingPolicy) Allow ¶
func (sp *SamplingPolicy) Allow() bool
type Traffic ¶
type Traffic struct { Typ TrafficTyp // Typ: type of traffic, receive request or send request Cmd string // Cmd: command Cost time.Duration // Cost: elapse of processing Code string // Code: error code Msg string // Msg: error message if you have Req any Resp any }
Traffic is provided by user when logging
type TrafficConfig ¶
type TrafficConfig struct { // Enabled makes the framework log traffic Enabled bool // Directory to log to when Enabled is enabled Directory string // Filename is the name of the logfile which will be placed inside the directory Filename string // MaxSize the max size in MB of the logfile before it's rolled MaxSize int // MaxBackups the max number of rolled files to keep MaxBackups int // MaxAge the max age in days to keep a logfile MaxAge int // StrLimit the max length of string to log StrLimit int // ArrLimit the max length of array to log ArrLimit int // DeepLimit the max depth of struct to log DeepLimit int // Ignores is a list of fields to ignore when logging Ignores []string // Stream is the stream to log to Stream WriterSyncer }
TrafficConfig for traffic logging
type TrafficConfigOption ¶
type TrafficConfigOption func(*TrafficConfig)
func WithTrafficArrLimit ¶ added in v1.1.1
func WithTrafficArrLimit(arrLimit int) TrafficConfigOption
func WithTrafficDeepLimit ¶ added in v1.1.1
func WithTrafficDeepLimit(deepLimit int) TrafficConfigOption
func WithTrafficDirectory ¶
func WithTrafficDirectory(directory string) TrafficConfigOption
func WithTrafficEnabled ¶ added in v1.5.0
func WithTrafficEnabled(enabled bool) TrafficConfigOption
func WithTrafficFilename ¶
func WithTrafficFilename(filename string) TrafficConfigOption
func WithTrafficIgnoresOpt ¶ added in v1.1.1
func WithTrafficIgnoresOpt(ignores ...string) TrafficConfigOption
func WithTrafficMaxAge ¶
func WithTrafficMaxAge(maxAge int) TrafficConfigOption
func WithTrafficMaxBackups ¶
func WithTrafficMaxBackups(maxBackups int) TrafficConfigOption
func WithTrafficMaxSize ¶
func WithTrafficMaxSize(maxSize int) TrafficConfigOption
func WithTrafficStrLimit ¶ added in v1.1.1
func WithTrafficStrLimit(strLimit int) TrafficConfigOption
func WithTrafficStream ¶ added in v1.5.0
func WithTrafficStream(stream WriterSyncer) TrafficConfigOption
type TrafficEntry ¶
type TrafficEntry interface { // Data logs traffic Data(traffic *Traffic) // DataWith logs traffic with fields DataWith(traffic *Traffic, fields Fields) // WithFields adds fields to traffic dataLogger WithFields(fields Fields) TrafficEntry // WithTracing adds requestId to traffic dataLogger WithTracing(requestId string) TrafficEntry // WithIgnores adds ignores to traffic dataLogger WithIgnores(ignores ...string) TrafficEntry // WithPolicy adds policy to traffic dataLogger // disable: true: disable policy, false: enable policy WithPolicy(policy Policy) TrafficEntry Start(req *ReqEntity) *TrafficRec }
func NewTrafficEntry ¶
func NewTrafficEntry(config TrafficConfig) TrafficEntry
func NewTrafficEntryWithOpts ¶
func NewTrafficEntryWithOpts(opts ...TrafficConfigOption) TrafficEntry
NewTrafficEntryWithOpts creates a new traffic entry with options
func TrafficEntryFromContext ¶
func TrafficEntryFromContext(ctx context.Context) TrafficEntry
TrafficEntryFromContext get traffic dataLogger from context, allows us to pass dataLogger between functions
func WithTrafficFields ¶
func WithTrafficFields(ctx context.Context, fields Fields) TrafficEntry
func WithTrafficIgnores ¶
func WithTrafficIgnores(ctx context.Context, ignores ...string) TrafficEntry
func WithTrafficTracing ¶
func WithTrafficTracing(ctx context.Context, requestId string) TrafficEntry
type TrafficRec ¶
type TrafficRec struct {
// contains filtered or unexported fields
}
func StartTrafficRec ¶
func StartTrafficRec(ctx context.Context, req *ReqEntity) *TrafficRec
StartTrafficRec starts a new traffic log entry
func (*TrafficRec) End ¶
func (t *TrafficRec) End(resp *RespEntity, fields Fields)
func (*TrafficRec) EndWithIgnores ¶ added in v1.5.2
func (t *TrafficRec) EndWithIgnores(resp *RespEntity, fields Fields, ignores ...string)
type TrafficTyp ¶
type TrafficTyp string
const ( TrafficTypRecv TrafficTyp = "recv" TrafficTypSend TrafficTyp = "send" )
type TrimOption ¶
type TrimOption func(*OutputTrimmer)
func WithArrLimit ¶
func WithArrLimit(limit int) TrimOption
func WithDeepLimit ¶
func WithDeepLimit(limit int) TrimOption
func WithIgnores ¶
func WithIgnores(ignores ...string) TrimOption
func WithStrLimit ¶
func WithStrLimit(limit int) TrimOption
type WriterSyncer ¶ added in v1.5.0
type WriterSyncer = zapcore.WriteSyncer
WriterSyncer is an interface that groups the Write and Sync methods.