root.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package log15
  2. import (
  3. "os"
  4. "github.com/mattn/go-colorable"
  5. isatty "github.com/mattn/go-isatty"
  6. )
  7. // Predefined handlers
  8. var (
  9. root *logger
  10. StdoutHandler = StreamHandler(os.Stdout, LogfmtFormat())
  11. StderrHandler = StreamHandler(os.Stderr, LogfmtFormat())
  12. )
  13. func init() {
  14. if isatty.IsTerminal(os.Stdout.Fd()) {
  15. StdoutHandler = StreamHandler(colorable.NewColorableStdout(), TerminalFormat())
  16. }
  17. if isatty.IsTerminal(os.Stderr.Fd()) {
  18. StderrHandler = StreamHandler(colorable.NewColorableStderr(), TerminalFormat())
  19. }
  20. root = &logger{[]interface{}{}, new(swapHandler)}
  21. root.SetHandler(StdoutHandler)
  22. }
  23. // New returns a new logger with the given context.
  24. // New is a convenient alias for Root().New
  25. func New(ctx ...interface{}) Logger {
  26. return root.New(ctx...)
  27. }
  28. // Root returns the root logger
  29. func Root() Logger {
  30. return root
  31. }
  32. // The following functions bypass the exported logger methods (logger.Debug,
  33. // etc.) to keep the call depth the same for all paths to logger.write so
  34. // runtime.Caller(2) always refers to the call site in client code.
  35. // Debug is a convenient alias for Root().Debug
  36. func Debug(msg string, ctx ...interface{}) {
  37. root.write(msg, LvlDebug, ctx)
  38. }
  39. // Info is a convenient alias for Root().Info
  40. func Info(msg string, ctx ...interface{}) {
  41. root.write(msg, LvlInfo, ctx)
  42. }
  43. // Warn is a convenient alias for Root().Warn
  44. func Warn(msg string, ctx ...interface{}) {
  45. root.write(msg, LvlWarn, ctx)
  46. }
  47. // Error is a convenient alias for Root().Error
  48. func Error(msg string, ctx ...interface{}) {
  49. root.write(msg, LvlError, ctx)
  50. }
  51. // Crit is a convenient alias for Root().Crit
  52. func Crit(msg string, ctx ...interface{}) {
  53. root.write(msg, LvlCrit, ctx)
  54. }