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