本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

483 lines
9.8KB

  1. // Copyright (c) 2016 Couchbase, Inc.
  2. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  3. // except in compliance with the License. You may obtain a copy of the License at
  4. // http://www.apache.org/licenses/LICENSE-2.0
  5. // Unless required by applicable law or agreed to in writing, software distributed under the
  6. // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  7. // either express or implied. See the License for the specific language governing permissions
  8. // and limitations under the License.
  9. package logging
  10. import (
  11. "os"
  12. "runtime"
  13. "strings"
  14. "sync"
  15. )
  16. type Level int
  17. const (
  18. NONE = Level(iota) // Disable all logging
  19. FATAL // System is in severe error state and has to abort
  20. SEVERE // System is in severe error state and cannot recover reliably
  21. ERROR // System is in error state but can recover and continue reliably
  22. WARN // System approaching error state, or is in a correct but undesirable state
  23. INFO // System-level events and status, in correct states
  24. REQUEST // Request-level events, with request-specific rlevel
  25. TRACE // Trace detailed system execution, e.g. function entry / exit
  26. DEBUG // Debug
  27. )
  28. type LogEntryFormatter int
  29. const (
  30. TEXTFORMATTER = LogEntryFormatter(iota)
  31. JSONFORMATTER
  32. KVFORMATTER
  33. UNIFORMFORMATTER
  34. )
  35. func (level Level) String() string {
  36. return _LEVEL_NAMES[level]
  37. }
  38. var _LEVEL_NAMES = []string{
  39. DEBUG: "DEBUG",
  40. TRACE: "TRACE",
  41. REQUEST: "REQUEST",
  42. INFO: "INFO",
  43. WARN: "WARN",
  44. ERROR: "ERROR",
  45. SEVERE: "SEVERE",
  46. FATAL: "FATAL",
  47. NONE: "NONE",
  48. }
  49. var _LEVEL_MAP = map[string]Level{
  50. "debug": DEBUG,
  51. "trace": TRACE,
  52. "request": REQUEST,
  53. "info": INFO,
  54. "warn": WARN,
  55. "error": ERROR,
  56. "severe": SEVERE,
  57. "fatal": FATAL,
  58. "none": NONE,
  59. }
  60. func ParseLevel(name string) (level Level, ok bool) {
  61. level, ok = _LEVEL_MAP[strings.ToLower(name)]
  62. return
  63. }
  64. /*
  65. Pair supports logging of key-value pairs. Keys beginning with _ are
  66. reserved for the logger, e.g. _time, _level, _msg, and _rlevel. The
  67. Pair APIs are designed to avoid heap allocation and garbage
  68. collection.
  69. */
  70. type Pairs []Pair
  71. type Pair struct {
  72. Name string
  73. Value interface{}
  74. }
  75. /*
  76. Map allows key-value pairs to be specified using map literals or data
  77. structures. For example:
  78. Errorm(msg, Map{...})
  79. Map incurs heap allocation and garbage collection, so the Pair APIs
  80. should be preferred.
  81. */
  82. type Map map[string]interface{}
  83. // Logger provides a common interface for logging libraries
  84. type Logger interface {
  85. /*
  86. These APIs write all the given pairs in addition to standard logger keys.
  87. */
  88. Logp(level Level, msg string, kv ...Pair)
  89. Debugp(msg string, kv ...Pair)
  90. Tracep(msg string, kv ...Pair)
  91. Requestp(rlevel Level, msg string, kv ...Pair)
  92. Infop(msg string, kv ...Pair)
  93. Warnp(msg string, kv ...Pair)
  94. Errorp(msg string, kv ...Pair)
  95. Severep(msg string, kv ...Pair)
  96. Fatalp(msg string, kv ...Pair)
  97. /*
  98. These APIs write the fields in the given kv Map in addition to standard logger keys.
  99. */
  100. Logm(level Level, msg string, kv Map)
  101. Debugm(msg string, kv Map)
  102. Tracem(msg string, kv Map)
  103. Requestm(rlevel Level, msg string, kv Map)
  104. Infom(msg string, kv Map)
  105. Warnm(msg string, kv Map)
  106. Errorm(msg string, kv Map)
  107. Severem(msg string, kv Map)
  108. Fatalm(msg string, kv Map)
  109. /*
  110. These APIs only write _msg, _time, _level, and other logger keys. If
  111. the msg contains other fields, use the Pair or Map APIs instead.
  112. */
  113. Logf(level Level, fmt string, args ...interface{})
  114. Debugf(fmt string, args ...interface{})
  115. Tracef(fmt string, args ...interface{})
  116. Requestf(rlevel Level, fmt string, args ...interface{})
  117. Infof(fmt string, args ...interface{})
  118. Warnf(fmt string, args ...interface{})
  119. Errorf(fmt string, args ...interface{})
  120. Severef(fmt string, args ...interface{})
  121. Fatalf(fmt string, args ...interface{})
  122. /*
  123. These APIs control the logging level
  124. */
  125. SetLevel(Level) // Set the logging level
  126. Level() Level // Get the current logging level
  127. }
  128. var logger Logger = nil
  129. var curLevel Level = DEBUG // initially set to never skip
  130. var loggerMutex sync.RWMutex
  131. // All the methods below first acquire the mutex (mostly in exclusive mode)
  132. // and only then check if logging at the current level is enabled.
  133. // This introduces a fair bottleneck for those log entries that should be
  134. // skipped (the majority, at INFO or below levels)
  135. // We try to predict here if we should lock the mutex at all by caching
  136. // the current log level: while dynamically changing logger, there might
  137. // be the odd entry skipped as the new level is cached.
  138. // Since we seem to never change the logger, this is not an issue.
  139. func skipLogging(level Level) bool {
  140. if logger == nil {
  141. return true
  142. }
  143. return level > curLevel
  144. }
  145. func SetLogger(newLogger Logger) {
  146. loggerMutex.Lock()
  147. defer loggerMutex.Unlock()
  148. logger = newLogger
  149. if logger == nil {
  150. curLevel = NONE
  151. } else {
  152. curLevel = newLogger.Level()
  153. }
  154. }
  155. func Logp(level Level, msg string, kv ...Pair) {
  156. if skipLogging(level) {
  157. return
  158. }
  159. loggerMutex.Lock()
  160. defer loggerMutex.Unlock()
  161. logger.Logp(level, msg, kv...)
  162. }
  163. func Debugp(msg string, kv ...Pair) {
  164. if skipLogging(DEBUG) {
  165. return
  166. }
  167. loggerMutex.Lock()
  168. defer loggerMutex.Unlock()
  169. logger.Debugp(msg, kv...)
  170. }
  171. func Tracep(msg string, kv ...Pair) {
  172. if skipLogging(TRACE) {
  173. return
  174. }
  175. loggerMutex.Lock()
  176. defer loggerMutex.Unlock()
  177. logger.Tracep(msg, kv...)
  178. }
  179. func Requestp(rlevel Level, msg string, kv ...Pair) {
  180. if skipLogging(REQUEST) {
  181. return
  182. }
  183. loggerMutex.Lock()
  184. defer loggerMutex.Unlock()
  185. logger.Requestp(rlevel, msg, kv...)
  186. }
  187. func Infop(msg string, kv ...Pair) {
  188. if skipLogging(INFO) {
  189. return
  190. }
  191. loggerMutex.Lock()
  192. defer loggerMutex.Unlock()
  193. logger.Infop(msg, kv...)
  194. }
  195. func Warnp(msg string, kv ...Pair) {
  196. if skipLogging(WARN) {
  197. return
  198. }
  199. loggerMutex.Lock()
  200. defer loggerMutex.Unlock()
  201. logger.Warnp(msg, kv...)
  202. }
  203. func Errorp(msg string, kv ...Pair) {
  204. if skipLogging(ERROR) {
  205. return
  206. }
  207. loggerMutex.Lock()
  208. defer loggerMutex.Unlock()
  209. logger.Errorp(msg, kv...)
  210. }
  211. func Severep(msg string, kv ...Pair) {
  212. if skipLogging(SEVERE) {
  213. return
  214. }
  215. loggerMutex.Lock()
  216. defer loggerMutex.Unlock()
  217. logger.Severep(msg, kv...)
  218. }
  219. func Fatalp(msg string, kv ...Pair) {
  220. if skipLogging(FATAL) {
  221. return
  222. }
  223. loggerMutex.Lock()
  224. defer loggerMutex.Unlock()
  225. logger.Fatalp(msg, kv...)
  226. }
  227. func Logm(level Level, msg string, kv Map) {
  228. if skipLogging(level) {
  229. return
  230. }
  231. loggerMutex.Lock()
  232. defer loggerMutex.Unlock()
  233. logger.Logm(level, msg, kv)
  234. }
  235. func Debugm(msg string, kv Map) {
  236. if skipLogging(DEBUG) {
  237. return
  238. }
  239. loggerMutex.Lock()
  240. defer loggerMutex.Unlock()
  241. logger.Debugm(msg, kv)
  242. }
  243. func Tracem(msg string, kv Map) {
  244. if skipLogging(TRACE) {
  245. return
  246. }
  247. loggerMutex.Lock()
  248. defer loggerMutex.Unlock()
  249. logger.Tracem(msg, kv)
  250. }
  251. func Requestm(rlevel Level, msg string, kv Map) {
  252. if skipLogging(REQUEST) {
  253. return
  254. }
  255. loggerMutex.Lock()
  256. defer loggerMutex.Unlock()
  257. logger.Requestm(rlevel, msg, kv)
  258. }
  259. func Infom(msg string, kv Map) {
  260. if skipLogging(INFO) {
  261. return
  262. }
  263. loggerMutex.Lock()
  264. defer loggerMutex.Unlock()
  265. logger.Infom(msg, kv)
  266. }
  267. func Warnm(msg string, kv Map) {
  268. if skipLogging(WARN) {
  269. return
  270. }
  271. loggerMutex.Lock()
  272. defer loggerMutex.Unlock()
  273. logger.Warnm(msg, kv)
  274. }
  275. func Errorm(msg string, kv Map) {
  276. if skipLogging(ERROR) {
  277. return
  278. }
  279. loggerMutex.Lock()
  280. defer loggerMutex.Unlock()
  281. logger.Errorm(msg, kv)
  282. }
  283. func Severem(msg string, kv Map) {
  284. if skipLogging(SEVERE) {
  285. return
  286. }
  287. loggerMutex.Lock()
  288. defer loggerMutex.Unlock()
  289. logger.Severem(msg, kv)
  290. }
  291. func Fatalm(msg string, kv Map) {
  292. if skipLogging(FATAL) {
  293. return
  294. }
  295. loggerMutex.Lock()
  296. defer loggerMutex.Unlock()
  297. logger.Fatalm(msg, kv)
  298. }
  299. func Logf(level Level, fmt string, args ...interface{}) {
  300. if skipLogging(level) {
  301. return
  302. }
  303. loggerMutex.Lock()
  304. defer loggerMutex.Unlock()
  305. logger.Logf(level, fmt, args...)
  306. }
  307. func Debugf(fmt string, args ...interface{}) {
  308. if skipLogging(DEBUG) {
  309. return
  310. }
  311. loggerMutex.Lock()
  312. defer loggerMutex.Unlock()
  313. logger.Debugf(fmt, args...)
  314. }
  315. func Tracef(fmt string, args ...interface{}) {
  316. if skipLogging(TRACE) {
  317. return
  318. }
  319. loggerMutex.Lock()
  320. defer loggerMutex.Unlock()
  321. logger.Tracef(fmt, args...)
  322. }
  323. func Requestf(rlevel Level, fmt string, args ...interface{}) {
  324. if skipLogging(REQUEST) {
  325. return
  326. }
  327. loggerMutex.Lock()
  328. defer loggerMutex.Unlock()
  329. logger.Requestf(rlevel, fmt, args...)
  330. }
  331. func Infof(fmt string, args ...interface{}) {
  332. if skipLogging(INFO) {
  333. return
  334. }
  335. loggerMutex.Lock()
  336. defer loggerMutex.Unlock()
  337. logger.Infof(fmt, args...)
  338. }
  339. func Warnf(fmt string, args ...interface{}) {
  340. if skipLogging(WARN) {
  341. return
  342. }
  343. loggerMutex.Lock()
  344. defer loggerMutex.Unlock()
  345. logger.Warnf(fmt, args...)
  346. }
  347. func Errorf(fmt string, args ...interface{}) {
  348. if skipLogging(ERROR) {
  349. return
  350. }
  351. loggerMutex.Lock()
  352. defer loggerMutex.Unlock()
  353. logger.Errorf(fmt, args...)
  354. }
  355. func Severef(fmt string, args ...interface{}) {
  356. if skipLogging(SEVERE) {
  357. return
  358. }
  359. loggerMutex.Lock()
  360. defer loggerMutex.Unlock()
  361. logger.Severef(fmt, args...)
  362. }
  363. func Fatalf(fmt string, args ...interface{}) {
  364. if skipLogging(FATAL) {
  365. return
  366. }
  367. loggerMutex.Lock()
  368. defer loggerMutex.Unlock()
  369. logger.Fatalf(fmt, args...)
  370. }
  371. func SetLevel(level Level) {
  372. loggerMutex.Lock()
  373. defer loggerMutex.Unlock()
  374. logger.SetLevel(level)
  375. curLevel = level
  376. }
  377. func LogLevel() Level {
  378. loggerMutex.RLock()
  379. defer loggerMutex.RUnlock()
  380. return logger.Level()
  381. }
  382. func Stackf(level Level, fmt string, args ...interface{}) {
  383. if skipLogging(level) {
  384. return
  385. }
  386. buf := make([]byte, 1<<16)
  387. n := runtime.Stack(buf, false)
  388. s := string(buf[0:n])
  389. loggerMutex.Lock()
  390. defer loggerMutex.Unlock()
  391. logger.Logf(level, fmt, args...)
  392. logger.Logf(level, s)
  393. }
  394. func init() {
  395. logger := NewLogger(os.Stderr, INFO, TEXTFORMATTER)
  396. SetLogger(logger)
  397. }
上海开阖软件有限公司 沪ICP备12045867号-1