log.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // Dumpf object
  36. func Dumpf(format string, a ...interface{}) string {
  37. return spew.Sprintf(format, a...)
  38. }
  39. // Dump object
  40. func Dump(a ...interface{}) string {
  41. return spew.Sdump(a...)
  42. }
  43. func dump(a ...interface{}) []interface{} {
  44. for ai, av := range a {
  45. if avf, ok := av.(func() string); ok {
  46. a[ai] = avf()
  47. } else if avf, ok := av.(Stringer); ok {
  48. a[ai] = avf.LogString()
  49. } else if avf, ok := av.(fmt.GoStringer); ok {
  50. a[ai] = avf.GoString()
  51. } else if avf, ok := av.(fmt.Stringer); ok {
  52. a[ai] = avf.String()
  53. }
  54. }
  55. return a
  56. }
  57. // Trace log
  58. func Trace(a ...interface{}) {
  59. if level <= TRACE {
  60. _, path, line, _ := runtime.Caller(1)
  61. _, file := filepath.Split(path)
  62. fmt.Printf("TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  63. }
  64. }
  65. // Tracef log
  66. func Tracef(format string, a ...interface{}) {
  67. if level <= TRACE {
  68. _, path, line, _ := runtime.Caller(1)
  69. _, file := filepath.Split(path)
  70. fmt.Printf("TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  71. }
  72. }
  73. // Debug log
  74. func Debug(a ...interface{}) {
  75. if level <= DEBUG {
  76. _, path, line, _ := runtime.Caller(1)
  77. _, file := filepath.Split(path)
  78. fmt.Printf("DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  79. }
  80. }
  81. // Debugf log
  82. func Debugf(format string, a ...interface{}) {
  83. if level <= DEBUG {
  84. _, path, line, _ := runtime.Caller(1)
  85. _, file := filepath.Split(path)
  86. fmt.Printf("DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  87. }
  88. }
  89. // Info log
  90. func Info(a ...interface{}) {
  91. if level <= INFO {
  92. _, path, line, _ := runtime.Caller(1)
  93. _, file := filepath.Split(path)
  94. fmt.Printf("INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  95. }
  96. }
  97. // Infof log
  98. func Infof(format string, a ...interface{}) {
  99. if level <= INFO {
  100. _, path, line, _ := runtime.Caller(1)
  101. _, file := filepath.Split(path)
  102. fmt.Printf("INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  103. }
  104. }
  105. // Warning log
  106. func Warning(a ...interface{}) {
  107. if level <= WARNING {
  108. _, path, line, _ := runtime.Caller(1)
  109. _, file := filepath.Split(path)
  110. fmt.Printf("WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  111. }
  112. }
  113. // Warningf log
  114. func Warningf(format string, a ...interface{}) {
  115. if level <= WARNING {
  116. _, path, line, _ := runtime.Caller(1)
  117. _, file := filepath.Split(path)
  118. fmt.Printf("WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  119. }
  120. }
  121. // Error log
  122. func Error(a ...interface{}) {
  123. if level <= ERROR {
  124. _, path, line, _ := runtime.Caller(1)
  125. _, file := filepath.Split(path)
  126. fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(dump(a...)...))
  127. }
  128. }
  129. // Errorf log
  130. func Errorf(format string, a ...interface{}) {
  131. if level <= ERROR {
  132. _, path, line, _ := runtime.Caller(1)
  133. _, file := filepath.Split(path)
  134. fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, dump(a...)...))
  135. }
  136. }