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 | |
parent | 98bbb0f559a8883bc47bae80607dbe326a448e61 (diff) |
Diffstat (limited to 'vendor/go.mau.fi/util/exzerolog')
-rw-r--r-- | vendor/go.mau.fi/util/exzerolog/callermarshal.go | 28 | ||||
-rw-r--r-- | vendor/go.mau.fi/util/exzerolog/defaults.go | 32 | ||||
-rw-r--r-- | vendor/go.mau.fi/util/exzerolog/generics.go | 45 | ||||
-rw-r--r-- | vendor/go.mau.fi/util/exzerolog/writer.go | 81 |
4 files changed, 186 insertions, 0 deletions
diff --git a/vendor/go.mau.fi/util/exzerolog/callermarshal.go b/vendor/go.mau.fi/util/exzerolog/callermarshal.go new file mode 100644 index 0000000..938a5e0 --- /dev/null +++ b/vendor/go.mau.fi/util/exzerolog/callermarshal.go @@ -0,0 +1,28 @@ +// Copyright (c) 2023 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 ( + "fmt" + "runtime" + "strings" +) + +// CallerWithFunctionName is an implementation for zerolog.CallerMarshalFunc that includes the caller function name +// in addition to the file and line number. +// +// Use as +// +// zerolog.CallerMarshalFunc = exzerolog.CallerWithFunctionName +func CallerWithFunctionName(pc uintptr, file string, line int) string { + files := strings.Split(file, "/") + file = files[len(files)-1] + name := runtime.FuncForPC(pc).Name() + fns := strings.Split(name, ".") + name = fns[len(fns)-1] + return fmt.Sprintf("%s:%d:%s()", file, line, name) +} 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 +} diff --git a/vendor/go.mau.fi/util/exzerolog/generics.go b/vendor/go.mau.fi/util/exzerolog/generics.go new file mode 100644 index 0000000..ca1910b --- /dev/null +++ b/vendor/go.mau.fi/util/exzerolog/generics.go @@ -0,0 +1,45 @@ +// Copyright (c) 2023 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 ( + "fmt" + + "github.com/rs/zerolog" +) + +func ArrayOf[T any](slice []T, fn func(arr *zerolog.Array, item T)) *zerolog.Array { + arr := zerolog.Arr() + for _, item := range slice { + fn(arr, item) + } + return arr +} + +func AddObject[T zerolog.LogObjectMarshaler](arr *zerolog.Array, obj T) { + arr.Object(obj) +} + +func AddStringer[T fmt.Stringer](arr *zerolog.Array, str T) { + arr.Str(str.String()) +} + +func AddStr[T ~string](arr *zerolog.Array, str T) { + arr.Str(string(str)) +} + +func ArrayOfObjs[T zerolog.LogObjectMarshaler](slice []T) *zerolog.Array { + return ArrayOf(slice, AddObject[T]) +} + +func ArrayOfStringers[T fmt.Stringer](slice []T) *zerolog.Array { + return ArrayOf(slice, AddStringer[T]) +} + +func ArrayOfStrs[T ~string](slice []T) *zerolog.Array { + return ArrayOf(slice, AddStr[T]) +} diff --git a/vendor/go.mau.fi/util/exzerolog/writer.go b/vendor/go.mau.fi/util/exzerolog/writer.go new file mode 100644 index 0000000..c570985 --- /dev/null +++ b/vendor/go.mau.fi/util/exzerolog/writer.go @@ -0,0 +1,81 @@ +// Copyright (c) 2023 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 ( + "bytes" + "sync" + + "github.com/rs/zerolog" +) + +// LogWriter wraps a zerolog.Logger and provides an io.Writer with buffering so each written line is logged separately. +type LogWriter struct { + log zerolog.Logger + level zerolog.Level + field string + mu sync.Mutex + buf bytes.Buffer +} + +func NewLogWriter(log zerolog.Logger) *LogWriter { + zerolog.Nop() + return &LogWriter{ + log: log, + level: zerolog.DebugLevel, + field: zerolog.MessageFieldName, + } +} + +func (lw *LogWriter) WithLevel(level zerolog.Level) *LogWriter { + return &LogWriter{ + log: lw.log, + level: level, + field: lw.field, + } +} + +func (lw *LogWriter) WithField(field string) *LogWriter { + return &LogWriter{ + log: lw.log, + level: lw.level, + field: field, + } +} + +func (lw *LogWriter) writeLine(data []byte) { + if len(data) == 0 { + return + } + if data[len(data)-1] == '\n' { + data = data[:len(data)-1] + } + if lw.buf.Len() > 0 { + lw.buf.Write(data) + data = lw.buf.Bytes() + lw.buf.Reset() + } + lw.log.WithLevel(lw.level).Bytes(lw.field, data).Send() +} + +func (lw *LogWriter) Write(data []byte) (n int, err error) { + lw.mu.Lock() + defer lw.mu.Unlock() + newline := bytes.IndexByte(data, '\n') + if newline == len(data)-1 { + lw.writeLine(data) + } else if newline < 0 { + lw.buf.Write(data) + } else { + lines := bytes.Split(data, []byte{'\n'}) + for _, line := range lines[:len(lines)-1] { + lw.writeLine(line) + } + lw.buf.Write(lines[len(lines)-1]) + } + return len(data), nil +} |