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 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).
Templates
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).