1package zerolog 2 3import ( 4 "strconv" 5 "time" 6) 7import "sync/atomic" 8 9const ( 10 // TimeFormatUnix defines a time format that makes time fields to be 11 // serialized as Unix timestamp integers. 12 TimeFormatUnix = "" 13 14 // TimeFormatUnix defines a time format that makes time fields to be 15 // serialized as Unix timestamp integers in milliseconds. 16 TimeFormatUnixMs = "UNIXMS" 17) 18 19var ( 20 // TimestampFieldName is the field name used for the timestamp field. 21 TimestampFieldName = "time" 22 23 // LevelFieldName is the field name used for the level field. 24 LevelFieldName = "level" 25 26 // LevelFieldMarshalFunc allows customization of global level field marshaling 27 LevelFieldMarshalFunc = func(l Level) string { 28 return l.String() 29 } 30 31 // MessageFieldName is the field name used for the message field. 32 MessageFieldName = "message" 33 34 // ErrorFieldName is the field name used for error fields. 35 ErrorFieldName = "error" 36 37 // CallerFieldName is the field name used for caller field. 38 CallerFieldName = "caller" 39 40 // CallerSkipFrameCount is the number of stack frames to skip to find the caller. 41 CallerSkipFrameCount = 2 42 43 // CallerMarshalFunc allows customization of global caller marshaling 44 CallerMarshalFunc = func(file string, line int) string { 45 return file + ":" + strconv.Itoa(line) 46 } 47 48 // ErrorStackFieldName is the field name used for error stacks. 49 ErrorStackFieldName = "stack" 50 51 // ErrorStackMarshaler extract the stack from err if any. 52 ErrorStackMarshaler func(err error) interface{} 53 54 // ErrorMarshalFunc allows customization of global error marshaling 55 ErrorMarshalFunc = func(err error) interface{} { 56 return err 57 } 58 59 // TimeFieldFormat defines the time format of the Time field type. If set to 60 // TimeFormatUnix or TimeFormatUnixMs, the time is formatted as an UNIX 61 // timestamp as integer. 62 TimeFieldFormat = time.RFC3339 63 64 // TimestampFunc defines the function called to generate a timestamp. 65 TimestampFunc = time.Now 66 67 // DurationFieldUnit defines the unit for time.Duration type fields added 68 // using the Dur method. 69 DurationFieldUnit = time.Millisecond 70 71 // DurationFieldInteger renders Dur fields as integer instead of float if 72 // set to true. 73 DurationFieldInteger = false 74 75 // ErrorHandler is called whenever zerolog fails to write an event on its 76 // output. If not set, an error is printed on the stderr. This handler must 77 // be thread safe and non-blocking. 78 ErrorHandler func(err error) 79) 80 81var ( 82 gLevel = new(uint32) 83 disableSampling = new(uint32) 84) 85 86// SetGlobalLevel sets the global override for log level. If this 87// values is raised, all Loggers will use at least this value. 88// 89// To globally disable logs, set GlobalLevel to Disabled. 90func SetGlobalLevel(l Level) { 91 atomic.StoreUint32(gLevel, uint32(l)) 92} 93 94// GlobalLevel returns the current global log level 95func GlobalLevel() Level { 96 return Level(atomic.LoadUint32(gLevel)) 97} 98 99// DisableSampling will disable sampling in all Loggers if true. 100func DisableSampling(v bool) { 101 var i uint32 102 if v { 103 i = 1 104 } 105 atomic.StoreUint32(disableSampling, i) 106} 107 108func samplingDisabled() bool { 109 return atomic.LoadUint32(disableSampling) == 1 110} 111