2017-02-20 18:39:36 +03:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2023-11-29 10:33:50 +03:00
|
|
|
"sync/atomic"
|
2023-11-25 15:57:23 +03:00
|
|
|
"time"
|
2017-02-20 18:39:36 +03:00
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
"golang.org/x/exp/slog"
|
2017-02-20 18:39:36 +03:00
|
|
|
)
|
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
var root atomic.Value
|
|
|
|
|
2017-02-20 18:39:36 +03:00
|
|
|
func init() {
|
2023-12-30 23:16:02 +03:00
|
|
|
root.Store(&logger{slog.New(DiscardHandler())})
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
// SetDefault sets the default global logger
|
|
|
|
func SetDefault(l Logger) {
|
|
|
|
root.Store(l)
|
|
|
|
if lg, ok := l.(*logger); ok {
|
|
|
|
slog.SetDefault(lg.inner)
|
|
|
|
}
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Root returns the root logger
|
|
|
|
func Root() Logger {
|
2023-11-29 10:33:50 +03:00
|
|
|
return root.Load().(Logger)
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The following functions bypass the exported logger methods (logger.Debug,
|
2023-11-29 10:33:50 +03:00
|
|
|
// etc.) to keep the call depth the same for all paths to logger.Write so
|
2017-02-20 18:39:36 +03:00
|
|
|
// runtime.Caller(2) always refers to the call site in client code.
|
|
|
|
|
2017-02-20 19:05:15 +03:00
|
|
|
// Trace is a convenient alias for Root().Trace
|
2023-02-22 15:39:41 +03:00
|
|
|
//
|
|
|
|
// Log a message at the trace level with context key/value pairs
|
|
|
|
//
|
|
|
|
// # Usage
|
|
|
|
//
|
|
|
|
// log.Trace("msg")
|
|
|
|
// log.Trace("msg", "key1", val1)
|
|
|
|
// log.Trace("msg", "key1", val1, "key2", val2)
|
2017-02-20 19:05:15 +03:00
|
|
|
func Trace(msg string, ctx ...interface{}) {
|
2023-11-29 10:33:50 +03:00
|
|
|
Root().Write(LevelTrace, msg, ctx...)
|
2017-02-20 19:05:15 +03:00
|
|
|
}
|
|
|
|
|
2017-02-20 18:39:36 +03:00
|
|
|
// Debug is a convenient alias for Root().Debug
|
2023-02-22 15:39:41 +03:00
|
|
|
//
|
|
|
|
// Log a message at the debug level with context key/value pairs
|
|
|
|
//
|
|
|
|
// # Usage Examples
|
|
|
|
//
|
|
|
|
// log.Debug("msg")
|
|
|
|
// log.Debug("msg", "key1", val1)
|
|
|
|
// log.Debug("msg", "key1", val1, "key2", val2)
|
2017-02-20 18:39:36 +03:00
|
|
|
func Debug(msg string, ctx ...interface{}) {
|
2023-11-29 10:33:50 +03:00
|
|
|
Root().Write(slog.LevelDebug, msg, ctx...)
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info is a convenient alias for Root().Info
|
2023-02-22 15:39:41 +03:00
|
|
|
//
|
|
|
|
// Log a message at the info level with context key/value pairs
|
|
|
|
//
|
|
|
|
// # Usage Examples
|
|
|
|
//
|
|
|
|
// log.Info("msg")
|
|
|
|
// log.Info("msg", "key1", val1)
|
|
|
|
// log.Info("msg", "key1", val1, "key2", val2)
|
2017-02-20 18:39:36 +03:00
|
|
|
func Info(msg string, ctx ...interface{}) {
|
2023-11-29 10:33:50 +03:00
|
|
|
Root().Write(slog.LevelInfo, msg, ctx...)
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Warn is a convenient alias for Root().Warn
|
2023-02-22 15:39:41 +03:00
|
|
|
//
|
|
|
|
// Log a message at the warn level with context key/value pairs
|
|
|
|
//
|
|
|
|
// # Usage Examples
|
|
|
|
//
|
|
|
|
// log.Warn("msg")
|
|
|
|
// log.Warn("msg", "key1", val1)
|
|
|
|
// log.Warn("msg", "key1", val1, "key2", val2)
|
2017-02-20 18:39:36 +03:00
|
|
|
func Warn(msg string, ctx ...interface{}) {
|
2023-11-29 10:33:50 +03:00
|
|
|
Root().Write(slog.LevelWarn, msg, ctx...)
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error is a convenient alias for Root().Error
|
2023-02-22 15:39:41 +03:00
|
|
|
//
|
|
|
|
// Log a message at the error level with context key/value pairs
|
|
|
|
//
|
|
|
|
// # Usage Examples
|
|
|
|
//
|
|
|
|
// log.Error("msg")
|
|
|
|
// log.Error("msg", "key1", val1)
|
|
|
|
// log.Error("msg", "key1", val1, "key2", val2)
|
2017-02-20 18:39:36 +03:00
|
|
|
func Error(msg string, ctx ...interface{}) {
|
2023-11-29 10:33:50 +03:00
|
|
|
Root().Write(slog.LevelError, msg, ctx...)
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Crit is a convenient alias for Root().Crit
|
2023-02-22 15:39:41 +03:00
|
|
|
//
|
|
|
|
// Log a message at the crit level with context key/value pairs, and then exit.
|
|
|
|
//
|
|
|
|
// # Usage Examples
|
|
|
|
//
|
|
|
|
// log.Crit("msg")
|
|
|
|
// log.Crit("msg", "key1", val1)
|
|
|
|
// log.Crit("msg", "key1", val1, "key2", val2)
|
2017-02-20 18:39:36 +03:00
|
|
|
func Crit(msg string, ctx ...interface{}) {
|
2023-11-29 10:33:50 +03:00
|
|
|
Root().Write(LevelCrit, msg, ctx...)
|
2023-11-25 15:57:23 +03:00
|
|
|
time.Sleep(3 * time.Second)
|
2017-02-20 19:05:15 +03:00
|
|
|
os.Exit(1)
|
2017-02-20 18:39:36 +03:00
|
|
|
}
|
2018-06-14 12:21:17 +03:00
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
// New returns a new logger with the given context.
|
|
|
|
// New is a convenient alias for Root().New
|
|
|
|
func New(ctx ...interface{}) Logger {
|
|
|
|
return Root().With(ctx...)
|
2018-06-14 12:21:17 +03:00
|
|
|
}
|