This module implements a simple logger. It has been designed to be as simple as possible to avoid bloat, if this library does not fullfill your needs, write your own.
Format strings support the following variables which must be prefixed with the dollar operator ($):
Operator | Output |
---|---|
$date | Current date |
$time | Current time |
$app | os.getAppFilename() |
The following example demonstrates logging to three different handlers simultaneously:
var L = newConsoleLogger() var fL = newFileLogger("test.log", fmtStr = verboseFmtStr) var rL = newRollingFileLogger("rolling.log", fmtStr = verboseFmtStr) handlers.add(L) handlers.add(fL) handlers.add(rL) info("920410:52 accepted") warn("4 8 15 16 23 4-- Error") error("922044:16 SYSTEM FAILURE") fatal("SYSTEM FAILURE SYSTEM FAILURE")
Types
TLevel = enum lvlAll, ## all levels active lvlDebug, ## debug level (and any above) active lvlInfo, ## info level (and any above) active lvlWarn, ## warn level (and any above) active lvlError, ## error level (and any above) active lvlFatal, ## fatal level (and any above) active lvlNone ## no levels active
- logging level
PLogger = ref object of PObject levelThreshold*: TLevel ## only messages of level >= levelThreshold ## should be processed fmtStr: string ## = defaultFmtStr by default, see substituteLog for $date etc.
- abstract logger; the base type of all loggers
PConsoleLogger = ref object of PLogger
- logger that writes the messages to the
PFileLogger = ref object of PLogger f: TFile
- logger that writes the messages to a file
PRollingFileLogger = ref object of PFileLogger maxLines: int curLine: int baseName: string baseMode: TFileMode logFiles: int
- logger that writes the messages to a file and performs log rotation
Vars
level = lvlAll
- global log filter
handlers: seq[PLogger] = []
- handlers with their own log levels
Consts
LevelNames: array[TLevel, string] = ["DEBUG", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "NONE"]
defaultFmtStr = ""
- default string between log level and message per logger
verboseFmtStr = "$date $time "
Procs
proc defaultFilename(): string {.raises: [], tags: [].}
- Returns the default filename for a logger.
proc newConsoleLogger(levelThreshold = lvlAll; fmtStr = defaultFmtStr): PConsoleLogger {. raises: [], tags: [].}
- Creates a new console logger. This logger logs to the console.
proc newFileLogger(filename = defaultFilename(); mode: TFileMode = fmAppend; levelThreshold = lvlAll; fmtStr = defaultFmtStr): PFileLogger {. raises: [E_Base, EIO], tags: [].}
- Creates a new file logger. This logger logs to a file.
proc newRollingFileLogger(filename = defaultFilename(); mode: TFileMode = fmReadWrite; levelThreshold = lvlAll; fmtStr = defaultFmtStr; maxLines = 1000): PRollingFileLogger {. raises: [E_Base, EIO, EOverflow], tags: [FReadIO].}
- Creates a new rolling file logger. Once a file reaches maxLines lines a new log file will be started and the old will be renamed.
Methods
method log(logger: PLogger; level: TLevel; frmt: string; args: varargs[string, `$`]) {.raises: [E_Base], tags: [FTime, FWriteIO, FReadIO].}
- Override this method in custom loggers. Default implementation does nothing.
method log(logger: PConsoleLogger; level: TLevel; frmt: string; args: varargs[string, `$`]) {.raises: [EIO, EInvalidValue], tags: [FWriteIO, FReadIO, FTime].}
- Logs to the console using logger only.
method log(logger: PFileLogger; level: TLevel; frmt: string; args: varargs[string, `$`]) {.raises: [EIO, EInvalidValue], tags: [FWriteIO, FReadIO, FTime].}
- Logs to a file using logger only.
method log(logger: PRollingFileLogger; level: TLevel; frmt: string; args: varargs[string, `$`]) {. raises: [EOS, E_Base, EIO, EInvalidValue], tags: [FReadIO, FWriteIO].}
- Logs to a file using rolling logger only.
Templates
template log(level: TLevel; frmt: string; args: varargs[string, `$`])
- Logs a message to all registered handlers at the given level.
template debug(frmt: string; args: varargs[string, `$`])
- Logs a debug message to all registered handlers.
template info(frmt: string; args: varargs[string, `$`])
- Logs an info message to all registered handlers.
template warn(frmt: string; args: varargs[string, `$`])
- Logs a warning message to all registered handlers.
template error(frmt: string; args: varargs[string, `$`])
- Logs an error message to all registered handlers.
template fatal(frmt: string; args: varargs[string, `$`])
- Logs a fatal error message to all registered handlers.