log.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package log
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "strings"
  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. // IsDebugEnable func
  26. func IsDebugEnable() bool {
  27. return level <= DEBUG
  28. }
  29. // Trace log
  30. func Trace(a ...interface{}) {
  31. if level <= TRACE {
  32. for i := 1; i > 0 && i < 100; i++ {
  33. _, path, _, _ := runtime.Caller(i)
  34. if path != "" {
  35. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  36. _, path, line, _ := runtime.Caller(i + 1)
  37. _, file := filepath.Split(path)
  38. fmt.Fprintf(os.Stderr, "TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
  39. return
  40. }
  41. } else {
  42. i = -999
  43. }
  44. }
  45. _, path, line, _ := runtime.Caller(1)
  46. _, file := filepath.Split(path)
  47. fmt.Printf("TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
  48. }
  49. }
  50. // Tracef log
  51. func Tracef(format string, a ...interface{}) {
  52. if level <= TRACE {
  53. for i := 1; i > 0 && i < 100; i++ {
  54. _, path, _, _ := runtime.Caller(i)
  55. if path != "" {
  56. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  57. _, path, line, _ := runtime.Caller(i + 1)
  58. _, file := filepath.Split(path)
  59. fmt.Fprintf(os.Stderr, "TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
  60. return
  61. }
  62. } else {
  63. i = -999
  64. }
  65. }
  66. _, path, line, _ := runtime.Caller(1)
  67. _, file := filepath.Split(path)
  68. fmt.Printf("TRACE: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
  69. }
  70. }
  71. // Debug log
  72. func Debug(a ...interface{}) {
  73. if level <= DEBUG {
  74. for i := 1; i > 0 && i < 100; i++ {
  75. _, path, _, _ := runtime.Caller(i)
  76. if path != "" {
  77. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  78. _, path, line, _ := runtime.Caller(i + 1)
  79. _, file := filepath.Split(path)
  80. fmt.Fprintf(os.Stderr, "DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
  81. return
  82. }
  83. } else {
  84. i = -999
  85. }
  86. }
  87. _, path, line, _ := runtime.Caller(1)
  88. _, file := filepath.Split(path)
  89. fmt.Printf("DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
  90. }
  91. }
  92. // Debugf log
  93. func Debugf(format string, a ...interface{}) {
  94. if level <= DEBUG {
  95. for i := 1; i > 0 && i < 100; i++ {
  96. _, path, _, _ := runtime.Caller(i)
  97. if path != "" {
  98. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  99. _, path, line, _ := runtime.Caller(i + 1)
  100. _, file := filepath.Split(path)
  101. fmt.Fprintf(os.Stderr, "DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
  102. return
  103. }
  104. } else {
  105. i = -999
  106. }
  107. }
  108. _, path, line, _ := runtime.Caller(1)
  109. _, file := filepath.Split(path)
  110. fmt.Printf("DEBUG: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
  111. }
  112. }
  113. // Info log
  114. func Info(a ...interface{}) {
  115. if level <= INFO {
  116. for i := 1; i > 0 && i < 100; i++ {
  117. _, path, _, _ := runtime.Caller(i)
  118. if path != "" {
  119. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  120. _, path, line, _ := runtime.Caller(i + 1)
  121. _, file := filepath.Split(path)
  122. fmt.Fprintf(os.Stderr, "INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
  123. return
  124. }
  125. } else {
  126. i = -999
  127. }
  128. }
  129. _, path, line, _ := runtime.Caller(1)
  130. _, file := filepath.Split(path)
  131. fmt.Printf("INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
  132. }
  133. }
  134. // Infof log
  135. func Infof(format string, a ...interface{}) {
  136. if level <= INFO {
  137. for i := 1; i > 0 && i < 100; i++ {
  138. _, path, _, _ := runtime.Caller(i)
  139. if path != "" {
  140. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  141. _, path, line, _ := runtime.Caller(i + 1)
  142. _, file := filepath.Split(path)
  143. fmt.Fprintf(os.Stderr, "INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
  144. return
  145. }
  146. } else {
  147. i = -999
  148. }
  149. }
  150. _, path, line, _ := runtime.Caller(1)
  151. _, file := filepath.Split(path)
  152. fmt.Printf("INFO: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
  153. }
  154. }
  155. // Warning log
  156. func Warning(a ...interface{}) {
  157. if level <= WARNING {
  158. for i := 1; i > 0 && i < 100; i++ {
  159. _, path, _, _ := runtime.Caller(i)
  160. if path != "" {
  161. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  162. _, path, line, _ := runtime.Caller(i + 1)
  163. _, file := filepath.Split(path)
  164. fmt.Fprintf(os.Stderr, "WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
  165. return
  166. }
  167. } else {
  168. i = -999
  169. }
  170. }
  171. _, path, line, _ := runtime.Caller(1)
  172. _, file := filepath.Split(path)
  173. fmt.Printf("WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
  174. }
  175. }
  176. // Warningf log
  177. func Warningf(format string, a ...interface{}) {
  178. if level <= WARNING {
  179. for i := 1; i > 0 && i < 100; i++ {
  180. _, path, _, _ := runtime.Caller(i)
  181. if path != "" {
  182. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  183. _, path, line, _ := runtime.Caller(i + 1)
  184. _, file := filepath.Split(path)
  185. fmt.Fprintf(os.Stderr, "WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
  186. return
  187. }
  188. } else {
  189. i = -999
  190. }
  191. }
  192. _, path, line, _ := runtime.Caller(1)
  193. _, file := filepath.Split(path)
  194. fmt.Printf("WARN: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
  195. }
  196. }
  197. // Error log
  198. func Error(a ...interface{}) {
  199. if level <= ERROR {
  200. for i := 1; i > 0 && i < 100; i++ {
  201. _, path, _, _ := runtime.Caller(i)
  202. if path != "" {
  203. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  204. _, path, line, _ := runtime.Caller(i + 1)
  205. _, file := filepath.Split(path)
  206. fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
  207. return
  208. }
  209. } else {
  210. i = -999
  211. }
  212. }
  213. _, path, line, _ := runtime.Caller(1)
  214. _, file := filepath.Split(path)
  215. fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprint(a...))
  216. }
  217. }
  218. // Errorf log
  219. func Errorf(format string, a ...interface{}) {
  220. if level <= ERROR {
  221. for i := 1; i > 0 && i < 100; i++ {
  222. _, path, _, _ := runtime.Caller(i)
  223. if path != "" {
  224. if strings.HasSuffix(path, "/libexec/src/runtime/panic.go") {
  225. _, path, line, _ := runtime.Caller(i + 1)
  226. _, file := filepath.Split(path)
  227. fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
  228. return
  229. }
  230. } else {
  231. i = -999
  232. }
  233. }
  234. _, path, line, _ := runtime.Caller(1)
  235. _, file := filepath.Split(path)
  236. fmt.Fprintf(os.Stderr, "ERROR: %s %v:%v: %s\n", time.Now().Format("01:04:05.000"), file, line, fmt.Sprintf(format, a...))
  237. }
  238. }