zlog

package
v0.8.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2024 License: Apache-2.0 Imports: 18 Imported by: 24

README

logr -> zlog

sed -i -e '/logr/ { s,go-logr/logr,UNO-SOFT/zlog/v2,; s/logr.Logger/*slog.Logger/; s/logr.FromContext\(OrDiscard\)*/zlog.SFromContext/; s/logr.NewContext/zlog.NewSContext/; }; /logger\.Error([^"]/ s/logger\.Error(\([^,]*\),\(.*\))$/logger.Error(\2, "error", \1)/; /logger\.V/ s/logger\.V([0-9][0-9]*)\.Info/logger.Debug/; /logger\.With/ { s/WithName/WithGroup/; s/WithValues/With/; }' $(fgrep -l logr $(find . -type f -name '*.go'))

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

View Source
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

View Source
const DefaultTimeFormat = "15:04:05.999"

DefaultTimeFormat is a "precise" KitchenTime.

Variables

View Source
var DefaultConsoleHandlerOptions = HandlerOptions{}

DefaultConsoleHandlerOptions *does not* add the source.

View 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.

View Source
var (
	// TimeFormat is the format used to print the time (padded with zeros if it is the DefaultTimeFormat).
	TimeFormat = DefaultTimeFormat
)

Functions

func IsTerminal

func IsTerminal(w io.Writer) bool

IsTerminal returns whether the io.Writer is a terminal or not.

func MaybeConsoleHandler

func MaybeConsoleHandler(level slog.Leveler, w io.Writer) slog.Handler

MaybeConsoleHandler returns an slog.JSONHandler if w is a terminal, and slog.TextHandler otherwise.

func NewContext added in v0.1.1

func NewContext(ctx context.Context, logger Logger) context.Context

NewContext returns a new context with the given logger embedded.

func NewSContext added in v0.6.1

func NewSContext(ctx context.Context, logger *slog.Logger) context.Context

NewSContext returns a new context with the given logger embedded.

func SFromContext added in v0.6.1

func SFromContext(ctx context.Context) *slog.Logger

SFromContext returns the Logger embedded into the Context, or the default logger otherwise.

func SetHandler

func SetHandler(lgr Logger, h slog.Handler)

SetHandler sets the handler on the given Logger.

func SetLevel

func SetLevel(lgr Logger, level slog.Leveler)

SetLevel sets the level on the given Logger.

func SetOutput

func SetOutput(lgr Logger, w io.Writer)

SetOutput sets the output on the given Logger.

Types

type Color added in v0.2.0

type Color uint8

Color is a color.

const (
	Black Color = iota + 30
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
)

Colors

func (Color) Add added in v0.2.0

func (c Color) Add(s string) string

Add adds the coloring to the given string.

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.

func (*ConsoleHandler) Enabled added in v0.2.0

func (h *ConsoleHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled implements slog.Handler.Enabled.

func (*ConsoleHandler) Handle added in v0.2.0

func (h *ConsoleHandler) Handle(ctx context.Context, r slog.Record) error

Handle implements slog.Handler.Handle.

func (*ConsoleHandler) WithAttrs added in v0.2.0

func (h *ConsoleHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs implements slog.Handler.WithAttrs.

func (*ConsoleHandler) WithGroup added in v0.2.0

func (h *ConsoleHandler) WithGroup(name string) slog.Handler

WithGroup implements slog.Handler.WithGroup.

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

func (h *LevelHandler) Enabled(ctx context.Context, level slog.Level) bool

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) Handle

func (h *LevelHandler) Handle(ctx context.Context, r slog.Record) error

Handle implements Handler.Handle.

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.

func (*LevelHandler) WithAttrs

func (h *LevelHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs implements Handler.WithAttrs.

func (*LevelHandler) WithGroup

func (h *LevelHandler) WithGroup(name string) slog.Handler

WithGroup implements Handler.WithGroup.

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

func FromContext(ctx context.Context) Logger

FromContext returns the Logger embedded into the Context, or the default logger otherwise.

func New

func New(w io.Writer) Logger

New returns a new logr.Logger writing to w as a zerolog.Logger, at LevelInfo.

func NewLogger

func NewLogger(h slog.Handler) Logger

NewLogger returns a new Logger writing to w.

func NewT added in v0.2.3

func NewT(t testing.TB) Logger

NewT return a new text writer for a testing.T

func (Logger) Debug added in v0.5.0

func (lgr Logger) Debug(msg string, args ...any)

Debug calls Debug if enabled.

func (Logger) DebugContext added in v0.7.0

func (lgr Logger) DebugContext(ctx context.Context, msg string, args ...any)

DebugContext calls DebugContext if enabled.

func (Logger) Error

func (lgr Logger) Error(err error, msg string, args ...any)

Error calls Error with ErrorLevel, always.

func (Logger) ErrorContext added in v0.7.0

func (lgr Logger) ErrorContext(ctx context.Context, err error, msg string, args ...any)

ErrorContext calls Error with ErrorLevel, always.

func (Logger) Info

func (lgr Logger) Info(msg string, args ...any)

Info calls Info if enabled.

func (Logger) InfoContext added in v0.7.0

func (lgr Logger) InfoContext(ctx context.Context, msg string, args ...any)

InfoContext calls InfoContext if enabled.

func (Logger) Log added in v0.1.1

func (lgr Logger) Log(keyvals ...interface{}) error

Log emulates go-kit/log.

func (Logger) Logr added in v0.5.0

func (lgr Logger) Logr() logr.Logger

Logr returns a go-logr/logr.Logger, using this Logger as LogSink

func (Logger) SLog added in v0.1.3

func (lgr Logger) SLog() *slog.Logger

SLog returns the underlying slog.Logger

func (Logger) SetHandler

func (lgr Logger) SetHandler(h slog.Handler)

SetHandler sets the Handler.

func (Logger) SetLevel

func (lgr Logger) SetLevel(level slog.Leveler)

SetLevel on the underlying LevelHandler.

func (Logger) SetOutput

func (lgr Logger) SetOutput(w io.Writer)

SetOutput sets the output to a new Logger.

func (Logger) V

func (lgr Logger) V(off int) Logger

V offsets the logging levels by off (emulates logr.Logger.V).

func (Logger) Warn added in v0.5.0

func (lgr Logger) Warn(msg string, args ...any)

Warn calls Warn if enabled.

func (Logger) WarnContext added in v0.7.0

func (lgr Logger) WarnContext(ctx context.Context, msg string, args ...any)

WarnContext calls WarContext if enabled.

func (Logger) WithGroup added in v0.1.3

func (lgr Logger) WithGroup(s string) Logger

WithGroup is slog.WithGroup

func (Logger) WithName added in v0.1.3

func (lgr Logger) WithName(s string) Logger

WithName implements logr.WithName with slog.WithGroup

func (Logger) WithValues

func (lgr Logger) WithValues(args ...any) Logger

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.

func (LogrLevel) Level added in v0.1.2

func (l LogrLevel) Level() slog.Level

Level returns the slog.Level, converted from the logr level.

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

func (lw *MultiHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled reports whether any of the underlying handlers is enabled for the given level.

func (*MultiHandler) Handle added in v0.1.2

func (lw *MultiHandler) Handle(ctx context.Context, r slog.Record) error

Handle the record.

func (*MultiHandler) Swap added in v0.1.2

func (lw *MultiHandler) Swap(ws ...slog.Handler)

Swap the current writers with the defined.

func (*MultiHandler) WithAttrs added in v0.1.2

func (lw *MultiHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new slog.Handler with the given attrs set on all underlying handlers.

func (*MultiHandler) WithGroup added in v0.1.2

func (lw *MultiHandler) WithGroup(name string) slog.Handler

WithGroup returns a new slog.Handler with the given group set on all underlying handlers.

type SLogSink added in v0.1.3

type SLogSink struct{ *slog.Logger }

SLogSink is an logr.LogSink for an slog.Logger.

func (SLogSink) Enabled added in v0.1.3

func (ls SLogSink) Enabled(level int) bool

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

func (ls SLogSink) Error(err error, msg string, keysAndValues ...interface{})

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

func (ls SLogSink) Info(level int, msg string, keysAndValues ...interface{})

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

func (ls SLogSink) WithName(name string) logr.LogSink

WithName returns a new LogSink with the specified name appended. See Logger.WithName for more details.

func (SLogSink) WithValues added in v0.1.3

func (ls SLogSink) WithValues(keysAndValues ...interface{}) logr.LogSink

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

func (*SyncWriter) Write added in v0.2.5

func (sw *SyncWriter) Write(p []byte) (int, error)

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL