meowmail/utils/logger

Logger is a simple and efficient logging library for Nim, designed to provide a flexible and easy-to-use logging solution for applications of all sizes. It supports logging to both files and the console, with customizable log levels, namespaces, and formatting options.

This is a modified version of the original Logger library, from Miqueas Martínez Nelson "darltrash" López.

This modified version makes use of a read-write lock to ensure thread safety when multiple threads are logging simultaneously.

License zlib | (c) 2026 Miqueas Martínez & Nelson López.

Modified by George Lemon for Supranim Framework | https://supranim.com

Types

InstantiationInfo = tuple[filename: string, line: int, column: int]
LogFormat = enum
  fmtText, fmtJson
Logger = ref object
  filePrefix*: TimeFormat
  namespace*: string
  exitOnError*: bool
  logToFile*: bool
  logToConsole*: bool
  defaultLogLevel*: LogLevel
  minLevel*: LogLevel
  format*: LogFormat
  maxFileSize*: int
  maxFiles*: int
LogLevel = enum
  OTHER, TRACE, INFO, DEBUG, WARN, ERROR, FATAL

Vars

logitGlobal: Logger = nil
Process-wide logger instance. Installed once during server startup via setGlobalLogger; safe to read from any thread afterwards (the Logger itself is internally synchronized). Always nil-check before use — CLI-only code paths never install one.

Procs

proc `$`(lvl: LogLevel): string {....raises: [], tags: [], forbids: [].}
proc finish(self: var Logger) {.inline, ...raises: [], tags: [], forbids: [].}
proc header(self: Logger; message: string) {....raises: [IOError, ValueError],
    tags: [TimeEffect, WriteIOEffect], forbids: [].}
proc logsFolder(self: Logger): string {.inline, ...raises: [], tags: [],
                                        forbids: [].}
proc logsFolder=(self: var Logger; newLogsFolder: string) {.
    ...raises: [IOError, ValueError], tags: [ReadDirEffect], forbids: [].}
proc newLogger(logsFolder = getTempDir(); namespace = "Logger";
               defaultLogLevel = OTHER; logToFile = true; logToConsole = false;
               exitOnError = false; filePrefix = initTimeFormat("YYYY-MM-dd");
               minLevel = OTHER; format = fmtText; maxFileSize = 0; maxFiles = 0): Logger {.
    ...raises: [IOError], tags: [ReadDirEffect], forbids: [].}
Initializes a Logger instance with the specified configuration.
proc setGlobalLogger(logger: Logger) {.inline, ...raises: [], tags: [], forbids: [].}
Install logger as the process-wide logger (see logitGlobal).
proc start(self: var Logger) {....raises: [IOError, ValueError],
                               tags: [TimeEffect, WriteIOEffect], forbids: [].}
Prepares Logger for logging using the given Logger instance. This function assumes that Logger has everything ready to start logging, that means you must have set the path property.

Templates

template debug(self: Logger; message = ""; autoNewLine = true)
template error(self: Logger; message = ""; autoNewLine = true)
template fatal(self: Logger; message = ""; autoNewLine = true)
template info(self: Logger; message = ""; autoNewLine = true)
template log(self: Logger; level: LogLevel; message: string; autoNewLine = true;
             someInfo = none(InstantiationInfo))
Logs a message with the specified log level. The message can be a string or any expression that evaluates to a string. The autoNewLine parameter determines whether to automatically add a newline after the message (default is true).
template log(self: Logger; message = ""; autoNewLine = true)
template other(self: Logger; message = ""; autoNewLine = true)
template trace(self: Logger; message = ""; autoNewLine = true)
template warn(self: Logger; message = ""; autoNewLine = true)