Debian Codegraph @

exzerolog/defaults.go

raw
1 // Copyright (c) 2024 Tulir Asokan
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 package exzerolog
8
9 import (
10 "time"
11
12 "github.com/rs/zerolog"
13 deflog "github.com/rs/zerolog/log"
14 )
15
16 // SetupDefaults updates zerolog globals with sensible defaults.
17 //
18 // * [zerolog.TimeFieldFormat] is set to time.RFC3339Nano instead of time.RFC3339
19 // * [zerolog.CallerMarshalFunc] is set to [CallerWithFunctionName]
20 // * [zerolog.DefaultContextLogger] is set to the given logger with default_context_log=true and caller info enabled
21 // * The global default [log.Logger] is set to the given logger with global_log=true and caller info enabled
22 // * [zerolog.LevelColors] are updated to swap trace and debug level colors
23 func SetupDefaults(log *zerolog.Logger) {
24 zerolog.TimeFieldFormat = time.RFC3339Nano
25 zerolog.CallerMarshalFunc = CallerWithFunctionName
26 defaultCtxLog := log.With().Bool("default_context_log", true).Caller().Logger()
27 zerolog.DefaultContextLogger = &defaultCtxLog
28 deflog.Logger = log.With().Bool("global_log", true).Caller().Logger()
29 // Swap trace and debug level colors so trace pops out the least
30 zerolog.LevelColors[zerolog.TraceLevel] = 0
31 zerolog.LevelColors[zerolog.DebugLevel] = 34 // blue
32 }