logger

package
v1.4.15 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
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 Configure

func Configure(config Config)

Configure sets up the defaultLogger

func ConfigureTrafficLog

func ConfigureTrafficLog(config TrafficLogConfig)

ConfigureTrafficLog sets up traffic logging

func CopyToContext

func CopyToContext(srcCtx, dstCtx context.Context) context.Context

CopyToContext copy logger from srcCtx to dstCtx

func CopyTrafficToContext

func CopyTrafficToContext(srcCtx context.Context, dstCtx context.Context) context.Context

CopyTrafficToContext copies the traffic logger from the current context to the new context

func Data

func Data(tc *Traffic)

Data Log a request

func DataWith

func DataWith(tc *Traffic, fields Fields)

DataWith Log a request with fields

func Debug

func Debug(msg string)

Debug Log a message at the debug defaultLevel

func DebugWith

func DebugWith(msg string, fields Fields)

DebugWith Log a message with fields at the debug defaultLevel

func Debugf

func Debugf(format string, args ...any)

func Enabled

func Enabled(level Level) bool

func Error

func Error(msg string)

Error Log a message at the error defaultLevel

func ErrorWith

func ErrorWith(msg string, fields Fields)

ErrorWith Log a message with fields at the error defaultLevel

func Errorf

func Errorf(format string, args ...any)

func Info

func Info(msg string)

Info Log a message at the info defaultLevel

func InfoWith

func InfoWith(msg string, fields Fields)

InfoWith Log a message with fields at the info defaultLevel

func Infof

func Infof(format string, args ...any)

func JsonObjectWithOpts

func JsonObjectWithOpts(obj any, opts ...TrimOption) string

func SetLevel

func SetLevel(l Level)

func StringLimit

func StringLimit(s string, limit int) string

StringLimit returns a string with limited length at most

func TrimObject

func TrimObject(obj any) any

func TrimObjectWithOpts

func TrimObjectWithOpts(obj any, opts ...TrimOption) (ret any)

func Warn

func Warn(msg string)

Warn Log a message at the warn defaultLevel

func WarnWith

func WarnWith(msg string, fields Fields)

WarnWith Log a message with fields at the warn defaultLevel

func Warnf

func Warnf(format string, args ...any)

func WithLogger

func WithLogger(ctx context.Context, entry Entry) context.Context

WithLogger set given LogEntry to context and return new context, if ctx or entry is nil, return ctx

func WithTrafficEntry added in v1.1.0

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 {
	// LoggingLevel set log defaultLevel
	LoggingLevel Level
	// FileLoggingEnabled makes the framework log to a file
	// the fields below can be skipped if this value is false!
	FileLoggingEnabled bool
	// ConsoleLoggingEnabled makes the framework log to console
	ConsoleLoggingEnabled bool
	// 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 filelogging 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
	// ConsoleInfoStream
	ConsoleInfoStream *os.File
	// ConsoleErrorStream
	ConsoleErrorStream *os.File
	// ConsoleDebugStream
	ConsoleDebugStream *os.File
}

Config for logging

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

func FromContext(ctx context.Context) Entry

FromContext get Entry from context, if not found, return default logger

func NewEntry

func NewEntry(config Config) Entry

NewEntry create a new LogEntry instead of override defaultzaplogger

func With

func With(data any) Entry

With binds a default field to a log message

func WithError

func WithError(err error) Entry

WithError binds an error to a log message

func WithField

func WithField(k string, v any) Entry

WithField binds a field to a log message

func WithFields

func WithFields(fields Fields) Entry

WithFields binds a set of fields to a log message

func WithTracing

func WithTracing(requestId string) Entry

WithTracing create copy of LogEntry with tracing.Span

type Fields

type Fields map[string]any

type Level

type Level zapcore.Level

func GetLevel

func GetLevel() Level

type LogEntry

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

func (*LogEntry) Debug

func (le *LogEntry) Debug(msg string)

Debug logs a message at DebugLevel.

func (*LogEntry) DebugWith

func (le *LogEntry) DebugWith(msg string, fields Fields)

DebugWith logs a message with fields at DebugLevel.

func (*LogEntry) Debugf

func (le *LogEntry) Debugf(format string, args ...any)

Debugf logs a message at DebugLevel.

func (*LogEntry) Enabled

func (le *LogEntry) Enabled(level Level) bool

func (*LogEntry) Error

func (le *LogEntry) Error(msg string)

Error logs a message at ErrorLevel.

func (*LogEntry) ErrorWith

func (le *LogEntry) ErrorWith(msg string, fields Fields)

ErrorWith logs a message with fields at ErrorLevel.

func (*LogEntry) Errorf

func (le *LogEntry) Errorf(format string, args ...any)

func (*LogEntry) Info

func (le *LogEntry) Info(msg string)

Info logs a message at InfoLevel.

func (*LogEntry) InfoWith

func (le *LogEntry) InfoWith(msg string, fields Fields)

InfoWith logs a message with fields at InfoLevel.

func (*LogEntry) Infof

func (le *LogEntry) Infof(format string, args ...any)

func (*LogEntry) Warn

func (le *LogEntry) Warn(msg string)

Warn logs a message at WarnLevel.

func (*LogEntry) WarnWith

func (le *LogEntry) WarnWith(msg string, fields Fields)

WarnWith logs a message with fields at WarnLevel.

func (*LogEntry) Warnf

func (le *LogEntry) Warnf(format string, args ...any)

func (*LogEntry) With

func (le *LogEntry) With(data any) Entry

With binds a default field to a log message

func (*LogEntry) WithError

func (le *LogEntry) WithError(err error) Entry

WithError binds a default error field to a log message

func (*LogEntry) WithField

func (le *LogEntry) WithField(k string, v any) Entry

WithField binds a field to a log message

func (*LogEntry) WithFields

func (le *LogEntry) WithFields(fields Fields) Entry

WithFields Add a map of fields to the Entry.

func (*LogEntry) WithTracing

func (le *LogEntry) WithTracing(requestId string) Entry

WithTracing create copy of LogEntry with tracing.Span

type LogTrafficEntry

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

func (*LogTrafficEntry) Data

func (le *LogTrafficEntry) Data(tc *Traffic)

Data Log a request

func (*LogTrafficEntry) DataWith

func (le *LogTrafficEntry) DataWith(tc *Traffic, fields Fields)

DataWith Log a request with fields

func (*LogTrafficEntry) Start added in v1.4.10

func (le *LogTrafficEntry) Start(req *TrafficReq, fields Fields) *TrafficRec

func (*LogTrafficEntry) WithFields

func (le *LogTrafficEntry) WithFields(fields Fields) TrafficEntry

WithFields modifies an existing dataLogger with new fields (cannot be removed)

func (*LogTrafficEntry) WithIgnores

func (le *LogTrafficEntry) WithIgnores(ignores ...string) TrafficEntry

func (*LogTrafficEntry) WithPolicy

func (le *LogTrafficEntry) WithPolicy(policy Policy) TrafficEntry

WithPolicy create copy of LogEntry with policy disable: true: disable policy, false: enable policy

func (*LogTrafficEntry) WithTracing

func (le *LogTrafficEntry) WithTracing(requestId string) TrafficEntry

WithTracing create copy of LogEntry with tracing.Span

type ObjectTrimmer

type ObjectTrimmer struct {
	ArrLimit   int
	StrLimit   int
	DeepLimit  int
	WholeLimit int
	Ignores    []string
}

type Policy

type Policy interface {
	Allow() bool
}

func NewAllowAllPolicy

func NewAllowAllPolicy() Policy

func NewRateLimitPolicy

func NewRateLimitPolicy(r float64, b int) Policy

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

func NewSamplingPolicy(ratio float64) Policy

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 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, access or request
	Cmd  string        // Cmd: command
	Code int           // Code: error code
	Msg  string        // Msg: error message if you have
	Cost time.Duration // Cost: elapse of processing
	Req  any
	Resp any
}

Traffic is provided by user when logging

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 *TrafficReq, fields Fields) *TrafficRec
}

func TrafficEntryFromContext added in v1.1.0

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 TrafficLogConfig

type TrafficLogConfig struct {

	// FileLoggingEnabled makes the framework log to a file
	// the fields below can be skipped if this value is false!
	FileLoggingEnabled bool
	// ConsoleLoggingEnabled makes the framework log to console
	ConsoleLoggingEnabled bool
	// LoggingDirectory to log to to when filelogging is enabled
	LoggingDirectory 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
	// ConsoleStream
	ConsoleStream *os.File
}

TrafficLogConfig for traffic logging

type TrafficRec added in v1.4.10

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

func StartTrafficRec added in v1.4.10

func StartTrafficRec(ctx context.Context, req *TrafficReq, fields Fields) *TrafficRec

StartTrafficRec starts a new traffic log entry

func (*TrafficRec) End added in v1.4.10

func (t *TrafficRec) End(resp *TrafficResp, fields Fields)

type TrafficReq added in v1.4.10

type TrafficReq struct {
	Cmd string // Cmd: command
	Req any
}

TrafficReq is provided by user when logging

type TrafficResp added in v1.4.10

type TrafficResp struct {
	Code int    // Code: error code
	Msg  string // Msg: error message if you have
	Resp any
}

type TrafficTyp

type TrafficTyp string
const (
	TrafficTypReq  TrafficTyp = "req_to"
	TrafficTypResp TrafficTyp = "resp_from"
)

type TrimOption

type TrimOption func(*ObjectTrimmer)

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

func WithWholeLimit

func WithWholeLimit(limit int) TrimOption

Jump to

Keyboard shortcuts

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