log.go 3.5 KB

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