log.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package log
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "sync"
  7. "github.com/labstack/gommon/color"
  8. "github.com/mattn/go-colorable"
  9. "github.com/mattn/go-isatty"
  10. )
  11. type (
  12. Logger struct {
  13. level Level
  14. out io.Writer
  15. err io.Writer
  16. prefix string
  17. sync.Mutex
  18. }
  19. Level uint8
  20. )
  21. const (
  22. TRACE = iota
  23. DEBUG
  24. INFO
  25. NOTICE
  26. WARN
  27. ERROR
  28. FATAL
  29. OFF
  30. )
  31. var (
  32. global = New("-")
  33. levels []string
  34. )
  35. func New(prefix string) (l *Logger) {
  36. l = &Logger{
  37. level: INFO,
  38. prefix: prefix,
  39. }
  40. l.SetOutput(colorable.NewColorableStdout())
  41. l.err = colorable.NewColorableStderr()
  42. return
  43. }
  44. func (l *Logger) SetPrefix(p string) {
  45. l.prefix = p
  46. }
  47. func (l *Logger) SetLevel(v Level) {
  48. l.level = v
  49. }
  50. func (l *Logger) SetOutput(w io.Writer) {
  51. l.out = w
  52. l.err = w
  53. color.Disable()
  54. switch w := w.(type) {
  55. case *os.File:
  56. if isatty.IsTerminal(w.Fd()) {
  57. color.Enable()
  58. }
  59. levels = []string{
  60. color.Cyan("TRACE"),
  61. color.Blue("DEBUG"),
  62. color.Green("INFO"),
  63. color.Magenta("NOTICE"),
  64. color.Yellow("WARN"),
  65. color.Red("ERROR"),
  66. color.RedBg("FATAL"),
  67. }
  68. }
  69. }
  70. func (l *Logger) Trace(msg interface{}, args ...interface{}) {
  71. l.log(TRACE, l.out, msg, args...)
  72. }
  73. func (l *Logger) Debug(msg interface{}, args ...interface{}) {
  74. l.log(DEBUG, l.out, msg, args...)
  75. }
  76. func (l *Logger) Info(msg interface{}, args ...interface{}) {
  77. l.log(INFO, l.out, msg, args...)
  78. }
  79. func (l *Logger) Notice(msg interface{}, args ...interface{}) {
  80. l.log(NOTICE, l.out, msg, args...)
  81. }
  82. func (l *Logger) Warn(msg interface{}, args ...interface{}) {
  83. l.log(WARN, l.out, msg, args...)
  84. }
  85. func (l *Logger) Error(msg interface{}, args ...interface{}) {
  86. l.log(ERROR, l.err, msg, args...)
  87. }
  88. func (l *Logger) Fatal(msg interface{}, args ...interface{}) {
  89. l.log(FATAL, l.err, msg, args...)
  90. }
  91. func SetPrefix(p string) {
  92. global.SetPrefix(p)
  93. }
  94. func SetLevel(v Level) {
  95. global.SetLevel(v)
  96. }
  97. func SetOutput(w io.Writer) {
  98. global.SetOutput(w)
  99. }
  100. func Trace(msg interface{}, args ...interface{}) {
  101. global.Trace(msg, args...)
  102. }
  103. func Debug(msg interface{}, args ...interface{}) {
  104. global.Debug(msg, args...)
  105. }
  106. func Info(msg interface{}, args ...interface{}) {
  107. global.Info(msg, args...)
  108. }
  109. func Notice(msg interface{}, args ...interface{}) {
  110. global.Notice(msg, args...)
  111. }
  112. func Warn(msg interface{}, args ...interface{}) {
  113. global.Warn(msg, args...)
  114. }
  115. func Error(msg interface{}, args ...interface{}) {
  116. global.Error(msg, args...)
  117. }
  118. func Fatal(msg interface{}, args ...interface{}) {
  119. global.Fatal(msg, args...)
  120. }
  121. func (l *Logger) log(v Level, w io.Writer, msg interface{}, args ...interface{}) {
  122. l.Lock()
  123. defer l.Unlock()
  124. if v >= l.level {
  125. // TODO: Improve performance
  126. f := fmt.Sprintf("%s|%s|%s\n", levels[v], l.prefix, msg)
  127. fmt.Fprintf(w, f, args...)
  128. }
  129. }