log

package
v0.0.0-...-c5cebdb Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2024 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LevelTrace slog.Level = -8
	LevelDebug            = slog.LevelDebug
	LevelInfo             = slog.LevelInfo
	LevelWarn             = slog.LevelWarn
	LevelError            = slog.LevelError
	LevelCrit  slog.Level = 12

	// for backward-compatibility
	LvlTrace = LevelTrace
	LvlInfo  = LevelInfo
	LvlDebug = LevelDebug
)

Variables

This section is empty.

Functions

func Crit

func Crit(msg string, ctx ...interface{})

Crit is a convenient alias for Root().Crit

Log a message at the crit level with context key/value pairs, and then exit.

Usage Examples

log.Crit("msg")
log.Crit("msg", "key1", val1)
log.Crit("msg", "key1", val1, "key2", val2)

func Debug

func Debug(msg string, ctx ...interface{})

Debug is a convenient alias for Root().Debug

Log a message at the debug level with context key/value pairs

Usage Examples

log.Debug("msg")
log.Debug("msg", "key1", val1)
log.Debug("msg", "key1", val1, "key2", val2)

func DiscardHandler

func DiscardHandler() slog.Handler

DiscardHandler returns a no-op handler

func Error

func Error(msg string, ctx ...interface{})

Error is a convenient alias for Root().Error

Log a message at the error level with context key/value pairs

Usage Examples

log.Error("msg")
log.Error("msg", "key1", val1)
log.Error("msg", "key1", val1, "key2", val2)

func FormatLogfmtUint64

func FormatLogfmtUint64(n uint64) string

FormatLogfmtUint64 formats n with thousand separators.

func FormatSlogValue

func FormatSlogValue(v slog.Value, tmp []byte) (result []byte)

FormatSlogValue formats a slog.Value for serialization to terminal.

func FromLegacyLevel

func FromLegacyLevel(lvl int) slog.Level

convert from old Geth verbosity level constants to levels defined by slog

func Info

func Info(msg string, ctx ...interface{})

Info is a convenient alias for Root().Info

Log a message at the info level with context key/value pairs

Usage Examples

log.Info("msg")
log.Info("msg", "key1", val1)
log.Info("msg", "key1", val1, "key2", val2)

func JSONHandler

func JSONHandler(wr io.Writer) slog.Handler

JSONHandler returns a handler which prints records in JSON format.

func LevelAlignedString

func LevelAlignedString(l slog.Level) string

LevelAlignedString returns a 5-character string containing the name of a Lvl.

func LevelString

func LevelString(l slog.Level) string

LevelString returns a string containing the name of a Lvl.

func LogfmtHandler

func LogfmtHandler(wr io.Writer) slog.Handler

LogfmtHandler returns a handler which prints records in logfmt format, an easy machine-parseable but human-readable format for key/value pairs.

For more details see: http://godoc.org/github.com/kr/logfmt

func LogfmtHandlerWithLevel

func LogfmtHandlerWithLevel(wr io.Writer, level slog.Level) slog.Handler

LogfmtHandlerWithLevel returns the same handler as LogfmtHandler but it only outputs records which are less than or equal to the specified verbosity level.

func SetDefault

func SetDefault(l Logger)

SetDefault sets the default global logger

func Trace

func Trace(msg string, ctx ...interface{})

Trace is a convenient alias for Root().Trace

Log a message at the trace level with context key/value pairs

Usage

log.Trace("msg")
log.Trace("msg", "key1", val1)
log.Trace("msg", "key1", val1, "key2", val2)

func Warn

func Warn(msg string, ctx ...interface{})

Warn is a convenient alias for Root().Warn

Log a message at the warn level with context key/value pairs

Usage Examples

log.Warn("msg")
log.Warn("msg", "key1", val1)
log.Warn("msg", "key1", val1, "key2", val2)

Types

type GlogHandler

type GlogHandler struct {
	// contains filtered or unexported fields
}

GlogHandler is a log handler that mimics the filtering features of Google's glog logger: setting global log levels; overriding with callsite pattern matches; and requesting backtraces at certain positions.

func NewGlogHandler

func NewGlogHandler(h slog.Handler) *GlogHandler

NewGlogHandler creates a new log handler with filtering functionality similar to Google's glog logger. The returned handler implements Handler.

func (*GlogHandler) Enabled

func (h *GlogHandler) Enabled(ctx context.Context, lvl slog.Level) bool

func (*GlogHandler) Handle

func (h *GlogHandler) Handle(_ context.Context, r slog.Record) error

Log implements Handler.Log, filtering a log record through the global, local and backtrace filters, finally emitting it if either allow it through.

func (*GlogHandler) Verbosity

func (h *GlogHandler) Verbosity(level slog.Level)

Verbosity sets the glog verbosity ceiling. The verbosity of individual packages and source files can be raised using Vmodule.

func (*GlogHandler) Vmodule

func (h *GlogHandler) Vmodule(ruleset string) error

Vmodule sets the glog verbosity pattern.

The syntax of the argument is a comma-separated list of pattern=N, where the pattern is a literal file name or "glob" pattern matching and N is a V level.

For instance:

pattern="gopher.go=3"
 sets the V level to 3 in all Go files named "gopher.go"

pattern="foo=3"
 sets V to 3 in all files of any packages whose import path ends in "foo"

pattern="foo/*=3"
 sets V to 3 in all files of any packages whose import path contains "foo"

func (*GlogHandler) WithAttrs

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

func (*GlogHandler) WithGroup

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

type Logger

type Logger interface {
	// With returns a new Logger that has this logger's attributes plus the given attributes
	With(ctx ...interface{}) Logger

	// With returns a new Logger that has this logger's attributes plus the given attributes. Identical to 'With'.
	New(ctx ...interface{}) Logger

	// Log logs a message at the specified level with context key/value pairs
	Log(level slog.Level, msg string, ctx ...interface{})

	// Trace log a message at the trace level with context key/value pairs
	Trace(msg string, ctx ...interface{})

	// Debug logs a message at the debug level with context key/value pairs
	Debug(msg string, ctx ...interface{})

	// Info logs a message at the info level with context key/value pairs
	Info(msg string, ctx ...interface{})

	// Warn logs a message at the warn level with context key/value pairs
	Warn(msg string, ctx ...interface{})

	// Error logs a message at the error level with context key/value pairs
	Error(msg string, ctx ...interface{})

	// Crit logs a message at the crit level with context key/value pairs, and exits
	Crit(msg string, ctx ...interface{})

	// Write logs a message at the specified level
	Write(level slog.Level, msg string, attrs ...any)

	// Enabled reports whether l emits log records at the given context and level.
	Enabled(ctx context.Context, level slog.Level) bool

	// Handler returns the underlying handler of the inner logger.
	Handler() slog.Handler
}

A Logger writes key/value pairs to a Handler

func New

func New(ctx ...interface{}) Logger

New returns a new logger with the given context. New is a convenient alias for Root().New

func NewLogger

func NewLogger(h slog.Handler) Logger

NewLogger returns a logger with the specified handler set

func Root

func Root() Logger

Root returns the root logger

type TerminalHandler

type TerminalHandler struct {
	// contains filtered or unexported fields
}

func NewTerminalHandler

func NewTerminalHandler(wr io.Writer, useColor bool) *TerminalHandler

NewTerminalHandler returns a handler which formats log records at all levels optimized for human readability on a terminal with color-coded level output and terser human friendly timestamp. This format should only be used for interactive programs or while developing.

[LEVEL] [TIME] MESSAGE key=value key=value ...

Example:

[DBUG] [May 16 20:58:45] remove route ns=haproxy addr=127.0.0.1:50002

func NewTerminalHandlerWithLevel

func NewTerminalHandlerWithLevel(wr io.Writer, lvl slog.Level, useColor bool) *TerminalHandler

NewTerminalHandlerWithLevel returns the same handler as NewTerminalHandler but only outputs records which are less than or equal to the specified verbosity level.

func (*TerminalHandler) Enabled

func (h *TerminalHandler) Enabled(_ context.Context, level slog.Level) bool

func (*TerminalHandler) Handle

func (h *TerminalHandler) Handle(_ context.Context, r slog.Record) error

func (*TerminalHandler) ResetFieldPadding

func (t *TerminalHandler) ResetFieldPadding()

ResetFieldPadding zeroes the field-padding for all attribute pairs.

func (*TerminalHandler) WithAttrs

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

func (*TerminalHandler) WithGroup

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

type TerminalStringer

type TerminalStringer interface {
	TerminalString() string
}

TerminalStringer is an analogous interface to the stdlib stringer, allowing own types to have custom shortened serialization formats when printed to the screen.

Jump to

Keyboard shortcuts

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