log.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package log
  2. import (
  3. "fmt"
  4. "github.com/davecgh/go-spew/spew"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "time"
  9. )
  10. var level = 0
  11. const (
  12. // TRACE level
  13. TRACE = 0
  14. // DEBUG level
  15. DEBUG = 1
  16. // INFO level
  17. INFO = 2
  18. // WARNING level
  19. WARNING = 3
  20. // ERROR level
  21. ERROR = 4
  22. // FATAL level
  23. FATAL = 5
  24. )
  25. // Stringer interface
  26. type Stringer interface {
  27. LogString() string
  28. }
  29. // Init logger
  30. func Init(plevel int) {
  31. level = plevel
  32. spew.Config.DisableMethods = true
  33. spew.Config.Indent = " "
  34. }
  35. // Dump object
  36. func Dump(a ...interface{}) string {
  37. return spew.Sdump(a...)
  38. }
  39. func dump(a ...interface{}) []interface{} {
  40. for ai, av := range a {
  41. if avf, ok := av.(func() string); ok {
  42. a[ai] = avf()
  43. } else if avf, ok := av.(Stringer); ok {
  44. a[ai] = avf.LogString()
  45. } else if avf, ok := av.(fmt.GoStringer); ok {
  46. a[ai] = avf.GoString()
  47. } else if avf, ok := av.(fmt.Stringer); ok {
  48. a[ai] = avf.String()
  49. }
  50. }
  51. return a
  52. }
  53. // Trace log
  54. func Trace(a ...interface{}) {
  55. if level <= TRACE {
  56. _, path, line, _ := runtime.Caller(1)
  57. _, file := filepath.Split(path)
  58. fmt.Printf("TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  59. }
  60. }
  61. // Tracef log
  62. func Tracef(format string, a ...interface{}) {
  63. if level <= TRACE {
  64. _, path, line, _ := runtime.Caller(1)
  65. _, file := filepath.Split(path)
  66. fmt.Printf("TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  67. }
  68. }
  69. // Debug log
  70. func Debug(a ...interface{}) {
  71. if level <= DEBUG {
  72. _, path, line, _ := runtime.Caller(1)
  73. _, file := filepath.Split(path)
  74. fmt.Printf("DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  75. }
  76. }
  77. // Debugf log
  78. func Debugf(format string, a ...interface{}) {
  79. if level <= DEBUG {
  80. _, path, line, _ := runtime.Caller(1)
  81. _, file := filepath.Split(path)
  82. fmt.Printf("DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  83. }
  84. }
  85. // Info log
  86. func Info(a ...interface{}) {
  87. if level <= INFO {
  88. _, path, line, _ := runtime.Caller(1)
  89. _, file := filepath.Split(path)
  90. fmt.Printf("INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  91. }
  92. }
  93. // Infof log
  94. func Infof(format string, a ...interface{}) {
  95. if level <= INFO {
  96. _, path, line, _ := runtime.Caller(1)
  97. _, file := filepath.Split(path)
  98. fmt.Printf("INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  99. }
  100. }
  101. // Warning log
  102. func Warning(a ...interface{}) {
  103. if level <= WARNING {
  104. _, path, line, _ := runtime.Caller(1)
  105. _, file := filepath.Split(path)
  106. fmt.Printf("WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  107. }
  108. }
  109. // Warningf log
  110. func Warningf(format string, a ...interface{}) {
  111. if level <= WARNING {
  112. _, path, line, _ := runtime.Caller(1)
  113. _, file := filepath.Split(path)
  114. fmt.Printf("WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  115. }
  116. }
  117. // Error log
  118. func Error(a ...interface{}) {
  119. if level <= ERROR {
  120. _, path, line, _ := runtime.Caller(1)
  121. _, file := filepath.Split(path)
  122. fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  123. }
  124. }
  125. // Errorf log
  126. func Errorf(format string, a ...interface{}) {
  127. if level <= ERROR {
  128. _, path, line, _ := runtime.Caller(1)
  129. _, file := filepath.Split(path)
  130. fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  131. }
  132. }