...

Source file src/github.com/rs/zerolog/globals.go

Documentation: github.com/rs/zerolog

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

View as plain text