Documentation ¶
Overview ¶
Package zlog contains some very simple go-logr / zerologr helper functions. This sets the default timestamp format to time.RFC3339 with ms precision.
Package zlog contains some very simple go-logr / zerologr helper functions. This sets the default timestamp format to time.RFC3339 with ms precision.
Index ¶
- Constants
- Variables
- func IsTerminal(w io.Writer) bool
- func MaybeConsoleHandler(level slog.Leveler, w io.Writer) slog.Handler
- func NewContext(ctx context.Context, logger Logger) context.Context
- func NewSContext(ctx context.Context, logger *slog.Logger) context.Context
- func SFromContext(ctx context.Context) *slog.Logger
- func SetHandler(lgr Logger, h slog.Handler)
- func SetLevel(lgr Logger, level slog.Leveler)
- func SetOutput(lgr Logger, w io.Writer)
- type Color
- type ConsoleHandler
- type HandlerOptions
- type LevelHandler
- func (h *LevelHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (h *LevelHandler) GetLevel() slog.Leveler
- func (h *LevelHandler) Handle(ctx context.Context, r slog.Record) error
- func (h *LevelHandler) Handler() slog.Handler
- func (h *LevelHandler) SetLevel(level slog.Leveler)
- func (h *LevelHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *LevelHandler) WithGroup(name string) slog.Handler
- type Logger
- func (lgr Logger) Debug(msg string, args ...any)
- func (lgr Logger) DebugContext(ctx context.Context, msg string, args ...any)
- func (lgr Logger) Error(err error, msg string, args ...any)
- func (lgr Logger) ErrorContext(ctx context.Context, err error, msg string, args ...any)
- func (lgr Logger) Info(msg string, args ...any)
- func (lgr Logger) InfoContext(ctx context.Context, msg string, args ...any)
- func (lgr Logger) Log(keyvals ...interface{}) error
- func (lgr Logger) Logr() logr.Logger
- func (lgr Logger) SLog() *slog.Logger
- func (lgr Logger) SetHandler(h slog.Handler)
- func (lgr Logger) SetLevel(level slog.Leveler)
- func (lgr Logger) SetOutput(w io.Writer)
- func (lgr Logger) V(off int) Logger
- func (lgr Logger) Warn(msg string, args ...any)
- func (lgr Logger) WarnContext(ctx context.Context, msg string, args ...any)
- func (lgr Logger) WithGroup(s string) Logger
- func (lgr Logger) WithName(s string) Logger
- func (lgr Logger) WithValues(args ...any) Logger
- type LogrLevel
- type MultiHandler
- func (lw *MultiHandler) Add(w slog.Handler)
- func (lw *MultiHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (lw *MultiHandler) Handle(ctx context.Context, r slog.Record) error
- func (lw *MultiHandler) Swap(ws ...slog.Handler)
- func (lw *MultiHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (lw *MultiHandler) WithGroup(name string) slog.Handler
- type SLogSink
- func (ls SLogSink) Enabled(level int) bool
- func (ls SLogSink) Error(err error, msg string, keysAndValues ...interface{})
- func (ls SLogSink) Info(level int, msg string, keysAndValues ...interface{})
- func (ls SLogSink) Init(info logr.RuntimeInfo)
- func (ls SLogSink) WithName(name string) logr.LogSink
- func (ls SLogSink) WithValues(keysAndValues ...interface{}) logr.LogSink
- type SyncWriter
- type VerboseVar
Constants ¶
const ( TraceLevel = slog.LevelDebug - 1 DebugLevel = slog.LevelDebug InfoLevel = slog.LevelInfo ErrorLevel = slog.LevelError )
DebugLevel Level = -4 LevelInfo Level = 0 WarnLevel Level = 4 ErrorLevel Level = 8
const DefaultTimeFormat = "15:04:05.999"
DefaultTimeFormat is a "precise" KitchenTime.
Variables ¶
var DefaultConsoleHandlerOptions = HandlerOptions{}
DefaultConsoleHandlerOptions *does not* add the source.
var DefaultHandlerOptions = HandlerOptions{HandlerOptions: slog.HandlerOptions{ AddSource: true, ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { switch a.Key { case "time", "level", "source": return a default: if a.Value.Kind() == slog.KindAny { if ensurePrintableValueIsEmpty(&a.Value) { return zeroAttr } } } return a }}, }
DefaultHandlerOptions adds the source.
var ( // TimeFormat is the format used to print the time (padded with zeros if it is the DefaultTimeFormat). TimeFormat = DefaultTimeFormat )
Functions ¶
func IsTerminal ¶
IsTerminal returns whether the io.Writer is a terminal or not.
func MaybeConsoleHandler ¶
MaybeConsoleHandler returns an slog.JSONHandler if w is a terminal, and slog.TextHandler otherwise.
func NewContext ¶ added in v0.1.1
NewContext returns a new context with the given logger embedded.
func NewSContext ¶ added in v0.6.1
NewSContext returns a new context with the given logger embedded.
func SFromContext ¶ added in v0.6.1
SFromContext returns the Logger embedded into the Context, or the default logger otherwise.
func SetHandler ¶
SetHandler sets the handler on the given Logger.
Types ¶
type ConsoleHandler ¶ added in v0.2.0
type ConsoleHandler struct { HandlerOptions UseColor bool // contains filtered or unexported fields }
ConsoleHandler prints to the console
func NewConsoleHandler ¶ added in v0.2.0
func NewConsoleHandler(level slog.Leveler, w io.Writer) *ConsoleHandler
NewConsoleHandler returns a new ConsoleHandler which writes to w.
type HandlerOptions ¶ added in v0.5.3
type HandlerOptions struct{ slog.HandlerOptions }
HandlerOptions wraps slog.HandlerOptions, stripping source prefix.
func (HandlerOptions) NewJSONHandler ¶ added in v0.5.3
func (opts HandlerOptions) NewJSONHandler(w io.Writer) slog.Handler
type LevelHandler ¶
type LevelHandler struct {
// contains filtered or unexported fields
}
A LevelHandler wraps a Handler with an Enabled method that returns false for levels below a minimum.
func NewLevelHandler ¶
func NewLevelHandler(level slog.Leveler, h slog.Handler) *LevelHandler
NewLevelHandler returns a LevelHandler with the given level. All methods except Enabled delegate to h.
func (*LevelHandler) Enabled ¶
Enabled implements Handler.Enabled by reporting whether level is at least as large as h's level.
func (*LevelHandler) GetLevel ¶ added in v0.4.0
func (h *LevelHandler) GetLevel() slog.Leveler
func (*LevelHandler) Handler ¶
func (h *LevelHandler) Handler() slog.Handler
Handler returns the Handler wrapped by h.
func (*LevelHandler) SetLevel ¶
func (h *LevelHandler) SetLevel(level slog.Leveler)
SetLevel on the LevelHandler.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a helper type for logr.Logger -like slog.Logger.
func Discard ¶ added in v0.2.5
func Discard() Logger
Discard returns a Logger that does not log at all.
func FromContext ¶ added in v0.1.1
FromContext returns the Logger embedded into the Context, or the default logger otherwise.
func (Logger) DebugContext ¶ added in v0.7.0
DebugContext calls DebugContext if enabled.
func (Logger) ErrorContext ¶ added in v0.7.0
ErrorContext calls Error with ErrorLevel, always.
func (Logger) InfoContext ¶ added in v0.7.0
InfoContext calls InfoContext if enabled.
func (Logger) Logr ¶ added in v0.5.0
Logr returns a go-logr/logr.Logger, using this Logger as LogSink
func (Logger) SetHandler ¶
SetHandler sets the Handler.
func (Logger) WarnContext ¶ added in v0.7.0
WarnContext calls WarContext if enabled.
func (Logger) WithValues ¶
WithValues emulates logr.Logger.WithValues with slog.WithAttrs.
type LogrLevel ¶ added in v0.1.2
type LogrLevel int
LogrLevel is an slog.Leveler that converts from github.com/go-logr/logr levels to slog levels.
type MultiHandler ¶ added in v0.1.2
type MultiHandler struct {
// contains filtered or unexported fields
}
MultiHandler writes to all the specified handlers.
goroutine-safe.
func NewMultiHandler ¶
func NewMultiHandler(hs ...slog.Handler) *MultiHandler
NewMultiHandler returns a new slog.Handler that writes to all the specified Handlers.
func (*MultiHandler) Add ¶ added in v0.1.2
func (lw *MultiHandler) Add(w slog.Handler)
Add an additional writer to the targets.
func (*MultiHandler) Enabled ¶ added in v0.1.2
Enabled reports whether any of the underlying handlers is enabled for the given level.
func (*MultiHandler) Swap ¶ added in v0.1.2
func (lw *MultiHandler) Swap(ws ...slog.Handler)
Swap the current writers with the defined.
type SLogSink ¶ added in v0.1.3
SLogSink is an logr.LogSink for an slog.Logger.
func (SLogSink) Enabled ¶ added in v0.1.3
Enabled tests whether this LogSink is enabled at the specified V-level. For example, commandline flags might be used to set the logging verbosity and disable some info logs.
func (SLogSink) Error ¶ added in v0.1.3
Error logs an error, with the given message and key/value pairs as context. See Logger.Error for more details.
func (SLogSink) Info ¶ added in v0.1.3
Info logs a non-error message with the given key/value pairs as context. The level argument is provided for optional logging. This method will only be called when Enabled(level) is true. See Logger.Info for more details.
func (SLogSink) Init ¶ added in v0.1.3
func (ls SLogSink) Init(info logr.RuntimeInfo)
Init receives optional information about the logr library for LogSink implementations that need it.
func (SLogSink) WithName ¶ added in v0.1.3
WithName returns a new LogSink with the specified name appended. See Logger.WithName for more details.
func (SLogSink) WithValues ¶ added in v0.1.3
WithValues returns a new LogSink with additional key/value pairs. See Logger.WithValues for more details.
type SyncWriter ¶ added in v0.2.5
type SyncWriter struct {
// contains filtered or unexported fields
}
SyncWriter syncs each Write.
func NewSyncWriter ¶ added in v0.2.5
func NewSyncWriter(w io.Writer) *SyncWriter
NewSyncWriter returns an io.Writer that syncs each io.Write
type VerboseVar ¶ added in v0.4.0
type VerboseVar uint8
func (*VerboseVar) IsBoolFlag ¶ added in v0.4.0
func (vv *VerboseVar) IsBoolFlag() bool
func (*VerboseVar) Level ¶ added in v0.4.0
func (vv *VerboseVar) Level() slog.Level
func (*VerboseVar) Set ¶ added in v0.4.0
func (vv *VerboseVar) Set(s string) error
func (*VerboseVar) String ¶ added in v0.4.0
func (vv *VerboseVar) String() string