logging.nim 31 KB

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