2017-02-20 15:39:36 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2023-11-29 07:33:50 +00:00
|
|
|
"context"
|
|
|
|
"math"
|
2017-02-20 16:05:15 +00:00
|
|
|
"os"
|
2023-11-29 07:33:50 +00:00
|
|
|
"runtime"
|
2017-02-20 15:39:36 +00:00
|
|
|
"time"
|
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
"golang.org/x/exp/slog"
|
2017-02-20 15:39:36 +00:00
|
|
|
)
|
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
const errorKey = "LOG_ERROR"
|
2017-02-20 15:39:36 +00:00
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
const (
|
|
|
|
legacyLevelCrit = iota
|
|
|
|
legacyLevelError
|
|
|
|
legacyLevelWarn
|
|
|
|
legacyLevelInfo
|
|
|
|
legacyLevelDebug
|
|
|
|
legacyLevelTrace
|
|
|
|
)
|
2017-02-20 15:39:36 +00:00
|
|
|
|
|
|
|
const (
|
2023-11-29 07:33:50 +00: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 15:39:36 +00:00
|
|
|
)
|
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
// convert from old Geth verbosity level constants
|
|
|
|
// 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 17:30:32 +00:00
|
|
|
switch l {
|
2023-11-29 07:33:50 +00:00
|
|
|
case LevelTrace:
|
2017-02-23 17:30:32 +00:00
|
|
|
return "TRACE"
|
2023-11-29 07:33:50 +00:00
|
|
|
case slog.LevelDebug:
|
2017-02-23 17:30:32 +00:00
|
|
|
return "DEBUG"
|
2023-11-29 07:33:50 +00:00
|
|
|
case slog.LevelInfo:
|
2017-02-23 17:30:32 +00:00
|
|
|
return "INFO "
|
2023-11-29 07:33:50 +00:00
|
|
|
case slog.LevelWarn:
|
2017-02-23 17:30:32 +00:00
|
|
|
return "WARN "
|
2023-11-29 07:33:50 +00:00
|
|
|
case slog.LevelError:
|
2017-02-23 17:30:32 +00:00
|
|
|
return "ERROR"
|
2023-11-29 07:33:50 +00:00
|
|
|
case LevelCrit:
|
2017-02-23 17:30:32 +00:00
|
|
|
return "CRIT "
|
|
|
|
default:
|
2023-11-29 07:33:50 +00:00
|
|
|
return "unknown level"
|
2017-02-23 17:30:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 14:27:33 +00:00
|
|
|
// LevelString returns a string containing the name of a Lvl.
|
2023-11-29 07:33:50 +00:00
|
|
|
func LevelString(l slog.Level) string {
|
2017-02-20 15:39:36 +00:00
|
|
|
switch l {
|
2023-11-29 07:33:50 +00:00
|
|
|
case LevelTrace:
|
|
|
|
return "trace"
|
|
|
|
case slog.LevelDebug:
|
|
|
|
return "debug"
|
|
|
|
case slog.LevelInfo:
|
2017-02-20 15:39:36 +00:00
|
|
|
return "info"
|
2023-11-29 07:33:50 +00:00
|
|
|
case slog.LevelWarn:
|
2017-02-20 15:39:36 +00:00
|
|
|
return "warn"
|
2023-11-29 07:33:50 +00:00
|
|
|
case slog.LevelError:
|
2024-01-08 14:27:33 +00:00
|
|
|
return "error"
|
2023-11-29 07:33:50 +00:00
|
|
|
case LevelCrit:
|
2017-02-20 15:39:36 +00:00
|
|
|
return "crit"
|
|
|
|
default:
|
2023-11-29 07:33:50 +00:00
|
|
|
return "unknown"
|
2017-02-20 15:39:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Logger writes key/value pairs to a Handler
|
|
|
|
type Logger interface {
|
2023-11-29 07:33:50 +00:00
|
|
|
// With returns a new Logger that has this logger's attributes plus the given attributes
|
|
|
|
With(ctx ...interface{}) Logger
|
2017-02-20 15:39:36 +00:00
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
// With returns a new Logger that has this logger's attributes plus the given attributes. Identical to 'With'.
|
|
|
|
New(ctx ...interface{}) Logger
|
2017-02-20 15:39:36 +00:00
|
|
|
|
2023-11-29 07:33:50 +00: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 15:39:36 +00:00
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
// Trace log a message at the trace level with context key/value pairs
|
2017-02-20 16:05:15 +00:00
|
|
|
Trace(msg string, ctx ...interface{})
|
2023-02-22 12:39:41 +00:00
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
// Debug logs a message at the debug level with context key/value pairs
|
2017-02-20 15:39:36 +00:00
|
|
|
Debug(msg string, ctx ...interface{})
|
2023-02-22 12:39:41 +00:00
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
// Info logs a message at the info level with context key/value pairs
|
2017-02-20 15:39:36 +00:00
|
|
|
Info(msg string, ctx ...interface{})
|
2023-02-22 12:39:41 +00:00
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
// Warn logs a message at the warn level with context key/value pairs
|
2017-02-20 15:39:36 +00:00
|
|
|
Warn(msg string, ctx ...interface{})
|
2023-02-22 12:39:41 +00:00
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
// Error logs a message at the error level with context key/value pairs
|
2017-02-20 15:39:36 +00:00
|
|
|
Error(msg string, ctx ...interface{})
|
2023-02-22 12:39:41 +00:00
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
// Crit logs a message at the crit level with context key/value pairs, and exits
|
2017-02-20 15:39:36 +00:00
|
|
|
Crit(msg string, ctx ...interface{})
|
2023-11-29 07:33:50 +00: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 10:54:44 +00:00
|
|
|
|
|
|
|
// Enabled reports whether l emits log records at the given context and level.
|
|
|
|
Enabled(ctx context.Context, level slog.Level) bool
|
2017-02-20 15:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type logger struct {
|
2023-11-29 07:33:50 +00:00
|
|
|
inner *slog.Logger
|
2017-02-20 15:39:36 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
// NewLogger returns a logger with the specified handler set
|
|
|
|
func NewLogger(h slog.Handler) Logger {
|
|
|
|
return &logger{
|
|
|
|
slog.New(h),
|
2023-09-07 12:48:49 +00:00
|
|
|
}
|
2017-02-20 15:39:36 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
// write logs a message at the specified level:
|
|
|
|
func (l *logger) Write(level slog.Level, msg string, attrs ...any) {
|
|
|
|
if !l.inner.Enabled(context.Background(), level) {
|
|
|
|
return
|
|
|
|
}
|
2017-02-20 15:39:36 +00:00
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
var pcs [1]uintptr
|
|
|
|
runtime.Callers(3, pcs[:])
|
2017-02-20 15:39:36 +00:00
|
|
|
|
2023-11-29 07:33:50 +00: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 15:39:36 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
func (l *logger) Log(level slog.Level, msg string, attrs ...any) {
|
|
|
|
l.Write(level, msg, attrs...)
|
2017-02-20 15:39:36 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
func (l *logger) With(ctx ...interface{}) Logger {
|
|
|
|
return &logger{l.inner.With(ctx...)}
|
2017-02-20 15:39:36 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
func (l *logger) New(ctx ...interface{}) Logger {
|
|
|
|
return l.With(ctx...)
|
2017-02-20 15:39:36 +00: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 10:54:44 +00: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 07:33:50 +00:00
|
|
|
func (l *logger) Trace(msg string, ctx ...interface{}) {
|
|
|
|
l.Write(LevelTrace, msg, ctx...)
|
2017-02-20 15:39:36 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
func (l *logger) Debug(msg string, ctx ...interface{}) {
|
|
|
|
l.Write(slog.LevelDebug, msg, ctx...)
|
2017-02-20 15:39:36 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
func (l *logger) Info(msg string, ctx ...interface{}) {
|
|
|
|
l.Write(slog.LevelInfo, msg, ctx...)
|
2017-02-20 15:39:36 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
func (l *logger) Warn(msg string, ctx ...any) {
|
|
|
|
l.Write(slog.LevelWarn, msg, ctx...)
|
|
|
|
}
|
2017-02-20 15:39:36 +00:00
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
func (l *logger) Error(msg string, ctx ...interface{}) {
|
|
|
|
l.Write(slog.LevelError, msg, ctx...)
|
|
|
|
}
|
2017-02-20 15:39:36 +00:00
|
|
|
|
2023-11-29 07:33:50 +00:00
|
|
|
func (l *logger) Crit(msg string, ctx ...interface{}) {
|
|
|
|
l.Write(LevelCrit, msg, ctx...)
|
|
|
|
os.Exit(1)
|
2017-02-20 15:39:36 +00:00
|
|
|
}
|