2021-12-09 20:34:40 +03:00
|
|
|
package viper
|
|
|
|
|
|
|
|
import (
|
2023-09-12 01:03:41 +03:00
|
|
|
"context"
|
2021-12-09 20:34:40 +03:00
|
|
|
|
2023-09-12 01:03:41 +03:00
|
|
|
slog "github.com/sagikazarmark/slog-shim"
|
2021-12-09 20:34:40 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Logger is a unified interface for various logging use cases and practices, including:
|
2022-09-03 19:05:45 +03:00
|
|
|
// - leveled logging
|
|
|
|
// - structured logging
|
2023-09-12 01:48:37 +03:00
|
|
|
//
|
|
|
|
// Deprecated: use `log/slog` instead.
|
2021-12-09 20:34:40 +03:00
|
|
|
type Logger interface {
|
|
|
|
// Trace logs a Trace event.
|
|
|
|
//
|
|
|
|
// Even more fine-grained information than Debug events.
|
|
|
|
// Loggers not supporting this level should fall back to Debug.
|
2023-09-26 17:59:38 +03:00
|
|
|
Trace(msg string, keyvals ...any)
|
2021-12-09 20:34:40 +03:00
|
|
|
|
|
|
|
// Debug logs a Debug event.
|
|
|
|
//
|
|
|
|
// A verbose series of information events.
|
|
|
|
// They are useful when debugging the system.
|
2023-09-26 17:59:38 +03:00
|
|
|
Debug(msg string, keyvals ...any)
|
2021-12-09 20:34:40 +03:00
|
|
|
|
|
|
|
// Info logs an Info event.
|
|
|
|
//
|
|
|
|
// General information about what's happening inside the system.
|
2023-09-26 17:59:38 +03:00
|
|
|
Info(msg string, keyvals ...any)
|
2021-12-09 20:34:40 +03:00
|
|
|
|
|
|
|
// Warn logs a Warn(ing) event.
|
|
|
|
//
|
|
|
|
// Non-critical events that should be looked at.
|
2023-09-26 17:59:38 +03:00
|
|
|
Warn(msg string, keyvals ...any)
|
2021-12-09 20:34:40 +03:00
|
|
|
|
|
|
|
// Error logs an Error event.
|
|
|
|
//
|
|
|
|
// Critical events that require immediate attention.
|
|
|
|
// Loggers commonly provide Fatal and Panic levels above Error level,
|
2023-08-18 16:29:46 +03:00
|
|
|
// but exiting and panicking is out of scope for a logging library.
|
2023-09-26 17:59:38 +03:00
|
|
|
Error(msg string, keyvals ...any)
|
2021-12-09 20:34:40 +03:00
|
|
|
}
|
|
|
|
|
2023-09-12 01:03:41 +03:00
|
|
|
// WithLogger sets a custom logger.
|
|
|
|
func WithLogger(l *slog.Logger) Option {
|
|
|
|
return optionFunc(func(v *Viper) {
|
|
|
|
v.logger = l
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type discardHandler struct{}
|
|
|
|
|
|
|
|
func (n *discardHandler) Enabled(_ context.Context, _ slog.Level) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *discardHandler) Handle(_ context.Context, _ slog.Record) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-12 01:53:34 +03:00
|
|
|
func (n *discardHandler) WithAttrs(_ []slog.Attr) slog.Handler {
|
2023-09-12 01:03:41 +03:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2023-09-12 01:53:34 +03:00
|
|
|
func (n *discardHandler) WithGroup(_ string) slog.Handler {
|
2023-09-12 01:03:41 +03:00
|
|
|
return n
|
|
|
|
}
|