log.go 3.4 KB

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