khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame^] | 1 | package zerolog |
| 2 | |
| 3 | import "time" |
| 4 | import "sync/atomic" |
| 5 | |
| 6 | var ( |
| 7 | // TimestampFieldName is the field name used for the timestamp field. |
| 8 | TimestampFieldName = "time" |
| 9 | |
| 10 | // LevelFieldName is the field name used for the level field. |
| 11 | LevelFieldName = "level" |
| 12 | |
| 13 | // MessageFieldName is the field name used for the message field. |
| 14 | MessageFieldName = "message" |
| 15 | |
| 16 | // ErrorFieldName is the field name used for error fields. |
| 17 | ErrorFieldName = "error" |
| 18 | |
| 19 | // CallerFieldName is the field name used for caller field. |
| 20 | CallerFieldName = "caller" |
| 21 | |
| 22 | // CallerSkipFrameCount is the number of stack frames to skip to find the caller. |
| 23 | CallerSkipFrameCount = 2 |
| 24 | |
| 25 | // TimeFieldFormat defines the time format of the Time field type. |
| 26 | // If set to an empty string, the time is formatted as an UNIX timestamp |
| 27 | // as integer. |
| 28 | TimeFieldFormat = time.RFC3339 |
| 29 | |
| 30 | // TimestampFunc defines the function called to generate a timestamp. |
| 31 | TimestampFunc = time.Now |
| 32 | |
| 33 | // DurationFieldUnit defines the unit for time.Duration type fields added |
| 34 | // using the Dur method. |
| 35 | DurationFieldUnit = time.Millisecond |
| 36 | |
| 37 | // DurationFieldInteger renders Dur fields as integer instead of float if |
| 38 | // set to true. |
| 39 | DurationFieldInteger = false |
| 40 | |
| 41 | // ErrorHandler is called whenever zerolog fails to write an event on its |
| 42 | // output. If not set, an error is printed on the stderr. This handler must |
| 43 | // be thread safe and non-blocking. |
| 44 | ErrorHandler func(err error) |
| 45 | ) |
| 46 | |
| 47 | var ( |
| 48 | gLevel = new(uint32) |
| 49 | disableSampling = new(uint32) |
| 50 | ) |
| 51 | |
| 52 | // SetGlobalLevel sets the global override for log level. If this |
| 53 | // values is raised, all Loggers will use at least this value. |
| 54 | // |
| 55 | // To globally disable logs, set GlobalLevel to Disabled. |
| 56 | func SetGlobalLevel(l Level) { |
| 57 | atomic.StoreUint32(gLevel, uint32(l)) |
| 58 | } |
| 59 | |
| 60 | // GlobalLevel returns the current global log level |
| 61 | func GlobalLevel() Level { |
| 62 | return Level(atomic.LoadUint32(gLevel)) |
| 63 | } |
| 64 | |
| 65 | // DisableSampling will disable sampling in all Loggers if true. |
| 66 | func DisableSampling(v bool) { |
| 67 | var i uint32 |
| 68 | if v { |
| 69 | i = 1 |
| 70 | } |
| 71 | atomic.StoreUint32(disableSampling, i) |
| 72 | } |
| 73 | |
| 74 | func samplingDisabled() bool { |
| 75 | return atomic.LoadUint32(disableSampling) == 1 |
| 76 | } |