root.go 1.8 KB

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