log.go 3.4 KB

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