2018-04-30 18:42:17 +05:00
|
|
|
package zerolog
|
|
|
|
|
2019-03-07 07:56:50 +05:00
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
2018-04-30 18:42:17 +05:00
|
|
|
import "sync/atomic"
|
|
|
|
|
2019-10-13 13:55:38 +05:00
|
|
|
const (
|
|
|
|
// TimeFormatUnix defines a time format that makes time fields to be
|
|
|
|
// serialized as Unix timestamp integers.
|
|
|
|
TimeFormatUnix = ""
|
|
|
|
|
|
|
|
// TimeFormatUnix defines a time format that makes time fields to be
|
|
|
|
// serialized as Unix timestamp integers in milliseconds.
|
|
|
|
TimeFormatUnixMs = "UNIXMS"
|
|
|
|
)
|
|
|
|
|
2018-04-30 18:42:17 +05: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"
|
|
|
|
|
2019-10-13 13:55:38 +05:00
|
|
|
// LevelFieldMarshalFunc allows customization of global level field marshaling
|
|
|
|
LevelFieldMarshalFunc = func(l Level) string {
|
|
|
|
return l.String()
|
|
|
|
}
|
|
|
|
|
2018-04-30 18:42:17 +05:00
|
|
|
// MessageFieldName is the field name used for the message field.
|
|
|
|
MessageFieldName = "message"
|
|
|
|
|
|
|
|
// ErrorFieldName is the field name used for error fields.
|
|
|
|
ErrorFieldName = "error"
|
|
|
|
|
|
|
|
// CallerFieldName is the field name used for caller field.
|
|
|
|
CallerFieldName = "caller"
|
|
|
|
|
2019-03-07 07:56:50 +05:00
|
|
|
// CallerSkipFrameCount is the number of stack frames to skip to find the caller.
|
|
|
|
CallerSkipFrameCount = 2
|
|
|
|
|
|
|
|
// CallerMarshalFunc allows customization of global caller marshaling
|
|
|
|
CallerMarshalFunc = func(file string, line int) string {
|
2019-10-13 13:55:38 +05:00
|
|
|
return file + ":" + strconv.Itoa(line)
|
2019-03-07 07:56:50 +05: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
|
|
|
|
}
|
|
|
|
|
2019-10-13 13:55:38 +05:00
|
|
|
// TimeFieldFormat defines the time format of the Time field type. If set to
|
|
|
|
// TimeFormatUnix or TimeFormatUnixMs, the time is formatted as an UNIX
|
|
|
|
// timestamp as integer.
|
2018-04-30 18:42:17 +05:00
|
|
|
TimeFieldFormat = time.RFC3339
|
|
|
|
|
|
|
|
// TimestampFunc defines the function called to generate a timestamp.
|
|
|
|
TimestampFunc = time.Now
|
|
|
|
|
|
|
|
// 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
|
2019-03-07 07:56:50 +05: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)
|
2018-04-30 18:42:17 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
gLevel = new(uint32)
|
|
|
|
disableSampling = new(uint32)
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
atomic.StoreUint32(gLevel, uint32(l))
|
|
|
|
}
|
|
|
|
|
2019-03-07 07:56:50 +05:00
|
|
|
// GlobalLevel returns the current global log level
|
|
|
|
func GlobalLevel() Level {
|
2018-04-30 18:42:17 +05:00
|
|
|
return Level(atomic.LoadUint32(gLevel))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableSampling will disable sampling in all Loggers if true.
|
|
|
|
func DisableSampling(v bool) {
|
|
|
|
var i uint32
|
|
|
|
if v {
|
|
|
|
i = 1
|
|
|
|
}
|
|
|
|
atomic.StoreUint32(disableSampling, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func samplingDisabled() bool {
|
|
|
|
return atomic.LoadUint32(disableSampling) == 1
|
|
|
|
}
|