log.go 3.6 KB

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