log.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. mu 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. out: colorable.NewColorableStdout(),
  40. err: colorable.NewColorableStderr(),
  41. }
  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) Level() Level {
  51. return l.level
  52. }
  53. func (l *Logger) SetOutput(w io.Writer) {
  54. l.out = w
  55. l.err = w
  56. switch w := w.(type) {
  57. case *os.File:
  58. if isatty.IsTerminal(w.Fd()) {
  59. color.Enable()
  60. }
  61. default:
  62. color.Disable()
  63. }
  64. // NOTE: Reintialize levels to reflect color enable/disable call.
  65. initLevels()
  66. }
  67. func (l *Logger) Print(msg interface{}, args ...interface{}) {
  68. f := fmt.Sprintf("%s", msg)
  69. fmt.Fprintf(l.out, f, args...)
  70. }
  71. func (l *Logger) Println(msg interface{}, args ...interface{}) {
  72. f := fmt.Sprintf("%s\n", msg)
  73. fmt.Fprintf(l.out, f, args...)
  74. }
  75. func (l *Logger) Trace(msg interface{}, args ...interface{}) {
  76. l.log(TRACE, l.out, msg, args...)
  77. }
  78. func (l *Logger) Debug(msg interface{}, args ...interface{}) {
  79. l.log(DEBUG, l.out, msg, args...)
  80. }
  81. func (l *Logger) Info(msg interface{}, args ...interface{}) {
  82. l.log(INFO, l.out, msg, args...)
  83. }
  84. func (l *Logger) Notice(msg interface{}, args ...interface{}) {
  85. l.log(NOTICE, l.out, msg, args...)
  86. }
  87. func (l *Logger) Warn(msg interface{}, args ...interface{}) {
  88. l.log(WARN, l.out, msg, args...)
  89. }
  90. func (l *Logger) Error(msg interface{}, args ...interface{}) {
  91. l.log(ERROR, l.err, msg, args...)
  92. }
  93. func (l *Logger) Fatal(msg interface{}, args ...interface{}) {
  94. l.log(FATAL, l.err, msg, args...)
  95. }
  96. func SetPrefix(p string) {
  97. global.SetPrefix(p)
  98. }
  99. func SetLevel(v Level) {
  100. global.SetLevel(v)
  101. }
  102. func SetOutput(w io.Writer) {
  103. global.SetOutput(w)
  104. }
  105. func Print(msg interface{}, args ...interface{}) {
  106. global.Print(msg, args...)
  107. }
  108. func Println(msg interface{}, args ...interface{}) {
  109. global.Println(msg, args...)
  110. }
  111. func Trace(msg interface{}, args ...interface{}) {
  112. global.Trace(msg, args...)
  113. }
  114. func Debug(msg interface{}, args ...interface{}) {
  115. global.Debug(msg, args...)
  116. }
  117. func Info(msg interface{}, args ...interface{}) {
  118. global.Info(msg, args...)
  119. }
  120. func Notice(msg interface{}, args ...interface{}) {
  121. global.Notice(msg, args...)
  122. }
  123. func Warn(msg interface{}, args ...interface{}) {
  124. global.Warn(msg, args...)
  125. }
  126. func Error(msg interface{}, args ...interface{}) {
  127. global.Error(msg, args...)
  128. }
  129. func Fatal(msg interface{}, args ...interface{}) {
  130. global.Fatal(msg, args...)
  131. }
  132. func (l *Logger) log(v Level, w io.Writer, msg interface{}, args ...interface{}) {
  133. l.mu.Lock()
  134. defer l.mu.Unlock()
  135. if v >= l.level {
  136. // TODO: Improve performance
  137. f := fmt.Sprintf("%s|%s|%v\n", levels[v], l.prefix, msg)
  138. fmt.Fprintf(w, f, args...)
  139. }
  140. }
  141. func initLevels() {
  142. levels = []string{
  143. color.Cyan("TRACE"),
  144. color.Blue("DEBUG"),
  145. color.Green("INFO"),
  146. color.Magenta("NOTICE"),
  147. color.Yellow("WARN"),
  148. color.Red("ERROR"),
  149. color.RedBg("FATAL"),
  150. }
  151. }
  152. func init() {
  153. initLevels()
  154. }