diff options
author | Emile <git@emile.space> | 2024-10-25 15:55:50 +0200 |
---|---|---|
committer | Emile <git@emile.space> | 2024-10-25 15:55:50 +0200 |
commit | c90f36e3dd179d2de96f4f5fe38d8dc9a9de6dfe (patch) | |
tree | 89e9afb41c5bf76f48cfb09305a2d3db8d302b06 /vendor/go.mau.fi/util/exzerolog/defaults.go | |
parent | 98bbb0f559a8883bc47bae80607dbe326a448e61 (diff) |
Diffstat (limited to 'vendor/go.mau.fi/util/exzerolog/defaults.go')
-rw-r--r-- | vendor/go.mau.fi/util/exzerolog/defaults.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/vendor/go.mau.fi/util/exzerolog/defaults.go b/vendor/go.mau.fi/util/exzerolog/defaults.go new file mode 100644 index 0000000..c8c3c81 --- /dev/null +++ b/vendor/go.mau.fi/util/exzerolog/defaults.go @@ -0,0 +1,32 @@ +// Copyright (c) 2024 Tulir Asokan +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +package exzerolog + +import ( + "time" + + "github.com/rs/zerolog" + deflog "github.com/rs/zerolog/log" +) + +// SetupDefaults updates zerolog globals with sensible defaults. +// +// * [zerolog.TimeFieldFormat] is set to time.RFC3339Nano instead of time.RFC3339 +// * [zerolog.CallerMarshalFunc] is set to [CallerWithFunctionName] +// * [zerolog.DefaultContextLogger] is set to the given logger with default_context_log=true and caller info enabled +// * The global default [log.Logger] is set to the given logger with global_log=true and caller info enabled +// * [zerolog.LevelColors] are updated to swap trace and debug level colors +func SetupDefaults(log *zerolog.Logger) { + zerolog.TimeFieldFormat = time.RFC3339Nano + zerolog.CallerMarshalFunc = CallerWithFunctionName + defaultCtxLog := log.With().Bool("default_context_log", true).Caller().Logger() + zerolog.DefaultContextLogger = &defaultCtxLog + deflog.Logger = log.With().Bool("global_log", true).Caller().Logger() + // Swap trace and debug level colors so trace pops out the least + zerolog.LevelColors[zerolog.TraceLevel] = 0 + zerolog.LevelColors[zerolog.DebugLevel] = 34 // blue +} |