log

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2025 License: BSD-3-Clause Imports: 17 Imported by: 453

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
	LevelFatal slog.Level = 16  // Added for Fatal
	LevelVerbo slog.Level = -10 // Added for Verbo (most verbose)
)

Re-export slog levels for compatibility

View Source
const (
	// DebugLevel logs debug messages.
	DebugLevel = LevelDebug

	// InfoLevel logs informational messages.
	InfoLevel = LevelInfo

	// WarnLevel logs warning messages.
	WarnLevel = LevelWarn

	// ErrorLevel logs error messages.
	ErrorLevel = LevelError

	// FatalLevel logs critical messages and exits.
	FatalLevel = LevelCrit
)

Level aliases for convenience.

Variables

This section is empty.

Functions

func Crit

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

func Debug

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

func DiscardHandler

func DiscardHandler() slog.Handler

DiscardHandler returns a no-op handler

func Error

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

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

FromLegacyLevel converts from old Geth verbosity level constants to levels defined by slog

func Info

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

func JSONHandler

func JSONHandler(wr io.Writer) slog.Handler

JSONHandler returns a handler which prints records in JSON format.

func JSONHandlerWithLevel

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

JSONHandlerWithLevel returns a handler which prints records in JSON format that are less than or equal to the specified verbosity level.

func LevelAlignedString

func LevelAlignedString(l slog.Level) string

LevelAlignedString returns a 5-character aligned string for the level

func LevelString

func LevelString(l slog.Level) string

LevelString returns a string representation of the level

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 root logger

func Trace

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

Global convenience functions that use the root logger

func Warn

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

func WriterAt

func WriterAt(logger Logger, level slog.Level) io.Writer

WriterAt returns an io.Writer that writes to the logger at the specified level

Types

type Factory

type Factory interface {
	New(name string) Logger
	NewWithFields(name string, fields ...zap.Field) Logger
}

Factory interface for creating loggers

func NewFactory

func NewFactory(config zap.Config) Factory

NewFactory creates a new logger factory

type Field

type Field struct {
	Key   string
	Value any
}

Field is a key/value pair for structured logging.

type GlogHandler

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

GlogHandler wraps a slog.Handler to provide glog-style verbosity and vmodule filtering.

func NewGlogHandler

func NewGlogHandler(h slog.Handler) *GlogHandler

NewGlogHandler returns a Handler that filters records according to glog-style severity and verbosity settings. By default, it logs messages with level >= LevelInfo and suppresses verbose logs (levels < LevelInfo) until Verbosity is called.

func (*GlogHandler) Enabled

func (g *GlogHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled reports whether records at the given level should be logged or passed to Handle for further verbose filtering.

func (*GlogHandler) Handle

func (g *GlogHandler) Handle(ctx context.Context, r slog.Record) error

Handle filters the record according to severity and verbosity (including vmodule) and forwards it to the underlying handler if allowed.

func (*GlogHandler) Verbosity

func (g *GlogHandler) Verbosity(v slog.Level)

Verbosity sets both the minimum severity and default verbosity levels. Messages with level >= v will be logged as severity; messages with level < LevelInfo will be logged if their level <= v.

func (*GlogHandler) Vmodule

func (g *GlogHandler) Vmodule(spec string) error

Vmodule sets a per-file verbosity level according to the spec string of the form "pattern=level". Multiple specs may be comma-separated.

func (*GlogHandler) WithAttrs

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

WithAttrs returns a new glogHandler with the given attributes added to the underlying handler.

func (*GlogHandler) WithGroup

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

WithGroup returns a new glogHandler with the given group added to the underlying handler.

type Level

type Level = slog.Level

Level is the type for log levels

type Logger

type Logger interface {
	// Original geth-style methods
	With(ctx ...interface{}) Logger
	New(ctx ...interface{}) Logger
	Log(level slog.Level, msg string, ctx ...interface{})
	Trace(msg string, ctx ...interface{})
	Debug(msg string, ctx ...interface{})
	Info(msg string, ctx ...interface{})
	Warn(msg string, ctx ...interface{})
	Error(msg string, ctx ...interface{})
	Crit(msg string, ctx ...interface{})
	WriteLog(level slog.Level, msg string, attrs ...any)
	Enabled(ctx context.Context, level slog.Level) bool
	Handler() slog.Handler

	// Additional methods for node compatibility
	Fatal(msg string, fields ...zap.Field)
	Verbo(msg string, fields ...zap.Field)
	WithFields(fields ...zap.Field) Logger
	WithOptions(opts ...zap.Option) Logger
	SetLevel(level slog.Level)
	GetLevel() slog.Level
	EnabledLevel(lvl slog.Level) bool
	StopOnPanic()
	RecoverAndPanic(f func())
	RecoverAndExit(f, exit func())
	Stop()

	// io.Writer
	io.Writer
}

Logger interface that supports both the geth-style interface and zap fields

func New

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

New creates a new logger with the given context

func NewLogger

func NewLogger(h slog.Handler) Logger

NewLogger creates a new zap-backed logger

func NewNoOpLogger

func NewNoOpLogger() Logger

NewNoOpLogger creates a logger that discards all output

func NewZapLogger

func NewZapLogger(logger *zap.Logger) Logger

NewZapLogger creates a logger directly from a zap logger

func Root

func Root() Logger

Root returns the root logger

type LoggerWriter

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

LoggerWriter wraps a Logger to provide io.Writer interface

func (*LoggerWriter) Write

func (w *LoggerWriter) Write(p []byte) (n int, err error)

Write implements io.Writer

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 (h *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