log

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2025 License: BSD-3-Clause Imports: 23 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

View Source
var Discard io.WriteCloser = discard{}

Discard is a writer that discards all data

View Source
var ErrUnknownLevel = level.ErrUnknownLevel

Re-export level error

Functions

func ConsoleColorLevelEncoder added in v0.1.1

func ConsoleColorLevelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder)

func Crit

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

Crit logs a message at critical level with context

func Crit0 added in v0.1.1

func Crit0(msg string)

Crit0 logs a message at critical level

func CritF added in v0.1.1

func CritF(msg string, fields ...zap.Field)

CritF logs a message at critical level with zap fields

func Debug

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

Debug logs a message at debug level with context

func Debug0 added in v0.1.1

func Debug0(msg string)

Debug0 logs a message at debug level

func DebugF added in v0.1.1

func DebugF(msg string, fields ...zap.Field)

DebugF logs a message at debug level with zap fields

func DiscardHandler

func DiscardHandler() slog.Handler

DiscardHandler returns a no-op handler

func Error

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

Error logs a message at error level with context

func Error0 added in v0.1.1

func Error0(msg string)

Error0 logs a message at error level

func ErrorF added in v0.1.1

func ErrorF(msg string, fields ...zap.Field)

ErrorF logs a message at error level with zap fields

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{})

Info logs a message at info level with context

func Info0 added in v0.1.1

func Info0(msg string)

Info0 logs a message at info level

func InfoF added in v0.1.1

func InfoF(msg string, fields ...zap.Field)

InfoF logs a message at info level with zap fields

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 SetGlobalLogger added in v0.1.1

func SetGlobalLogger(l Logger)

SetGlobalLogger sets the global logger used by package-level functions

func Stringer added in v0.1.1

func Stringer(key string, val interface{}) zap.Field

Stringer creates a zap field for fmt.Stringer objects

func Trace

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

Trace logs a message at trace level with context

func Trace0 added in v0.1.1

func Trace0(msg string)

Trace0 logs a message at trace level

func TraceF added in v0.1.1

func TraceF(msg string, fields ...zap.Field)

TraceF logs a message at trace level with zap fields

func UserString added in v0.1.1

func UserString(key, val string) zap.Field

UserString creates a zap field for user-provided strings that may need sanitization

func UserStrings added in v0.1.1

func UserStrings(key string, vals []string) zap.Field

UserStrings creates a zap field for user-provided string slices that may need sanitization

func Warn

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

Warn logs a message at warn level with context

func Warn0 added in v0.1.1

func Warn0(msg string)

Warn0 logs a message at warn level

func WarnF added in v0.1.1

func WarnF(msg string, fields ...zap.Field)

WarnF logs a message at warn level with zap fields

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 Color added in v0.1.1

type Color string

Color represents ANSI color codes

const (
	Black       Color = "\033[0;30m"
	DarkGray    Color = "\033[1;30m"
	Red         Color = "\033[0;31m"
	LightRed    Color = "\033[1;31m"
	Green       Color = "\033[0;32m"
	LightGreen  Color = "\033[1;32m"
	Orange      Color = "\033[0;33m"
	Yellow      Color = "\033[1;33m"
	Blue        Color = "\033[0;34m"
	LightBlue   Color = "\033[1;34m"
	Purple      Color = "\033[0;35m"
	LightPurple Color = "\033[1;35m"
	Cyan        Color = "\033[0;36m"
	LightCyan   Color = "\033[1;36m"
	LightGray   Color = "\033[0;37m"
	White       Color = "\033[1;37m"

	Reset   Color = "\033[0;0m"
	Bold    Color = "\033[;1m"
	Reverse Color = "\033[;7m"
)

Colors

func (Color) Wrap added in v0.1.1

func (lc Color) Wrap(text string) string

Wrap wraps text with color

type Config added in v0.1.1

type Config struct {
	RotatingWriterConfig
	DisableWriterDisplaying bool   `json:"disableWriterDisplaying"`
	LogLevel                Level  `json:"logLevel"`
	DisplayLevel            Level  `json:"displayLevel"`
	LogFormat               Format `json:"logFormat"`
	MsgPrefix               string `json:"-"`
	LoggerName              string `json:"-"`
}

Config defines the configuration of a logger

func DefaultConfig added in v0.1.1

func DefaultConfig() Config

DefaultConfig returns a default configuration for the logger factory

type Factory

type Factory interface {
	// Make creates a new logger with name [name]
	Make(name string) (Logger, error)

	// MakeChain creates a new logger to log the events of chain [chainID]
	MakeChain(chainID string) (Logger, error)

	// SetLogLevel sets log levels for all loggers in factory with given logger name, level pairs.
	SetLogLevel(name string, level Level) error

	// SetDisplayLevel sets log display levels for all loggers in factory with given logger name, level pairs.
	SetDisplayLevel(name string, level Level) error

	// GetLogLevel returns all log levels in factory as name, level pairs
	GetLogLevel(name string) (Level, error)

	// GetDisplayLevel returns all log display levels in factory as name, level pairs
	GetDisplayLevel(name string) (Level, error)

	// GetLoggerNames returns the names of all logs created by this factory
	GetLoggerNames() []string

	// Close stops and clears all of a Factory's instantiated loggers
	Close()

	// Legacy methods for compatibility
	New(name string) Logger
	NewWithFields(name string, fields ...zap.Field) Logger
}

Factory interface for creating loggers - extended version with all methods needed by node

func NewFactoryWithConfig added in v0.1.1

func NewFactoryWithConfig(config Config) Factory

NewFactory creates a new logger factory with config

func NewSimpleFactory added in v0.1.1

func NewSimpleFactory(config zap.Config) Factory

NewSimpleFactory creates a simple logger factory from zap config This is a convenience function for simple use cases

type Field

type Field struct {
	Key   string
	Value any
}

Field is a key/value pair for structured logging.

type Format added in v0.1.1

type Format int

Format specifies the log format

const (
	Auto Format = iota
	Plain
	Colors
	JSON
)

func ToFormat added in v0.1.1

func ToFormat(h string, fd uintptr) (Format, error)

ToFormat converts a string to Format

func (Format) ConsoleEncoder added in v0.1.1

func (f Format) ConsoleEncoder() zapcore.Encoder

ConsoleEncoder returns a zapcore.Encoder for console output

func (Format) FileEncoder added in v0.1.1

func (f Format) FileEncoder() zapcore.Encoder

FileEncoder returns a zapcore.Encoder for file output

func (Format) MarshalJSON added in v0.1.1

func (f Format) MarshalJSON() ([]byte, error)

MarshalJSON marshals Format to JSON

func (Format) WrapPrefix added in v0.1.1

func (f Format) WrapPrefix(prefix string) string

WrapPrefix adds a prefix to messages if non-empty

type Func added in v0.1.1

type Func func(msg string, fields ...zap.Field)

Func defines the method signature used for all logging methods on the Logger interface

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 = level.Level

Level is an alias for level.Level for backwards compatibility

func ToLevel added in v0.1.1

func ToLevel(l string) (Level, error)

Re-export ToLevel for backwards compatibility

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 (alias for With)

func NewLogger

func NewLogger(prefix string, wrappedCores ...WrappedCore) Logger

NewLogger creates a logger with custom cores

func NewNoOpLogger

func NewNoOpLogger() Logger

NewNoOpLogger creates a logger that discards all output

func NewTestLogger added in v0.1.1

func NewTestLogger(lvl Level) Logger

NewTestLogger creates a logger suitable for testing

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

func With added in v0.1.1

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

With creates a new logger with the given context

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 RotatingWriterConfig added in v0.1.1

type RotatingWriterConfig struct {
	MaxSize   int    `json:"maxSize"` // in megabytes
	MaxFiles  int    `json:"maxFiles"`
	MaxAge    int    `json:"maxAge"` // in days
	Directory string `json:"directory"`
	Compress  bool   `json:"compress"`
}

RotatingWriterConfig defines rotating log file configuration

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.

type WrappedCore added in v0.1.1

type WrappedCore struct {
	zapcore.Core
	AtomicLevel    zap.AtomicLevel
	Writer         zapcore.WriteSyncer
	WriterDisabled bool
}

WrappedCore wraps a zapcore.Core with additional functionality

func NewWrappedCore added in v0.1.1

func NewWrappedCore(lvl Level, writer zapcore.WriteSyncer, encoder zapcore.Encoder) *WrappedCore

NewWrappedCore creates a wrapped core with atomic level

func (*WrappedCore) Check added in v0.1.1

Check implements zapcore.Core

func (*WrappedCore) Enabled added in v0.1.1

func (c *WrappedCore) Enabled(level zapcore.Level) bool

Enabled implements zapcore.Core

func (*WrappedCore) Sync added in v0.1.1

func (c *WrappedCore) Sync() error

Sync implements zapcore.Core

func (*WrappedCore) With added in v0.1.1

func (c *WrappedCore) With(fields []zapcore.Field) zapcore.Core

With implements zapcore.Core

func (*WrappedCore) Write added in v0.1.1

func (c *WrappedCore) Write(ent zapcore.Entry, fields []zapcore.Field) error

Write implements zapcore.Core

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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