logging.nim 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Andreas Rumpf, Dominik Picheta
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements a simple logger.
  10. ##
  11. ## It has been designed to be as simple as possible to avoid bloat.
  12. ## If this library does not fulfill your needs, write your own.
  13. ##
  14. ## Basic usage
  15. ## ===========
  16. ##
  17. ## To get started, first create a logger:
  18. ##
  19. ## .. code-block::
  20. ## import std/logging
  21. ##
  22. ## var logger = newConsoleLogger()
  23. ##
  24. ## The logger that was created above logs to the console, but this module
  25. ## also provides loggers that log to files, such as the
  26. ## `FileLogger<#FileLogger>`_. Creating custom loggers is also possible by
  27. ## inheriting from the `Logger<#Logger>`_ type.
  28. ##
  29. ## Once a logger has been created, call its `log proc
  30. ## <#log.e,ConsoleLogger,Level,varargs[string,]>`_ to log a message:
  31. ##
  32. ## .. code-block::
  33. ## logger.log(lvlInfo, "a log message")
  34. ## # Output: INFO a log message
  35. ##
  36. ## The ``INFO`` within the output is the result of a format string being
  37. ## prepended to the message, and it will differ depending on the message's
  38. ## level. Format strings are `explained in more detail
  39. ## here<#basic-usage-format-strings>`_.
  40. ##
  41. ## There are six logging levels: debug, info, notice, warn, error, and fatal.
  42. ## They are described in more detail within the `Level enum's documentation
  43. ## <#Level>`_. A message is logged if its level is at or above both the logger's
  44. ## ``levelThreshold`` field and the global log filter. The latter can be changed
  45. ## with the `setLogFilter proc<#setLogFilter,Level>`_.
  46. ##
  47. ## .. warning::
  48. ## For loggers that log to a console or to files, only error and fatal
  49. ## messages will cause their output buffers to be flushed immediately by default.
  50. ## set ``flushThreshold`` when creating the logger to change this.
  51. ##
  52. ## Handlers
  53. ## --------
  54. ##
  55. ## When using multiple loggers, calling the log proc for each logger can
  56. ## become repetitive. Instead of doing that, register each logger that will be
  57. ## used with the `addHandler proc<#addHandler,Logger>`_, which is demonstrated
  58. ## in the following example:
  59. ##
  60. ## .. code-block::
  61. ## import std/logging
  62. ##
  63. ## var consoleLog = newConsoleLogger()
  64. ## var fileLog = newFileLogger("errors.log", levelThreshold=lvlError)
  65. ## var rollingLog = newRollingFileLogger("rolling.log")
  66. ##
  67. ## addHandler(consoleLog)
  68. ## addHandler(fileLog)
  69. ## addHandler(rollingLog)
  70. ##
  71. ## After doing this, use either the `log template
  72. ## <#log.t,Level,varargs[string,]>`_ or one of the level-specific templates,
  73. ## such as the `error template<#error.t,varargs[string,]>`_, to log messages
  74. ## to all registered handlers at once.
  75. ##
  76. ## .. code-block::
  77. ## # This example uses the loggers created above
  78. ## log(lvlError, "an error occurred")
  79. ## error("an error occurred") # Equivalent to the above line
  80. ## info("something normal happened") # Will not be written to errors.log
  81. ##
  82. ## Note that a message's level is still checked against each handler's
  83. ## ``levelThreshold`` and the global log filter.
  84. ##
  85. ## Format strings
  86. ## --------------
  87. ##
  88. ## Log messages are prefixed with format strings. These strings contain
  89. ## placeholders for variables, such as ``$time``, that are replaced with their
  90. ## corresponding values, such as the current time, before they are prepended to
  91. ## a log message. Characters that are not part of variables are unaffected.
  92. ##
  93. ## The format string used by a logger can be specified by providing the `fmtStr`
  94. ## argument when creating the logger or by setting its `fmtStr` field afterward.
  95. ## If not specified, the `default format string<#defaultFmtStr>`_ is used.
  96. ##
  97. ## The following variables, which must be prefixed with a dollar sign (``$``),
  98. ## are available:
  99. ##
  100. ## ============ =======================
  101. ## Variable Output
  102. ## ============ =======================
  103. ## $date Current date
  104. ## $time Current time
  105. ## $datetime $dateT$time
  106. ## $app `os.getAppFilename()<os.html#getAppFilename>`_
  107. ## $appname Base name of ``$app``
  108. ## $appdir Directory name of ``$app``
  109. ## $levelid First letter of log level
  110. ## $levelname Log level name
  111. ## ============ =======================
  112. ##
  113. ## Note that ``$app``, ``$appname``, and ``$appdir`` are not supported when
  114. ## using the JavaScript backend.
  115. ##
  116. ## The following example illustrates how to use format strings:
  117. ##
  118. ## .. code-block::
  119. ## import std/logging
  120. ##
  121. ## var logger = newConsoleLogger(fmtStr="[$time] - $levelname: ")
  122. ## logger.log(lvlInfo, "this is a message")
  123. ## # Output: [19:50:13] - INFO: this is a message
  124. ##
  125. ## Notes when using multiple threads
  126. ## ---------------------------------
  127. ##
  128. ## There are a few details to keep in mind when using this module within
  129. ## multiple threads:
  130. ## * The global log filter is actually a thread-local variable, so it needs to
  131. ## be set in each thread that uses this module.
  132. ## * The list of registered handlers is also a thread-local variable. If a
  133. ## handler will be used in multiple threads, it needs to be registered in
  134. ## each of those threads.
  135. ##
  136. ## See also
  137. ## ========
  138. ## * `strutils module<strutils.html>`_ for common string functions
  139. ## * `strformat module<strformat.html>`_ for string interpolation and formatting
  140. ## * `strscans module<strscans.html>`_ for ``scanf`` and ``scanp`` macros, which
  141. ## offer easier substring extraction than regular expressions
  142. import strutils, times
  143. when not defined(js):
  144. import os
  145. when defined(nimPreviewSlimSystem):
  146. import std/syncio
  147. type
  148. Level* = enum ## \
  149. ## Enumeration of logging levels.
  150. ##
  151. ## Debug messages represent the lowest logging level, and fatal error
  152. ## messages represent the highest logging level. ``lvlAll`` can be used
  153. ## to enable all messages, while ``lvlNone`` can be used to disable all
  154. ## messages.
  155. ##
  156. ## Typical usage for each logging level, from lowest to highest, is
  157. ## described below:
  158. ##
  159. ## * **Debug** - debugging information helpful only to developers
  160. ## * **Info** - anything associated with normal operation and without
  161. ## any particular importance
  162. ## * **Notice** - more important information that users should be
  163. ## notified about
  164. ## * **Warn** - impending problems that require some attention
  165. ## * **Error** - error conditions that the application can recover from
  166. ## * **Fatal** - fatal errors that prevent the application from continuing
  167. ##
  168. ## It is completely up to the application how to utilize each level.
  169. ##
  170. ## Individual loggers have a ``levelThreshold`` field that filters out
  171. ## any messages with a level lower than the threshold. There is also
  172. ## a global filter that applies to all log messages, and it can be changed
  173. ## using the `setLogFilter proc<#setLogFilter,Level>`_.
  174. lvlAll, ## All levels active
  175. lvlDebug, ## Debug level and above are active
  176. lvlInfo, ## Info level and above are active
  177. lvlNotice, ## Notice level and above are active
  178. lvlWarn, ## Warn level and above are active
  179. lvlError, ## Error level and above are active
  180. lvlFatal, ## Fatal level and above are active
  181. lvlNone ## No levels active; nothing is logged
  182. const
  183. LevelNames*: array[Level, string] = [
  184. "DEBUG", "DEBUG", "INFO", "NOTICE", "WARN", "ERROR", "FATAL", "NONE"
  185. ] ## Array of strings representing each logging level.
  186. defaultFmtStr* = "$levelname " ## The default format string.
  187. verboseFmtStr* = "$levelid, [$datetime] -- $appname: " ## \
  188. ## A more verbose format string.
  189. ##
  190. ## This string can be passed as the ``frmStr`` argument to procs that create
  191. ## new loggers, such as the `newConsoleLogger proc<#newConsoleLogger>`_.
  192. ##
  193. ## If a different format string is preferred, refer to the
  194. ## `documentation about format strings<#basic-usage-format-strings>`_
  195. ## for more information, including a list of available variables.
  196. defaultFlushThreshold = when NimMajor >= 2:
  197. when defined(nimV1LogFlushBehavior): lvlError else: lvlAll
  198. else:
  199. when defined(nimFlushAllLogs): lvlAll else: lvlError
  200. ## The threshold above which log messages to file-like loggers
  201. ## are automatically flushed.
  202. ##
  203. ## By default, only error and fatal messages are logged,
  204. ## but defining ``-d:nimFlushAllLogs`` will make all levels be flushed
  205. type
  206. Logger* = ref object of RootObj
  207. ## The abstract base type of all loggers.
  208. ##
  209. ## Custom loggers should inherit from this type. They should also provide
  210. ## their own implementation of the
  211. ## `log method<#log.e,Logger,Level,varargs[string,]>`_.
  212. ##
  213. ## See also:
  214. ## * `ConsoleLogger<#ConsoleLogger>`_
  215. ## * `FileLogger<#FileLogger>`_
  216. ## * `RollingFileLogger<#RollingFileLogger>`_
  217. levelThreshold*: Level ## Only messages that are at or above this
  218. ## threshold will be logged
  219. fmtStr*: string ## Format string to prepend to each log message;
  220. ## defaultFmtStr is the default
  221. ConsoleLogger* = ref object of Logger
  222. ## A logger that writes log messages to the console.
  223. ##
  224. ## Create a new ``ConsoleLogger`` with the `newConsoleLogger proc
  225. ## <#newConsoleLogger>`_.
  226. ##
  227. ## See also:
  228. ## * `FileLogger<#FileLogger>`_
  229. ## * `RollingFileLogger<#RollingFileLogger>`_
  230. useStderr*: bool ## If true, writes to stderr; otherwise, writes to stdout
  231. flushThreshold*: Level ## Only messages that are at or above this
  232. ## threshold will be flushed immediately
  233. when not defined(js):
  234. type
  235. FileLogger* = ref object of Logger
  236. ## A logger that writes log messages to a file.
  237. ##
  238. ## Create a new ``FileLogger`` with the `newFileLogger proc
  239. ## <#newFileLogger,File>`_.
  240. ##
  241. ## **Note:** This logger is not available for the JavaScript backend.
  242. ##
  243. ## See also:
  244. ## * `ConsoleLogger<#ConsoleLogger>`_
  245. ## * `RollingFileLogger<#RollingFileLogger>`_
  246. file*: File ## The wrapped file
  247. flushThreshold*: Level ## Only messages that are at or above this
  248. ## threshold will be flushed immediately
  249. RollingFileLogger* = ref object of FileLogger
  250. ## A logger that writes log messages to a file while performing log
  251. ## rotation.
  252. ##
  253. ## Create a new ``RollingFileLogger`` with the `newRollingFileLogger proc
  254. ## <#newRollingFileLogger,FileMode,Positive,int>`_.
  255. ##
  256. ## **Note:** This logger is not available for the JavaScript backend.
  257. ##
  258. ## See also:
  259. ## * `ConsoleLogger<#ConsoleLogger>`_
  260. ## * `FileLogger<#FileLogger>`_
  261. maxLines: int # maximum number of lines
  262. curLine: int
  263. baseName: string # initial filename
  264. baseMode: FileMode # initial file mode
  265. logFiles: int # how many log files already created, e.g. basename.1, basename.2...
  266. bufSize: int # size of output buffer (-1: use system defaults, 0: unbuffered, >0: fixed buffer size)
  267. var
  268. level {.threadvar.}: Level ## global log filter
  269. handlers {.threadvar.}: seq[Logger] ## handlers with their own log levels
  270. proc substituteLog*(frmt: string, level: Level,
  271. args: varargs[string, `$`]): string =
  272. ## Formats a log message at the specified level with the given format string.
  273. ##
  274. ## The `format variables<#basic-usage-format-strings>`_ present within
  275. ## ``frmt`` will be replaced with the corresponding values before being
  276. ## prepended to ``args`` and returned.
  277. ##
  278. ## Unless you are implementing a custom logger, there is little need to call
  279. ## this directly. Use either a logger's log method or one of the logging
  280. ## templates.
  281. ##
  282. ## See also:
  283. ## * `log method<#log.e,ConsoleLogger,Level,varargs[string,]>`_
  284. ## for the ConsoleLogger
  285. ## * `log method<#log.e,FileLogger,Level,varargs[string,]>`_
  286. ## for the FileLogger
  287. ## * `log method<#log.e,RollingFileLogger,Level,varargs[string,]>`_
  288. ## for the RollingFileLogger
  289. ## * `log template<#log.t,Level,varargs[string,]>`_
  290. runnableExamples:
  291. doAssert substituteLog(defaultFmtStr, lvlInfo, "a message") == "INFO a message"
  292. doAssert substituteLog("$levelid - ", lvlError, "an error") == "E - an error"
  293. doAssert substituteLog("$levelid", lvlDebug, "error") == "Derror"
  294. var msgLen = 0
  295. for arg in args:
  296. msgLen += arg.len
  297. result = newStringOfCap(frmt.len + msgLen + 20)
  298. var i = 0
  299. while i < frmt.len:
  300. if frmt[i] != '$':
  301. result.add(frmt[i])
  302. inc(i)
  303. else:
  304. inc(i)
  305. var v = ""
  306. let app = when defined(js): "" else: getAppFilename()
  307. while i < frmt.len and frmt[i] in IdentChars:
  308. v.add(toLowerAscii(frmt[i]))
  309. inc(i)
  310. case v
  311. of "date": result.add(getDateStr())
  312. of "time": result.add(getClockStr())
  313. of "datetime": result.add(getDateStr() & "T" & getClockStr())
  314. of "app": result.add(app)
  315. of "appdir":
  316. when not defined(js): result.add(app.splitFile.dir)
  317. of "appname":
  318. when not defined(js): result.add(app.splitFile.name)
  319. of "levelid": result.add(LevelNames[level][0])
  320. of "levelname": result.add(LevelNames[level])
  321. else: discard
  322. for arg in args:
  323. result.add(arg)
  324. method log*(logger: Logger, level: Level, args: varargs[string, `$`]) {.
  325. raises: [Exception], gcsafe,
  326. tags: [RootEffect], base.} =
  327. ## Override this method in custom loggers. The default implementation does
  328. ## nothing.
  329. ##
  330. ## See also:
  331. ## * `log method<#log.e,ConsoleLogger,Level,varargs[string,]>`_
  332. ## for the ConsoleLogger
  333. ## * `log method<#log.e,FileLogger,Level,varargs[string,]>`_
  334. ## for the FileLogger
  335. ## * `log method<#log.e,RollingFileLogger,Level,varargs[string,]>`_
  336. ## for the RollingFileLogger
  337. ## * `log template<#log.t,Level,varargs[string,]>`_
  338. discard
  339. method log*(logger: ConsoleLogger, level: Level, args: varargs[string, `$`]) =
  340. ## Logs to the console with the given `ConsoleLogger<#ConsoleLogger>`_ only.
  341. ##
  342. ## This method ignores the list of registered handlers.
  343. ##
  344. ## Whether the message is logged depends on both the ConsoleLogger's
  345. ## ``levelThreshold`` field and the global log filter set using the
  346. ## `setLogFilter proc<#setLogFilter,Level>`_.
  347. ##
  348. ## **Note:** Only error and fatal messages will cause the output buffer
  349. ## to be flushed immediately by default. Set ``flushThreshold`` when creating
  350. ## the logger to change this.
  351. ##
  352. ## See also:
  353. ## * `log method<#log.e,FileLogger,Level,varargs[string,]>`_
  354. ## for the FileLogger
  355. ## * `log method<#log.e,RollingFileLogger,Level,varargs[string,]>`_
  356. ## for the RollingFileLogger
  357. ## * `log template<#log.t,Level,varargs[string,]>`_
  358. ##
  359. ## **Examples:**
  360. ##
  361. ## .. code-block::
  362. ## var consoleLog = newConsoleLogger()
  363. ## consoleLog.log(lvlInfo, "this is a message")
  364. ## consoleLog.log(lvlError, "error code is: ", 404)
  365. if level >= logging.level and level >= logger.levelThreshold:
  366. let ln = substituteLog(logger.fmtStr, level, args)
  367. when defined(js):
  368. let cln = ln.cstring
  369. case level
  370. of lvlDebug: {.emit: "console.debug(`cln`);".}
  371. of lvlInfo: {.emit: "console.info(`cln`);".}
  372. of lvlWarn: {.emit: "console.warn(`cln`);".}
  373. of lvlError: {.emit: "console.error(`cln`);".}
  374. else: {.emit: "console.log(`cln`);".}
  375. else:
  376. try:
  377. var handle = stdout
  378. if logger.useStderr:
  379. handle = stderr
  380. writeLine(handle, ln)
  381. if level >= logger.flushThreshold: flushFile(handle)
  382. except IOError:
  383. discard
  384. proc newConsoleLogger*(levelThreshold = lvlAll, fmtStr = defaultFmtStr,
  385. useStderr = false, flushThreshold = defaultFlushThreshold): ConsoleLogger =
  386. ## Creates a new `ConsoleLogger<#ConsoleLogger>`_.
  387. ##
  388. ## By default, log messages are written to ``stdout``. If ``useStderr`` is
  389. ## true, they are written to ``stderr`` instead.
  390. ##
  391. ## For the JavaScript backend, log messages are written to the console,
  392. ## and ``useStderr`` is ignored.
  393. ##
  394. ## See also:
  395. ## * `newFileLogger proc<#newFileLogger,File>`_ that uses a file handle
  396. ## * `newFileLogger proc<#newFileLogger,FileMode,int>`_
  397. ## that accepts a filename
  398. ## * `newRollingFileLogger proc<#newRollingFileLogger,FileMode,Positive,int>`_
  399. ##
  400. ## **Examples:**
  401. ##
  402. ## .. code-block::
  403. ## var normalLog = newConsoleLogger()
  404. ## var formatLog = newConsoleLogger(fmtStr=verboseFmtStr)
  405. ## var errorLog = newConsoleLogger(levelThreshold=lvlError, useStderr=true)
  406. new result
  407. result.fmtStr = fmtStr
  408. result.levelThreshold = levelThreshold
  409. result.flushThreshold = flushThreshold
  410. result.useStderr = useStderr
  411. when not defined(js):
  412. method log*(logger: FileLogger, level: Level, args: varargs[string, `$`]) =
  413. ## Logs a message at the specified level using the given
  414. ## `FileLogger<#FileLogger>`_ only.
  415. ##
  416. ## This method ignores the list of registered handlers.
  417. ##
  418. ## Whether the message is logged depends on both the FileLogger's
  419. ## ``levelThreshold`` field and the global log filter set using the
  420. ## `setLogFilter proc<#setLogFilter,Level>`_.
  421. ##
  422. ## **Notes:**
  423. ## * Only error and fatal messages will cause the output buffer
  424. ## to be flushed immediately by default. Set ``flushThreshold`` when creating
  425. ## the logger to change this.
  426. ## * This method is not available for the JavaScript backend.
  427. ##
  428. ## See also:
  429. ## * `log method<#log.e,ConsoleLogger,Level,varargs[string,]>`_
  430. ## for the ConsoleLogger
  431. ## * `log method<#log.e,RollingFileLogger,Level,varargs[string,]>`_
  432. ## for the RollingFileLogger
  433. ## * `log template<#log.t,Level,varargs[string,]>`_
  434. ##
  435. ## **Examples:**
  436. ##
  437. ## .. code-block::
  438. ## var fileLog = newFileLogger("messages.log")
  439. ## fileLog.log(lvlInfo, "this is a message")
  440. ## fileLog.log(lvlError, "error code is: ", 404)
  441. if level >= logging.level and level >= logger.levelThreshold:
  442. writeLine(logger.file, substituteLog(logger.fmtStr, level, args))
  443. if level >= logger.flushThreshold: flushFile(logger.file)
  444. proc defaultFilename*(): string =
  445. ## Returns the filename that is used by default when naming log files.
  446. ##
  447. ## **Note:** This proc is not available for the JavaScript backend.
  448. var (path, name, _) = splitFile(getAppFilename())
  449. result = changeFileExt(path / name, "log")
  450. proc newFileLogger*(file: File,
  451. levelThreshold = lvlAll,
  452. fmtStr = defaultFmtStr,
  453. flushThreshold = defaultFlushThreshold): FileLogger =
  454. ## Creates a new `FileLogger<#FileLogger>`_ that uses the given file handle.
  455. ##
  456. ## **Note:** This proc is not available for the JavaScript backend.
  457. ##
  458. ## See also:
  459. ## * `newConsoleLogger proc<#newConsoleLogger>`_
  460. ## * `newFileLogger proc<#newFileLogger,FileMode,int>`_
  461. ## that accepts a filename
  462. ## * `newRollingFileLogger proc<#newRollingFileLogger,FileMode,Positive,int>`_
  463. ##
  464. ## **Examples:**
  465. ##
  466. ## .. code-block::
  467. ## var messages = open("messages.log", fmWrite)
  468. ## var formatted = open("formatted.log", fmWrite)
  469. ## var errors = open("errors.log", fmWrite)
  470. ##
  471. ## var normalLog = newFileLogger(messages)
  472. ## var formatLog = newFileLogger(formatted, fmtStr=verboseFmtStr)
  473. ## var errorLog = newFileLogger(errors, levelThreshold=lvlError)
  474. new(result)
  475. result.file = file
  476. result.levelThreshold = levelThreshold
  477. result.flushThreshold = flushThreshold
  478. result.fmtStr = fmtStr
  479. proc newFileLogger*(filename = defaultFilename(),
  480. mode: FileMode = fmAppend,
  481. levelThreshold = lvlAll,
  482. fmtStr = defaultFmtStr,
  483. bufSize: int = -1,
  484. flushThreshold = defaultFlushThreshold): FileLogger =
  485. ## Creates a new `FileLogger<#FileLogger>`_ that logs to a file with the
  486. ## given filename.
  487. ##
  488. ## ``bufSize`` controls the size of the output buffer that is used when
  489. ## writing to the log file. The following values can be provided:
  490. ## * ``-1`` - use system defaults
  491. ## * ``0`` - unbuffered
  492. ## * ``> 0`` - fixed buffer size
  493. ##
  494. ## **Note:** This proc is not available for the JavaScript backend.
  495. ##
  496. ## See also:
  497. ## * `newConsoleLogger proc<#newConsoleLogger>`_
  498. ## * `newFileLogger proc<#newFileLogger,File>`_ that uses a file handle
  499. ## * `newRollingFileLogger proc<#newRollingFileLogger,FileMode,Positive,int>`_
  500. ##
  501. ## **Examples:**
  502. ##
  503. ## .. code-block::
  504. ## var normalLog = newFileLogger("messages.log")
  505. ## var formatLog = newFileLogger("formatted.log", fmtStr=verboseFmtStr)
  506. ## var errorLog = newFileLogger("errors.log", levelThreshold=lvlError)
  507. let file = open(filename, mode, bufSize = bufSize)
  508. newFileLogger(file, levelThreshold, fmtStr, flushThreshold)
  509. # ------
  510. proc countLogLines(logger: RollingFileLogger): int =
  511. let fp = open(logger.baseName, fmRead)
  512. for line in fp.lines():
  513. result.inc()
  514. fp.close()
  515. proc countFiles(filename: string): int =
  516. # Example: file.log.1
  517. result = 0
  518. var (dir, name, ext) = splitFile(filename)
  519. if dir == "":
  520. dir = "."
  521. for kind, path in walkDir(dir):
  522. if kind == pcFile:
  523. let llfn = name & ext & ExtSep
  524. if path.extractFilename.startsWith(llfn):
  525. let numS = path.extractFilename[llfn.len .. ^1]
  526. try:
  527. let num = parseInt(numS)
  528. if num > result:
  529. result = num
  530. except ValueError: discard
  531. proc newRollingFileLogger*(filename = defaultFilename(),
  532. mode: FileMode = fmReadWrite,
  533. levelThreshold = lvlAll,
  534. fmtStr = defaultFmtStr,
  535. maxLines: Positive = 1000,
  536. bufSize: int = -1,
  537. flushThreshold = defaultFlushThreshold): RollingFileLogger =
  538. ## Creates a new `RollingFileLogger<#RollingFileLogger>`_.
  539. ##
  540. ## Once the current log file being written to contains ``maxLines`` lines,
  541. ## a new log file will be created, and the old log file will be renamed.
  542. ##
  543. ## ``bufSize`` controls the size of the output buffer that is used when
  544. ## writing to the log file. The following values can be provided:
  545. ## * ``-1`` - use system defaults
  546. ## * ``0`` - unbuffered
  547. ## * ``> 0`` - fixed buffer size
  548. ##
  549. ## **Note:** This proc is not available in the JavaScript backend.
  550. ##
  551. ## See also:
  552. ## * `newConsoleLogger proc<#newConsoleLogger>`_
  553. ## * `newFileLogger proc<#newFileLogger,File>`_ that uses a file handle
  554. ## * `newFileLogger proc<#newFileLogger,FileMode,int>`_
  555. ## that accepts a filename
  556. ##
  557. ## **Examples:**
  558. ##
  559. ## .. code-block::
  560. ## var normalLog = newRollingFileLogger("messages.log")
  561. ## var formatLog = newRollingFileLogger("formatted.log", fmtStr=verboseFmtStr)
  562. ## var shortLog = newRollingFileLogger("short.log", maxLines=200)
  563. ## var errorLog = newRollingFileLogger("errors.log", levelThreshold=lvlError)
  564. new(result)
  565. result.levelThreshold = levelThreshold
  566. result.fmtStr = fmtStr
  567. result.maxLines = maxLines
  568. result.bufSize = bufSize
  569. result.file = open(filename, mode, bufSize = result.bufSize)
  570. result.curLine = 0
  571. result.baseName = filename
  572. result.baseMode = mode
  573. result.flushThreshold = flushThreshold
  574. result.logFiles = countFiles(filename)
  575. if mode == fmAppend:
  576. # We need to get a line count because we will be appending to the file.
  577. result.curLine = countLogLines(result)
  578. proc rotate(logger: RollingFileLogger) =
  579. let (dir, name, ext) = splitFile(logger.baseName)
  580. for i in countdown(logger.logFiles, 0):
  581. let srcSuff = if i != 0: ExtSep & $i else: ""
  582. moveFile(dir / (name & ext & srcSuff),
  583. dir / (name & ext & ExtSep & $(i+1)))
  584. method log*(logger: RollingFileLogger, level: Level, args: varargs[string, `$`]) =
  585. ## Logs a message at the specified level using the given
  586. ## `RollingFileLogger<#RollingFileLogger>`_ only.
  587. ##
  588. ## This method ignores the list of registered handlers.
  589. ##
  590. ## Whether the message is logged depends on both the RollingFileLogger's
  591. ## ``levelThreshold`` field and the global log filter set using the
  592. ## `setLogFilter proc<#setLogFilter,Level>`_.
  593. ##
  594. ## **Notes:**
  595. ## * Only error and fatal messages will cause the output buffer
  596. ## to be flushed immediately by default. Set ``flushThreshold`` when creating
  597. ## the logger to change this.
  598. ## * This method is not available for the JavaScript backend.
  599. ##
  600. ## See also:
  601. ## * `log method<#log.e,ConsoleLogger,Level,varargs[string,]>`_
  602. ## for the ConsoleLogger
  603. ## * `log method<#log.e,FileLogger,Level,varargs[string,]>`_
  604. ## for the FileLogger
  605. ## * `log template<#log.t,Level,varargs[string,]>`_
  606. ##
  607. ## **Examples:**
  608. ##
  609. ## .. code-block::
  610. ## var rollingLog = newRollingFileLogger("messages.log")
  611. ## rollingLog.log(lvlInfo, "this is a message")
  612. ## rollingLog.log(lvlError, "error code is: ", 404)
  613. if level >= logging.level and level >= logger.levelThreshold:
  614. if logger.curLine >= logger.maxLines:
  615. logger.file.close()
  616. rotate(logger)
  617. logger.logFiles.inc
  618. logger.curLine = 0
  619. logger.file = open(logger.baseName, logger.baseMode,
  620. bufSize = logger.bufSize)
  621. writeLine(logger.file, substituteLog(logger.fmtStr, level, args))
  622. if level >= logger.flushThreshold: flushFile(logger.file)
  623. logger.curLine.inc
  624. # --------
  625. proc logLoop(level: Level, args: varargs[string, `$`]) =
  626. for logger in items(handlers):
  627. if level >= logger.levelThreshold:
  628. log(logger, level, args)
  629. template log*(level: Level, args: varargs[string, `$`]) =
  630. ## Logs a message at the specified level to all registered handlers.
  631. ##
  632. ## Whether the message is logged depends on both the FileLogger's
  633. ## `levelThreshold` field and the global log filter set using the
  634. ## `setLogFilter proc<#setLogFilter,Level>`_.
  635. ##
  636. ## **Examples:**
  637. ##
  638. ## .. code-block::
  639. ## var logger = newConsoleLogger()
  640. ## addHandler(logger)
  641. ##
  642. ## log(lvlInfo, "This is an example.")
  643. ##
  644. ## See also:
  645. ## * `debug template<#debug.t,varargs[string,]>`_
  646. ## * `info template<#info.t,varargs[string,]>`_
  647. ## * `notice template<#notice.t,varargs[string,]>`_
  648. ## * `warn template<#warn.t,varargs[string,]>`_
  649. ## * `error template<#error.t,varargs[string,]>`_
  650. ## * `fatal template<#fatal.t,varargs[string,]>`_
  651. bind logLoop
  652. bind `%`
  653. bind logging.level
  654. if level >= logging.level:
  655. logLoop(level, args)
  656. template debug*(args: varargs[string, `$`]) =
  657. ## Logs a debug message to all registered handlers.
  658. ##
  659. ## Debug messages are typically useful to the application developer only,
  660. ## and they are usually disabled in release builds, although this template
  661. ## does not make that distinction.
  662. ##
  663. ## **Examples:**
  664. ##
  665. ## .. code-block::
  666. ## var logger = newConsoleLogger()
  667. ## addHandler(logger)
  668. ##
  669. ## debug("myProc called with arguments: foo, 5")
  670. ##
  671. ## See also:
  672. ## * `log template<#log.t,Level,varargs[string,]>`_
  673. ## * `info template<#info.t,varargs[string,]>`_
  674. ## * `notice template<#notice.t,varargs[string,]>`_
  675. log(lvlDebug, args)
  676. template info*(args: varargs[string, `$`]) =
  677. ## Logs an info message to all registered handlers.
  678. ##
  679. ## Info messages are typically generated during the normal operation
  680. ## of an application and are of no particular importance. It can be useful to
  681. ## aggregate these messages for later analysis.
  682. ##
  683. ## **Examples:**
  684. ##
  685. ## .. code-block::
  686. ## var logger = newConsoleLogger()
  687. ## addHandler(logger)
  688. ##
  689. ## info("Application started successfully.")
  690. ##
  691. ## See also:
  692. ## * `log template<#log.t,Level,varargs[string,]>`_
  693. ## * `debug template<#debug.t,varargs[string,]>`_
  694. ## * `notice template<#notice.t,varargs[string,]>`_
  695. log(lvlInfo, args)
  696. template notice*(args: varargs[string, `$`]) =
  697. ## Logs an notice to all registered handlers.
  698. ##
  699. ## Notices are semantically very similar to info messages, but they are meant
  700. ## to be messages that the user should be actively notified about, depending
  701. ## on the application.
  702. ##
  703. ## **Examples:**
  704. ##
  705. ## .. code-block::
  706. ## var logger = newConsoleLogger()
  707. ## addHandler(logger)
  708. ##
  709. ## notice("An important operation has completed.")
  710. ##
  711. ## See also:
  712. ## * `log template<#log.t,Level,varargs[string,]>`_
  713. ## * `debug template<#debug.t,varargs[string,]>`_
  714. ## * `info template<#info.t,varargs[string,]>`_
  715. log(lvlNotice, args)
  716. template warn*(args: varargs[string, `$`]) =
  717. ## Logs a warning message to all registered handlers.
  718. ##
  719. ## A warning is a non-error message that may indicate impending problems or
  720. ## degraded performance.
  721. ##
  722. ## **Examples:**
  723. ##
  724. ## .. code-block::
  725. ## var logger = newConsoleLogger()
  726. ## addHandler(logger)
  727. ##
  728. ## warn("The previous operation took too long to process.")
  729. ##
  730. ## See also:
  731. ## * `log template<#log.t,Level,varargs[string,]>`_
  732. ## * `error template<#error.t,varargs[string,]>`_
  733. ## * `fatal template<#fatal.t,varargs[string,]>`_
  734. log(lvlWarn, args)
  735. template error*(args: varargs[string, `$`]) =
  736. ## Logs an error message to all registered handlers.
  737. ##
  738. ## Error messages are for application-level error conditions, such as when
  739. ## some user input generated an exception. Typically, the application will
  740. ## continue to run, but with degraded functionality or loss of data, and
  741. ## these effects might be visible to users.
  742. ##
  743. ## **Examples:**
  744. ##
  745. ## .. code-block::
  746. ## var logger = newConsoleLogger()
  747. ## addHandler(logger)
  748. ##
  749. ## error("An exception occurred while processing the form.")
  750. ##
  751. ## See also:
  752. ## * `log template<#log.t,Level,varargs[string,]>`_
  753. ## * `warn template<#warn.t,varargs[string,]>`_
  754. ## * `fatal template<#fatal.t,varargs[string,]>`_
  755. log(lvlError, args)
  756. template fatal*(args: varargs[string, `$`]) =
  757. ## Logs a fatal error message to all registered handlers.
  758. ##
  759. ## Fatal error messages usually indicate that the application cannot continue
  760. ## to run and will exit due to a fatal condition. This template only logs the
  761. ## message, and it is the application's responsibility to exit properly.
  762. ##
  763. ## **Examples:**
  764. ##
  765. ## .. code-block::
  766. ## var logger = newConsoleLogger()
  767. ## addHandler(logger)
  768. ##
  769. ## fatal("Can't open database -- exiting.")
  770. ##
  771. ## See also:
  772. ## * `log template<#log.t,Level,varargs[string,]>`_
  773. ## * `warn template<#warn.t,varargs[string,]>`_
  774. ## * `error template<#error.t,varargs[string,]>`_
  775. log(lvlFatal, args)
  776. proc addHandler*(handler: Logger) =
  777. ## Adds a logger to the list of registered handlers.
  778. ##
  779. ## .. warning:: The list of handlers is a thread-local variable. If the given
  780. ## handler will be used in multiple threads, this proc should be called in
  781. ## each of those threads.
  782. ##
  783. ## See also:
  784. ## * `getHandlers proc<#getHandlers>`_
  785. runnableExamples:
  786. var logger = newConsoleLogger()
  787. addHandler(logger)
  788. doAssert logger in getHandlers()
  789. handlers.add(handler)
  790. proc getHandlers*(): seq[Logger] =
  791. ## Returns a list of all the registered handlers.
  792. ##
  793. ## See also:
  794. ## * `addHandler proc<#addHandler,Logger>`_
  795. return handlers
  796. proc setLogFilter*(lvl: Level) =
  797. ## Sets the global log filter.
  798. ##
  799. ## Messages below the provided level will not be logged regardless of an
  800. ## individual logger's ``levelThreshold``. By default, all messages are
  801. ## logged.
  802. ##
  803. ## .. warning:: The global log filter is a thread-local variable. If logging
  804. ## is being performed in multiple threads, this proc should be called in each
  805. ## thread unless it is intended that different threads should log at different
  806. ## logging levels.
  807. ##
  808. ## See also:
  809. ## * `getLogFilter proc<#getLogFilter>`_
  810. runnableExamples:
  811. setLogFilter(lvlError)
  812. doAssert getLogFilter() == lvlError
  813. level = lvl
  814. proc getLogFilter*(): Level =
  815. ## Gets the global log filter.
  816. ##
  817. ## See also:
  818. ## * `setLogFilter proc<#setLogFilter,Level>`_
  819. return level
  820. # --------------
  821. when not defined(testing) and isMainModule:
  822. var L = newConsoleLogger()
  823. when not defined(js):
  824. var fL = newFileLogger("test.log", fmtStr = verboseFmtStr)
  825. var rL = newRollingFileLogger("rolling.log", fmtStr = verboseFmtStr)
  826. addHandler(fL)
  827. addHandler(rL)
  828. addHandler(L)
  829. for i in 0 .. 25:
  830. info("hello", i)
  831. var nilString: string
  832. info "hello ", nilString