PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wrapper.go

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