wrapper.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright (C) 2010, Kyle Lemons <kyle@kylelemons.net>. All rights reserved.
  2. package log4go
  3. import (
  4. "errors"
  5. "fmt"
  6. "os"
  7. "strings"
  8. )
  9. var (
  10. Global Logger
  11. )
  12. func init() {
  13. Global = NewDefaultLogger(DEBUG)
  14. }
  15. // Wrapper for (*Logger).LoadConfiguration
  16. func LoadConfiguration(filename string) {
  17. Global.LoadConfiguration(filename)
  18. }
  19. // Wrapper for (*Logger).AddFilter
  20. func AddFilter(name string, lvl Level, writer LogWriter) {
  21. Global.AddFilter(name, lvl, writer)
  22. }
  23. // Wrapper for (*Logger).Close (closes and removes all logwriters)
  24. func Close() {
  25. Global.Close()
  26. }
  27. func Crash(args ...interface{}) {
  28. if len(args) > 0 {
  29. Global.intLogf(CRITICAL, strings.Repeat(" %v", len(args))[1:], args...)
  30. }
  31. panic(args)
  32. }
  33. // Logs the given message and crashes the program
  34. func Crashf(format string, args ...interface{}) {
  35. Global.intLogf(CRITICAL, format, args...)
  36. Global.Close() // so that hopefully the messages get logged
  37. panic(fmt.Sprintf(format, args...))
  38. }
  39. // Compatibility with `log`
  40. func Exit(args ...interface{}) {
  41. if len(args) > 0 {
  42. Global.intLogf(ERROR, strings.Repeat(" %v", len(args))[1:], args...)
  43. }
  44. Global.Close() // so that hopefully the messages get logged
  45. os.Exit(0)
  46. }
  47. // Compatibility with `log`
  48. func Exitf(format string, args ...interface{}) {
  49. Global.intLogf(ERROR, format, args...)
  50. Global.Close() // so that hopefully the messages get logged
  51. os.Exit(0)
  52. }
  53. // Compatibility with `log`
  54. func Stderr(args ...interface{}) {
  55. if len(args) > 0 {
  56. Global.intLogf(ERROR, strings.Repeat(" %v", len(args))[1:], args...)
  57. }
  58. }
  59. // Compatibility with `log`
  60. func Stderrf(format string, args ...interface{}) {
  61. Global.intLogf(ERROR, format, args...)
  62. }
  63. // Compatibility with `log`
  64. func Stdout(args ...interface{}) {
  65. if len(args) > 0 {
  66. Global.intLogf(INFO, strings.Repeat(" %v", len(args))[1:], args...)
  67. }
  68. }
  69. // Compatibility with `log`
  70. func Stdoutf(format string, args ...interface{}) {
  71. Global.intLogf(INFO, format, args...)
  72. }
  73. // Send a log message manually
  74. // Wrapper for (*Logger).Log
  75. func Log(lvl Level, source, message string) {
  76. Global.Log(lvl, source, message)
  77. }
  78. // Send a formatted log message easily
  79. // Wrapper for (*Logger).Logf
  80. func Logf(lvl Level, format string, args ...interface{}) {
  81. Global.intLogf(lvl, format, args...)
  82. }
  83. // Send a closure log message
  84. // Wrapper for (*Logger).Logc
  85. func Logc(lvl Level, closure func() string) {
  86. Global.intLogc(lvl, closure)
  87. }
  88. // Utility for finest log messages (see Debug() for parameter explanation)
  89. // Wrapper for (*Logger).Finest
  90. func Finest(arg0 interface{}, args ...interface{}) {
  91. const (
  92. lvl = FINEST
  93. )
  94. switch first := arg0.(type) {
  95. case string:
  96. // Use the string as a format string
  97. Global.intLogf(lvl, first, args...)
  98. case func() string:
  99. // Log the closure (no other arguments used)
  100. Global.intLogc(lvl, first)
  101. default:
  102. // Build a format string so that it will be similar to Sprint
  103. Global.intLogf(lvl, fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...)
  104. }
  105. }
  106. // Utility for fine log messages (see Debug() for parameter explanation)
  107. // Wrapper for (*Logger).Fine
  108. func Fine(arg0 interface{}, args ...interface{}) {
  109. const (
  110. lvl = FINE
  111. )
  112. switch first := arg0.(type) {
  113. case string:
  114. // Use the string as a format string
  115. Global.intLogf(lvl, first, args...)
  116. case func() string:
  117. // Log the closure (no other arguments used)
  118. Global.intLogc(lvl, first)
  119. default:
  120. // Build a format string so that it will be similar to Sprint
  121. Global.intLogf(lvl, fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...)
  122. }
  123. }
  124. // Utility for debug log messages
  125. // When given a string as the first argument, this behaves like Logf but with the DEBUG log level (e.g. the first argument is interpreted as a format for the latter arguments)
  126. // When given a closure of type func()string, this logs the string returned by the closure iff it will be logged. The closure runs at most one time.
  127. // When given anything else, the log message will be each of the arguments formatted with %v and separated by spaces (ala Sprint).
  128. // Wrapper for (*Logger).Debug
  129. func Debug(arg0 interface{}, args ...interface{}) {
  130. const (
  131. lvl = DEBUG
  132. )
  133. switch first := arg0.(type) {
  134. case string:
  135. // Use the string as a format string
  136. Global.intLogf(lvl, first, args...)
  137. case func() string:
  138. // Log the closure (no other arguments used)
  139. Global.intLogc(lvl, first)
  140. default:
  141. // Build a format string so that it will be similar to Sprint
  142. Global.intLogf(lvl, fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...)
  143. }
  144. }
  145. // Utility for trace log messages (see Debug() for parameter explanation)
  146. // Wrapper for (*Logger).Trace
  147. func Trace(arg0 interface{}, args ...interface{}) {
  148. const (
  149. lvl = TRACE
  150. )
  151. switch first := arg0.(type) {
  152. case string:
  153. // Use the string as a format string
  154. Global.intLogf(lvl, first, args...)
  155. case func() string:
  156. // Log the closure (no other arguments used)
  157. Global.intLogc(lvl, first)
  158. default:
  159. // Build a format string so that it will be similar to Sprint
  160. Global.intLogf(lvl, fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...)
  161. }
  162. }
  163. // Utility for info log messages (see Debug() for parameter explanation)
  164. // Wrapper for (*Logger).Info
  165. func Info(arg0 interface{}, args ...interface{}) {
  166. const (
  167. lvl = INFO
  168. )
  169. switch first := arg0.(type) {
  170. case string:
  171. // Use the string as a format string
  172. Global.intLogf(lvl, first, args...)
  173. case func() string:
  174. // Log the closure (no other arguments used)
  175. Global.intLogc(lvl, first)
  176. default:
  177. // Build a format string so that it will be similar to Sprint
  178. Global.intLogf(lvl, fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...)
  179. }
  180. }
  181. // Utility for warn log messages (returns an error for easy function returns) (see Debug() for parameter explanation)
  182. // These functions will execute a closure exactly once, to build the error message for the return
  183. // Wrapper for (*Logger).Warn
  184. func Warn(arg0 interface{}, args ...interface{}) error {
  185. const (
  186. lvl = WARNING
  187. )
  188. switch first := arg0.(type) {
  189. case string:
  190. // Use the string as a format string
  191. Global.intLogf(lvl, first, args...)
  192. return errors.New(fmt.Sprintf(first, args...))
  193. case func() string:
  194. // Log the closure (no other arguments used)
  195. str := first()
  196. Global.intLogf(lvl, "%s", str)
  197. return errors.New(str)
  198. default:
  199. // Build a format string so that it will be similar to Sprint
  200. Global.intLogf(lvl, fmt.Sprint(first)+strings.Repeat(" %v", len(args)), args...)
  201. return errors.New(fmt.Sprint(first) + fmt.Sprintf(strings.Repeat(" %v", len(args)), args...))
  202. }
  203. return nil
  204. }
  205. // Utility for error log messages (returns an error for easy function returns) (see Debug() for parameter explanation)
  206. // These functions will execute a closure exactly once, to build the error message for the return
  207. // Wrapper for (*Logger).Error
  208. func Error(arg0 interface{}, args ...interface{}) error {
  209. const (
  210. lvl = ERROR
  211. )
  212. switch first := arg0.(type) {
  213. case string:
  214. // Use the string as a format string
  215. Global.intLogf(lvl, first, args...)
  216. return errors.New(fmt.Sprintf(first, args...))
  217. case func() string:
  218. // Log the closure (no other arguments used)
  219. str := first()
  220. Global.intLogf(lvl, "%s", str)
  221. return errors.New(str)
  222. default:
  223. // Build a format string so that it will be similar to Sprint
  224. Global.intLogf(lvl, fmt.Sprint(first)+strings.Repeat(" %v", len(args)), args...)
  225. return errors.New(fmt.Sprint(first) + fmt.Sprintf(strings.Repeat(" %v", len(args)), args...))
  226. }
  227. return nil
  228. }
  229. // Utility for critical log messages (returns an error for easy function returns) (see Debug() for parameter explanation)
  230. // These functions will execute a closure exactly once, to build the error message for the return
  231. // Wrapper for (*Logger).Critical
  232. func Critical(arg0 interface{}, args ...interface{}) error {
  233. const (
  234. lvl = CRITICAL
  235. )
  236. switch first := arg0.(type) {
  237. case string:
  238. // Use the string as a format string
  239. Global.intLogf(lvl, first, args...)
  240. return errors.New(fmt.Sprintf(first, args...))
  241. case func() string:
  242. // Log the closure (no other arguments used)
  243. str := first()
  244. Global.intLogf(lvl, "%s", str)
  245. return errors.New(str)
  246. default:
  247. // Build a format string so that it will be similar to Sprint
  248. Global.intLogf(lvl, fmt.Sprint(first)+strings.Repeat(" %v", len(args)), args...)
  249. return errors.New(fmt.Sprint(first) + fmt.Sprintf(strings.Repeat(" %v", len(args)), args...))
  250. }
  251. return nil
  252. }