log.go 7.1 KB

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