logging.nim 31 KB

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