2017-02-20 18:39:36 +03:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2023-11-29 10:33:50 +03:00
|
|
|
"context"
|
2024-03-22 15:17:59 +03:00
|
|
|
"log/slog"
|
2023-11-29 10:33:50 +03:00
|
|
|
"math"
|
2017-02-20 19:05:15 +03:00
|
|
|
"os"
|
2023-11-29 10:33:50 +03:00
|
|
|
"runtime"
|
2017-02-20 18:39:36 +03:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
const errorKey = "LOG_ERROR"
|
2017-02-20 18:39:36 +03:00
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
const (
|
|
|
|
legacyLevelCrit = iota
|
|
|
|
legacyLevelError
|
|
|
|
legacyLevelWarn
|
|
|
|
legacyLevelInfo
|
|
|
|
legacyLevelDebug
|
|
|
|
legacyLevelTrace
|
|
|
|
)
|
2017-02-20 18:39:36 +03:00
|
|
|
|
|
|
|
const (
|
2023-11-29 10:33:50 +03:00
|
|
|
levelMaxVerbosity slog.Level = math.MinInt
|
|
|
|
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
|
2017-02-20 18:39:36 +03:00
|
|
|
)
|
|
|
|
|
2024-06-17 12:03:27 +03:00
|
|
|
// FromLegacyLevel converts from old Geth verbosity level constants
|
2023-11-29 10:33:50 +03:00
|
|
|
// to levels defined by slog
|
|
|
|
func FromLegacyLevel(lvl int) slog.Level {
|
|
|
|
switch lvl {
|
|
|
|
case legacyLevelCrit:
|
|
|
|
return LevelCrit
|
|
|
|
case legacyLevelError:
|
|
|
|
return slog.LevelError
|
|
|
|
case legacyLevelWarn:
|
|
|
|
return slog.LevelWarn
|
|
|
|
case legacyLevelInfo:
|
|
|
|
return slog.LevelInfo
|
|
|
|
case legacyLevelDebug:
|
|
|
|
return slog.LevelDebug
|
|
|
|
case legacyLevelTrace:
|
|
|
|
return LevelTrace
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: should we allow use of custom levels or force them to match existing max/min if they fall outside the range as I am doing here?
|
|
|
|
if lvl > legacyLevelTrace {
|
|
|
|
return LevelTrace
|
|
|
|
}
|
|
|
|
return LevelCrit
|
|
|
|
}
|
|
|
|
|
|
|
|
// LevelAlignedString returns a 5-character string containing the name of a Lvl.
|
|
|
|
func LevelAlignedString(l slog.Level) string {
|
2017-02-23 20:30:32 +03:00
|
|
|
switch l {
|
2023-11-29 10:33:50 +03:00
|
|
|
case LevelTrace:
|
2017-02-23 20:30:32 +03:00
|
|
|
return "TRACE"
|
2023-11-29 10:33:50 +03:00
|
|
|
case slog.LevelDebug:
|
2017-02-23 20:30:32 +03:00
|
|
|
return "DEBUG"
|
2023-11-29 10:33:50 +03:00
|
|
|
case slog.LevelInfo:
|
2017-02-23 20:30:32 +03:00
|
|
|
return "INFO "
|
2023-11-29 10:33:50 +03:00
|
|
|
case slog.LevelWarn:
|
2017-02-23 20:30:32 +03:00
|
|
|
return "WARN "
|
2023-11-29 10:33:50 +03:00
|
|
|
case slog.LevelError:
|
2017-02-23 20:30:32 +03:00
|
|
|
return "ERROR"
|
2023-11-29 10:33:50 +03:00
|
|
|
case LevelCrit:
|
2017-02-23 20:30:32 +03:00
|
|
|
return "CRIT "
|
|
|
|
default:
|
2023-11-29 10:33:50 +03:00
|
|
|
return "unknown level"
|
2017-02-23 20:30:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 17:27:33 +03:00
|
|
|
// LevelString returns a string containing the name of a Lvl.
|
2023-11-29 10:33:50 +03:00
|
|
|
func LevelString(l slog.Level) string {
|
2017-02-20 18:39:36 +03:00
|
|
|
switch l {
|
2023-11-29 10:33:50 +03:00
|
|
|
case LevelTrace:
|
|
|
|
return "trace"
|
|
|
|
case slog.LevelDebug:
|
|
|
|
return "debug"
|
|
|
|
case slog.LevelInfo:
|
2017-02-20 18:39:36 +03:00
|
|
|
return "info"
|
2023-11-29 10:33:50 +03:00
|
|
|
case slog.LevelWarn:
|
2017-02-20 18:39:36 +03:00
|
|
|
return "warn"
|
2023-11-29 10:33:50 +03:00
|
|
|
case slog.LevelError:
|
2024-01-08 17:27:33 +03:00
|
|
|
return "error"
|
2023-11-29 10:33:50 +03:00
|
|
|
case LevelCrit:
|
2017-02-20 18:39:36 +03:00
|
|
|
return "crit"
|
|
|
|
default:
|
2023-11-29 10:33:50 +03:00
|
|
|
return "unknown"
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Logger writes key/value pairs to a Handler
|
|
|
|
type Logger interface {
|
2023-11-29 10:33:50 +03:00
|
|
|
// With returns a new Logger that has this logger's attributes plus the given attributes
|
|
|
|
With(ctx ...interface{}) Logger
|
2017-02-20 18:39:36 +03:00
|
|
|
|
2024-06-17 12:03:27 +03:00
|
|
|
// New returns a new Logger that has this logger's attributes plus the given attributes. Identical to 'With'.
|
2023-11-29 10:33:50 +03:00
|
|
|
New(ctx ...interface{}) Logger
|
2017-02-20 18:39:36 +03:00
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
// Log logs a message at the specified level with context key/value pairs
|
|
|
|
Log(level slog.Level, msg string, ctx ...interface{})
|
2017-02-20 18:39:36 +03:00
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
// Trace log a message at the trace level with context key/value pairs
|
2017-02-20 19:05:15 +03:00
|
|
|
Trace(msg string, ctx ...interface{})
|
2023-02-22 15:39:41 +03:00
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
// Debug logs a message at the debug level with context key/value pairs
|
2017-02-20 18:39:36 +03:00
|
|
|
Debug(msg string, ctx ...interface{})
|
2023-02-22 15:39:41 +03:00
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
// Info logs a message at the info level with context key/value pairs
|
2017-02-20 18:39:36 +03:00
|
|
|
Info(msg string, ctx ...interface{})
|
2023-02-22 15:39:41 +03:00
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
// Warn logs a message at the warn level with context key/value pairs
|
2017-02-20 18:39:36 +03:00
|
|
|
Warn(msg string, ctx ...interface{})
|
2023-02-22 15:39:41 +03:00
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
// Error logs a message at the error level with context key/value pairs
|
2017-02-20 18:39:36 +03:00
|
|
|
Error(msg string, ctx ...interface{})
|
2023-02-22 15:39:41 +03:00
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
// Crit logs a message at the crit level with context key/value pairs, and exits
|
2017-02-20 18:39:36 +03:00
|
|
|
Crit(msg string, ctx ...interface{})
|
2023-11-29 10:33:50 +03:00
|
|
|
|
|
|
|
// Write logs a message at the specified level
|
|
|
|
Write(level slog.Level, msg string, attrs ...any)
|
log: remove lazy, remove unused interfaces, unexport methods (#28622)
This change
- Removes interface `log.Format`,
- Removes method `log.FormatFunc`,
- unexports `TerminalHandler.TerminalFormat` formatting methods (renamed to `TerminalHandler.format`)
- removes the notion of `log.Lazy` values
The lazy handler was useful in the old log package, since it
could defer the evaluation of costly attributes until later in the
log pipeline: thus, if the logging was done at 'Trace', we could
skip evaluation if logging only was set to 'Info'.
With the move to slog, this way of deferring evaluation is no longer
needed, since slog introduced 'Enabled': the caller can thus do
the evaluate-or-not decision at the callsite, which is much more
straight-forward than dealing with lazy reflect-based evaluation.
Also, lazy evaluation would not work with 'native' slog, as in, these
two statements would be evaluated differently:
```golang
log.Info("foo", "my lazy", lazyObj)
slog.Info("foo", "my lazy", lazyObj)
```
2023-12-05 13:54:44 +03:00
|
|
|
|
|
|
|
// Enabled reports whether l emits log records at the given context and level.
|
|
|
|
Enabled(ctx context.Context, level slog.Level) bool
|
2024-03-08 02:01:31 +03:00
|
|
|
|
|
|
|
// Handler returns the underlying handler of the inner logger.
|
|
|
|
Handler() slog.Handler
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type logger struct {
|
2023-11-29 10:33:50 +03:00
|
|
|
inner *slog.Logger
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
// NewLogger returns a logger with the specified handler set
|
|
|
|
func NewLogger(h slog.Handler) Logger {
|
|
|
|
return &logger{
|
|
|
|
slog.New(h),
|
2023-09-07 15:48:49 +03:00
|
|
|
}
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
2024-03-08 02:01:31 +03:00
|
|
|
func (l *logger) Handler() slog.Handler {
|
|
|
|
return l.inner.Handler()
|
|
|
|
}
|
|
|
|
|
2024-06-17 12:03:27 +03:00
|
|
|
// Write logs a message at the specified level.
|
2023-11-29 10:33:50 +03:00
|
|
|
func (l *logger) Write(level slog.Level, msg string, attrs ...any) {
|
|
|
|
if !l.inner.Enabled(context.Background(), level) {
|
|
|
|
return
|
|
|
|
}
|
2017-02-20 18:39:36 +03:00
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
var pcs [1]uintptr
|
|
|
|
runtime.Callers(3, pcs[:])
|
2017-02-20 18:39:36 +03:00
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
if len(attrs)%2 != 0 {
|
|
|
|
attrs = append(attrs, nil, errorKey, "Normalized odd number of arguments by adding nil")
|
|
|
|
}
|
|
|
|
r := slog.NewRecord(time.Now(), level, msg, pcs[0])
|
|
|
|
r.Add(attrs...)
|
|
|
|
l.inner.Handler().Handle(context.Background(), r)
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
func (l *logger) Log(level slog.Level, msg string, attrs ...any) {
|
|
|
|
l.Write(level, msg, attrs...)
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
func (l *logger) With(ctx ...interface{}) Logger {
|
|
|
|
return &logger{l.inner.With(ctx...)}
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
func (l *logger) New(ctx ...interface{}) Logger {
|
|
|
|
return l.With(ctx...)
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
log: remove lazy, remove unused interfaces, unexport methods (#28622)
This change
- Removes interface `log.Format`,
- Removes method `log.FormatFunc`,
- unexports `TerminalHandler.TerminalFormat` formatting methods (renamed to `TerminalHandler.format`)
- removes the notion of `log.Lazy` values
The lazy handler was useful in the old log package, since it
could defer the evaluation of costly attributes until later in the
log pipeline: thus, if the logging was done at 'Trace', we could
skip evaluation if logging only was set to 'Info'.
With the move to slog, this way of deferring evaluation is no longer
needed, since slog introduced 'Enabled': the caller can thus do
the evaluate-or-not decision at the callsite, which is much more
straight-forward than dealing with lazy reflect-based evaluation.
Also, lazy evaluation would not work with 'native' slog, as in, these
two statements would be evaluated differently:
```golang
log.Info("foo", "my lazy", lazyObj)
slog.Info("foo", "my lazy", lazyObj)
```
2023-12-05 13:54:44 +03:00
|
|
|
// Enabled reports whether l emits log records at the given context and level.
|
|
|
|
func (l *logger) Enabled(ctx context.Context, level slog.Level) bool {
|
|
|
|
return l.inner.Enabled(ctx, level)
|
|
|
|
}
|
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
func (l *logger) Trace(msg string, ctx ...interface{}) {
|
|
|
|
l.Write(LevelTrace, msg, ctx...)
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
func (l *logger) Debug(msg string, ctx ...interface{}) {
|
|
|
|
l.Write(slog.LevelDebug, msg, ctx...)
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
func (l *logger) Info(msg string, ctx ...interface{}) {
|
|
|
|
l.Write(slog.LevelInfo, msg, ctx...)
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
func (l *logger) Warn(msg string, ctx ...any) {
|
|
|
|
l.Write(slog.LevelWarn, msg, ctx...)
|
|
|
|
}
|
2017-02-20 18:39:36 +03:00
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
func (l *logger) Error(msg string, ctx ...interface{}) {
|
|
|
|
l.Write(slog.LevelError, msg, ctx...)
|
|
|
|
}
|
2017-02-20 18:39:36 +03:00
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
func (l *logger) Crit(msg string, ctx ...interface{}) {
|
|
|
|
l.Write(LevelCrit, msg, ctx...)
|
|
|
|
os.Exit(1)
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|