log.go 3.2 KB

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