2017-02-20 15:39:36 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-02-23 17:42:42 +00:00
|
|
|
root = &logger{[]interface{}{}, new(swapHandler)}
|
2017-02-20 15:39:36 +00:00
|
|
|
StdoutHandler = StreamHandler(os.Stdout, LogfmtFormat())
|
|
|
|
StderrHandler = StreamHandler(os.Stderr, LogfmtFormat())
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-02-23 17:42:42 +00:00
|
|
|
root.SetHandler(DiscardHandler())
|
2017-02-20 15:39:36 +00: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.New(ctx...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Root returns the root logger
|
|
|
|
func Root() Logger {
|
|
|
|
return root
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following functions bypass the exported logger methods (logger.Debug,
|
|
|
|
// etc.) to keep the call depth the same for all paths to logger.write so
|
|
|
|
// runtime.Caller(2) always refers to the call site in client code.
|
|
|
|
|
2017-02-20 16:05:15 +00:00
|
|
|
// Trace is a convenient alias for Root().Trace
|
2023-02-22 12:39:41 +00: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 16:05:15 +00:00
|
|
|
func Trace(msg string, ctx ...interface{}) {
|
2018-06-14 09:21:17 +00:00
|
|
|
root.write(msg, LvlTrace, ctx, skipLevel)
|
2017-02-20 16:05:15 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 15:39:36 +00:00
|
|
|
// Debug is a convenient alias for Root().Debug
|
2023-02-22 12:39:41 +00: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 15:39:36 +00:00
|
|
|
func Debug(msg string, ctx ...interface{}) {
|
2018-06-14 09:21:17 +00:00
|
|
|
root.write(msg, LvlDebug, ctx, skipLevel)
|
2017-02-20 15:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info is a convenient alias for Root().Info
|
2023-02-22 12:39:41 +00: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 15:39:36 +00:00
|
|
|
func Info(msg string, ctx ...interface{}) {
|
2018-06-14 09:21:17 +00:00
|
|
|
root.write(msg, LvlInfo, ctx, skipLevel)
|
2017-02-20 15:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Warn is a convenient alias for Root().Warn
|
2023-02-22 12:39:41 +00: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 15:39:36 +00:00
|
|
|
func Warn(msg string, ctx ...interface{}) {
|
2018-06-14 09:21:17 +00:00
|
|
|
root.write(msg, LvlWarn, ctx, skipLevel)
|
2017-02-20 15:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error is a convenient alias for Root().Error
|
2023-02-22 12:39:41 +00: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 15:39:36 +00:00
|
|
|
func Error(msg string, ctx ...interface{}) {
|
2018-06-14 09:21:17 +00:00
|
|
|
root.write(msg, LvlError, ctx, skipLevel)
|
2017-02-20 15:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Crit is a convenient alias for Root().Crit
|
2023-02-22 12:39:41 +00: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 15:39:36 +00:00
|
|
|
func Crit(msg string, ctx ...interface{}) {
|
2018-06-14 09:21:17 +00:00
|
|
|
root.write(msg, LvlCrit, ctx, skipLevel)
|
2017-02-20 16:05:15 +00:00
|
|
|
os.Exit(1)
|
2017-02-20 15:39:36 +00:00
|
|
|
}
|
2018-06-14 09:21:17 +00:00
|
|
|
|
|
|
|
// Output is a convenient alias for write, allowing for the modification of
|
|
|
|
// the calldepth (number of stack frames to skip).
|
|
|
|
// calldepth influences the reported line number of the log message.
|
|
|
|
// A calldepth of zero reports the immediate caller of Output.
|
|
|
|
// Non-zero calldepth skips as many stack frames.
|
|
|
|
func Output(msg string, lvl Lvl, calldepth int, ctx ...interface{}) {
|
|
|
|
root.write(msg, lvl, ctx, calldepth+skipLevel)
|
|
|
|
}
|