root.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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)
  28. }
  29. // Debug is a convenient alias for Root().Debug
  30. func Debug(msg string, ctx ...interface{}) {
  31. root.write(msg, LvlDebug, ctx)
  32. }
  33. // Info is a convenient alias for Root().Info
  34. func Info(msg string, ctx ...interface{}) {
  35. root.write(msg, LvlInfo, ctx)
  36. }
  37. // Warn is a convenient alias for Root().Warn
  38. func Warn(msg string, ctx ...interface{}) {
  39. root.write(msg, LvlWarn, ctx)
  40. }
  41. // Error is a convenient alias for Root().Error
  42. func Error(msg string, ctx ...interface{}) {
  43. root.write(msg, LvlError, ctx)
  44. }
  45. // Crit is a convenient alias for Root().Crit
  46. func Crit(msg string, ctx ...interface{}) {
  47. root.write(msg, LvlCrit, ctx)
  48. os.Exit(1)
  49. }