2017-05-15 18:16:13 +00:00
|
|
|
package zerolog
|
|
|
|
|
2019-02-20 19:39:29 +00:00
|
|
|
import (
|
2021-05-20 12:46:36 +00:00
|
|
|
"encoding/json"
|
2019-02-20 19:39:29 +00:00
|
|
|
"strconv"
|
2019-11-04 19:39:22 +00:00
|
|
|
"sync/atomic"
|
2019-02-20 19:39:29 +00:00
|
|
|
"time"
|
|
|
|
)
|
2017-05-15 18:16:13 +00:00
|
|
|
|
2019-04-19 22:48:31 +00:00
|
|
|
const (
|
|
|
|
// TimeFormatUnix defines a time format that makes time fields to be
|
|
|
|
// serialized as Unix timestamp integers.
|
|
|
|
TimeFormatUnix = ""
|
|
|
|
|
2019-10-24 10:02:36 +00:00
|
|
|
// TimeFormatUnixMs defines a time format that makes time fields to be
|
2019-04-19 22:48:31 +00:00
|
|
|
// serialized as Unix timestamp integers in milliseconds.
|
|
|
|
TimeFormatUnixMs = "UNIXMS"
|
2019-10-10 03:35:32 +00:00
|
|
|
|
2019-10-25 00:02:51 +00:00
|
|
|
// TimeFormatUnixMicro defines a time format that makes time fields to be
|
2019-10-10 03:35:32 +00:00
|
|
|
// serialized as Unix timestamp integers in microseconds.
|
2019-10-25 00:02:51 +00:00
|
|
|
TimeFormatUnixMicro = "UNIXMICRO"
|
2019-04-19 22:48:31 +00:00
|
|
|
)
|
|
|
|
|
2017-05-15 18:16:13 +00:00
|
|
|
var (
|
|
|
|
// TimestampFieldName is the field name used for the timestamp field.
|
|
|
|
TimestampFieldName = "time"
|
|
|
|
|
|
|
|
// LevelFieldName is the field name used for the level field.
|
|
|
|
LevelFieldName = "level"
|
|
|
|
|
2021-05-13 22:05:00 +00:00
|
|
|
// LevelTraceValue is the value used for the trace level field.
|
|
|
|
LevelTraceValue = "trace"
|
|
|
|
// LevelDebugValue is the value used for the debug level field.
|
|
|
|
LevelDebugValue = "debug"
|
|
|
|
// LevelInfoValue is the value used for the info level field.
|
|
|
|
LevelInfoValue = "info"
|
|
|
|
// LevelWarnValue is the value used for the warn level field.
|
|
|
|
LevelWarnValue = "warn"
|
|
|
|
// LevelErrorValue is the value used for the error level field.
|
|
|
|
LevelErrorValue = "error"
|
|
|
|
// LevelFatalValue is the value used for the fatal level field.
|
|
|
|
LevelFatalValue = "fatal"
|
|
|
|
// LevelPanicValue is the value used for the panic level field.
|
|
|
|
LevelPanicValue = "panic"
|
|
|
|
|
|
|
|
// LevelFieldMarshalFunc allows customization of global level field marshaling.
|
2019-03-02 00:08:23 +00:00
|
|
|
LevelFieldMarshalFunc = func(l Level) string {
|
|
|
|
return l.String()
|
|
|
|
}
|
|
|
|
|
2017-05-15 18:16:13 +00:00
|
|
|
// MessageFieldName is the field name used for the message field.
|
|
|
|
MessageFieldName = "message"
|
|
|
|
|
|
|
|
// ErrorFieldName is the field name used for error fields.
|
|
|
|
ErrorFieldName = "error"
|
|
|
|
|
2018-02-07 21:54:26 +00:00
|
|
|
// CallerFieldName is the field name used for caller field.
|
|
|
|
CallerFieldName = "caller"
|
|
|
|
|
2018-05-17 01:42:33 +00:00
|
|
|
// CallerSkipFrameCount is the number of stack frames to skip to find the caller.
|
|
|
|
CallerSkipFrameCount = 2
|
|
|
|
|
2019-02-20 19:39:29 +00:00
|
|
|
// CallerMarshalFunc allows customization of global caller marshaling
|
|
|
|
CallerMarshalFunc = func(file string, line int) string {
|
2019-03-02 00:08:23 +00:00
|
|
|
return file + ":" + strconv.Itoa(line)
|
2019-02-20 19:39:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 19:04:23 +00:00
|
|
|
// ErrorStackFieldName is the field name used for error stacks.
|
|
|
|
ErrorStackFieldName = "stack"
|
|
|
|
|
|
|
|
// ErrorStackMarshaler extract the stack from err if any.
|
|
|
|
ErrorStackMarshaler func(err error) interface{}
|
|
|
|
|
|
|
|
// ErrorMarshalFunc allows customization of global error marshaling
|
|
|
|
ErrorMarshalFunc = func(err error) interface{} {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-20 12:46:36 +00:00
|
|
|
// InterfaceMarshalFunc allows customization of interface marshaling.
|
|
|
|
// Default: "encoding/json.Marshal"
|
|
|
|
InterfaceMarshalFunc = json.Marshal
|
|
|
|
|
2019-04-19 22:48:31 +00:00
|
|
|
// TimeFieldFormat defines the time format of the Time field type. If set to
|
2019-10-25 00:02:51 +00:00
|
|
|
// TimeFormatUnix, TimeFormatUnixMs or TimeFormatUnixMicro, the time is formatted as an UNIX
|
2019-04-19 22:48:31 +00:00
|
|
|
// timestamp as integer.
|
2017-05-15 18:16:13 +00:00
|
|
|
TimeFieldFormat = time.RFC3339
|
2017-05-20 09:22:57 +00:00
|
|
|
|
|
|
|
// TimestampFunc defines the function called to generate a timestamp.
|
|
|
|
TimestampFunc = time.Now
|
2017-05-21 04:08:42 +00:00
|
|
|
|
|
|
|
// DurationFieldUnit defines the unit for time.Duration type fields added
|
|
|
|
// using the Dur method.
|
|
|
|
DurationFieldUnit = time.Millisecond
|
|
|
|
|
|
|
|
// DurationFieldInteger renders Dur fields as integer instead of float if
|
|
|
|
// set to true.
|
|
|
|
DurationFieldInteger = false
|
2018-11-20 18:54:42 +00:00
|
|
|
|
|
|
|
// ErrorHandler is called whenever zerolog fails to write an event on its
|
|
|
|
// output. If not set, an error is printed on the stderr. This handler must
|
|
|
|
// be thread safe and non-blocking.
|
|
|
|
ErrorHandler func(err error)
|
2017-05-15 18:16:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-11-04 19:39:22 +00:00
|
|
|
gLevel = new(int32)
|
|
|
|
disableSampling = new(int32)
|
2017-05-15 18:16:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetGlobalLevel sets the global override for log level. If this
|
|
|
|
// values is raised, all Loggers will use at least this value.
|
|
|
|
//
|
|
|
|
// To globally disable logs, set GlobalLevel to Disabled.
|
|
|
|
func SetGlobalLevel(l Level) {
|
2019-11-04 19:39:22 +00:00
|
|
|
atomic.StoreInt32(gLevel, int32(l))
|
2017-05-15 18:16:13 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 22:52:22 +00:00
|
|
|
// GlobalLevel returns the current global log level
|
|
|
|
func GlobalLevel() Level {
|
2019-11-04 19:39:22 +00:00
|
|
|
return Level(atomic.LoadInt32(gLevel))
|
2017-05-15 18:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DisableSampling will disable sampling in all Loggers if true.
|
|
|
|
func DisableSampling(v bool) {
|
2019-11-04 19:39:22 +00:00
|
|
|
var i int32
|
2017-05-15 18:16:13 +00:00
|
|
|
if v {
|
|
|
|
i = 1
|
|
|
|
}
|
2019-11-04 19:39:22 +00:00
|
|
|
atomic.StoreInt32(disableSampling, i)
|
2017-05-15 18:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func samplingDisabled() bool {
|
2019-11-04 19:39:22 +00:00
|
|
|
return atomic.LoadInt32(disableSampling) == 1
|
2017-05-15 18:16:13 +00:00
|
|
|
}
|