PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/wrapper.go

http://github.com/moovweb/log4go
Go | 290 lines | 199 code | 26 blank | 65 comment | 12 complexity | c3d6ee049e8016fa46da98dda2a61659 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. "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 LogLevel, 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 LogLevel, 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 LogLevel, 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 LogLevel, closure func() string) {
  86. Global.intLogc(lvl, closure)
  87. }
  88. // Utility for debug log messages
  89. // 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)
  90. // 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.
  91. // When given anything else, the log message will be each of the arguments formatted with %v and separated by spaces (ala Sprint).
  92. // Wrapper for (*Logger).Debug
  93. func Debug(arg0 interface{}, args ...interface{}) {
  94. const (
  95. lvl = DEBUG
  96. )
  97. switch first := arg0.(type) {
  98. case string:
  99. // Use the string as a format string
  100. Global.intLogf(lvl, first, args...)
  101. case func() string:
  102. // Log the closure (no other arguments used)
  103. Global.intLogc(lvl, first)
  104. default:
  105. // Build a format string so that it will be similar to Sprint
  106. Global.intLogf(lvl, fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...)
  107. }
  108. }
  109. // Utility for info log messages (see Debug() for parameter explanation)
  110. // Wrapper for (*Logger).Info
  111. func Info(arg0 interface{}, args ...interface{}) {
  112. const (
  113. lvl = INFO
  114. )
  115. switch first := arg0.(type) {
  116. case string:
  117. // Use the string as a format string
  118. Global.intLogf(lvl, first, args...)
  119. case func() string:
  120. // Log the closure (no other arguments used)
  121. Global.intLogc(lvl, first)
  122. default:
  123. // Build a format string so that it will be similar to Sprint
  124. Global.intLogf(lvl, fmt.Sprint(arg0)+strings.Repeat(" %v", len(args)), args...)
  125. }
  126. }
  127. // Utility for trace log messages (see Debug() for parameter explanation)
  128. // Wrapper for (*Logger).Trace
  129. func Notice(arg0 interface{}, args ...interface{}) {
  130. const (
  131. lvl = NOTICE
  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 warn log messages (returns an os.Error for easy function returns) (see Debug() for parameter explanation)
  146. // These functions will execute a closure exactly once, to build the error message for the return
  147. // Wrapper for (*Logger).Warn
  148. func Warn(arg0 interface{}, args ...interface{}) error {
  149. const (
  150. lvl = WARNING
  151. )
  152. switch first := arg0.(type) {
  153. case string:
  154. // Use the string as a format string
  155. Global.intLogf(lvl, first, args...)
  156. return errors.New(fmt.Sprintf(first, args...))
  157. case func() string:
  158. // Log the closure (no other arguments used)
  159. str := first()
  160. Global.intLogf(lvl, "%s", str)
  161. return errors.New(str)
  162. default:
  163. // Build a format string so that it will be similar to Sprint
  164. Global.intLogf(lvl, fmt.Sprint(first)+strings.Repeat(" %v", len(args)), args...)
  165. return errors.New(fmt.Sprint(first) + fmt.Sprintf(strings.Repeat(" %v", len(args)), args...))
  166. }
  167. return nil
  168. }
  169. // Utility for error log messages (returns an os.Error for easy function returns) (see Debug() for parameter explanation)
  170. // These functions will execute a closure exactly once, to build the error message for the return
  171. // Wrapper for (*Logger).Error
  172. func Error(arg0 interface{}, args ...interface{}) error {
  173. const (
  174. lvl = ERROR
  175. )
  176. switch first := arg0.(type) {
  177. case string:
  178. // Use the string as a format string
  179. Global.intLogf(lvl, first, args...)
  180. return errors.New(fmt.Sprintf(first, args...))
  181. case func() string:
  182. // Log the closure (no other arguments used)
  183. str := first()
  184. Global.intLogf(lvl, "%s", str)
  185. return errors.New(str)
  186. default:
  187. // Build a format string so that it will be similar to Sprint
  188. Global.intLogf(lvl, fmt.Sprint(first)+strings.Repeat(" %v", len(args)), args...)
  189. return errors.New(fmt.Sprint(first) + fmt.Sprintf(strings.Repeat(" %v", len(args)), args...))
  190. }
  191. return nil
  192. }
  193. // Utility for critical log messages (returns an os.Error for easy function returns) (see Debug() for parameter explanation)
  194. // These functions will execute a closure exactly once, to build the error message for the return
  195. // Wrapper for (*Logger).Critical
  196. func Critical(arg0 interface{}, args ...interface{}) error {
  197. const (
  198. lvl = CRITICAL
  199. )
  200. switch first := arg0.(type) {
  201. case string:
  202. // Use the string as a format string
  203. Global.intLogf(lvl, first, args...)
  204. return errors.New(fmt.Sprintf(first, args...))
  205. case func() string:
  206. // Log the closure (no other arguments used)
  207. str := first()
  208. Global.intLogf(lvl, "%s", str)
  209. return errors.New(str)
  210. default:
  211. // Build a format string so that it will be similar to Sprint
  212. Global.intLogf(lvl, fmt.Sprint(first)+strings.Repeat(" %v", len(args)), args...)
  213. return errors.New(fmt.Sprint(first) + fmt.Sprintf(strings.Repeat(" %v", len(args)), args...))
  214. }
  215. return nil
  216. }
  217. // Utility for alert log messages (returns an os.Error for easy function returns) (see Debug() for parameter explanation)
  218. // These functions will execute a closure exactly once, to build the error message for the return
  219. // Wrapper for (*Logger).Critical
  220. func Alert(arg0 interface{}, args ...interface{}) error {
  221. const (
  222. lvl = ALERT
  223. )
  224. switch first := arg0.(type) {
  225. case string:
  226. // Use the string as a format string
  227. Global.intLogf(lvl, first, args...)
  228. return errors.New(fmt.Sprintf(first, args...))
  229. case func() string:
  230. // Log the closure (no other arguments used)
  231. str := first()
  232. Global.intLogf(lvl, "%s", str)
  233. return errors.New(str)
  234. default:
  235. // Build a format string so that it will be similar to Sprint
  236. Global.intLogf(lvl, fmt.Sprint(first)+strings.Repeat(" %v", len(args)), args...)
  237. return errors.New(fmt.Sprint(first) + fmt.Sprintf(strings.Repeat(" %v", len(args)), args...))
  238. }
  239. return nil
  240. }
  241. // Utility for emergency log messages (returns an os.Error for easy function returns) (see Debug() for parameter explanation)
  242. // These functions will execute a closure exactly once, to build the error message for the return
  243. // Wrapper for (*Logger).Critical
  244. func Emergency(arg0 interface{}, args ...interface{}) error {
  245. const (
  246. lvl = EMERGENCY
  247. )
  248. switch first := arg0.(type) {
  249. case string:
  250. // Use the string as a format string
  251. Global.intLogf(lvl, first, args...)
  252. return errors.New(fmt.Sprintf(first, args...))
  253. case func() string:
  254. // Log the closure (no other arguments used)
  255. str := first()
  256. Global.intLogf(lvl, "%s", str)
  257. return errors.New(str)
  258. default:
  259. // Build a format string so that it will be similar to Sprint
  260. Global.intLogf(lvl, fmt.Sprint(first)+strings.Repeat(" %v", len(args)), args...)
  261. return errors.New(fmt.Sprint(first) + fmt.Sprintf(strings.Repeat(" %v", len(args)), args...))
  262. }
  263. return nil
  264. }