logx
#
logx configurationsServiceName
: set the service name, optional. onvolume
mode, the name is used to generate the log files. Withinrest/zrpc
services, the name will be set to the name ofrest
orzrpc
automatically.Mode
: the mode to output the logs, default isconsole
.console
mode writes the logs tostdout/stderr
.file
mode writes the logs to the files specified byPath
.volume
mode is used in docker, to write logs into mounted volumes.
Encoding
: indicates how to encode the logs, default isjson
.json
mode writes the logs in json format.plain
mode writes the logs with plain text, with terminal color enabled.
TimeFormat
: customize the time format, optional. Default is2006-01-02T15:04:05.000Z07:00
.Path
: set the log path, default tologs
.Level
: the logging level to filter logs. Default isinfo
.info
, all logs are written.error
,info
logs are suppressed.severe
,info
anderror
logs are suppressed, onlysevere
logs are written.
Compress
: whether or not to compress log files, only works withfile
mode.KeepDays
: how many days that the log files are kept, after the given days, the outdated files will be deleted automatically. It has no effect onconsole
mode.StackCooldownMillis
: how many milliseconds to rewrite stacktrace again. It’s used to avoid stacktrace flooding.
#
Logging methodsError
,Info
,Slow
: write any kind of messages into logs, with likefmt.Sprint(…)
.Errorf
,Infof
,Slowf
: write messages with given format into logs.Errorv
,Infov
,Slowv
: write any kind of messages into logs, with json marshalling to encode them.Errorw
,Infow
,Sloww
: write the string message with givenkey:value
fields.WithContext
: inject the given ctx into the log messages, typically used to logtrace-id
andspan-id
.WithDuration
: write elapsed duration into the log messages, with keyduration
.
#
Integrating with third-party logging libs- zap
- logrus
For more libs, please implement and PR to https://github.com/zeromicro/zero-contrib
#
Write the logs to specific storeslogx
defined two interfaces to let you customize logx
to write logs into any stores.
logx.NewWriter(w io.Writer)
logx.SetWriter(writer logx.Writer)
For example, if we want to write the logs into kafka instead of console or files, we can do it like below:
Complete code: https://github.com/zeromicro/zero-examples/blob/main/logx/tokafka/main.go
#
Filtering sensitive fieldsIf we need to prevent the password
fields from logging, we can do it like below:
Complete code: https://github.com/zeromicro/zero-examples/blob/main/logx/filterfields/main.go