root.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package log
  2. import (
  3. "os"
  4. )
  5. var (
  6. root = &logger{[]interface{}{}, new(swapHandler)}
  7. StdoutHandler = StreamHandler(os.Stdout, LogfmtFormat())
  8. StderrHandler = StreamHandler(os.Stderr, LogfmtFormat())
  9. )
  10. func init() {
  11. root.SetHandler(DiscardHandler())
  12. }
  13. // New returns a new logger with the given context.
  14. // New is a convenient alias for Root().New
  15. func New(ctx ...interface{}) Logger {
  16. return root.New(ctx...)
  17. }
  18. // Root returns the root logger
  19. func Root() Logger {
  20. return root
  21. }
  22. // The following functions bypass the exported logger methods (logger.Debug,
  23. // etc.) to keep the call depth the same for all paths to logger.write so
  24. // runtime.Caller(2) always refers to the call site in client code.
  25. // Trace is a convenient alias for Root().Trace
  26. func Trace(msg string, ctx ...interface{}) {
  27. root.write(msg, LvlTrace, ctx, skipLevel)
  28. }
  29. // Debug is a convenient alias for Root().Debug
  30. func Debug(msg string, ctx ...interface{}) {
  31. root.write(msg, LvlDebug, ctx, skipLevel)
  32. }
  33. // Info is a convenient alias for Root().Info
  34. func Info(msg string, ctx ...interface{}) {
  35. root.write(msg, LvlInfo, ctx, skipLevel)
  36. }
  37. // Warn is a convenient alias for Root().Warn
  38. func Warn(msg string, ctx ...interface{}) {
  39. root.write(msg, LvlWarn, ctx, skipLevel)
  40. }
  41. // Error is a convenient alias for Root().Error
  42. func Error(msg string, ctx ...interface{}) {
  43. root.write(msg, LvlError, ctx, skipLevel)
  44. }
  45. // Crit is a convenient alias for Root().Crit
  46. func Crit(msg string, ctx ...interface{}) {
  47. root.write(msg, LvlCrit, ctx, skipLevel)
  48. os.Exit(1)
  49. }
  50. // Output is a convenient alias for write, allowing for the modification of
  51. // the calldepth (number of stack frames to skip).
  52. // calldepth influences the reported line number of the log message.
  53. // A calldepth of zero reports the immediate caller of Output.
  54. // Non-zero calldepth skips as many stack frames.
  55. func Output(msg string, lvl Lvl, calldepth int, ctx ...interface{}) {
  56. root.write(msg, lvl, ctx, calldepth+skipLevel)
  57. }