Threadlogging
A thread safe logging library using Nim's own logging module. Creates a channel and listens for incoming messages which are then written to the console. ## How to use Your application needs to initialize the logger once, this opens the channel and starts listening
import threadlogging if isMainModule: # Create a logger to output debug into to the console var consoleLogger = newConsoleLogger(lvlDebug, "[$time] - $levelname: ") # And a file loffer to output errors and above to file var fileLogger = newFileLogger("errors.log", levelThreshold = lvlError, fmtStr = "[$time] - $levelname: ") var loggers = @[consoleLogger, fileLogger] initLogThread(loggers) notice "Here we go, logging is running!" # Go Off and do loads of other things doOtherStuff() # in other procedures, import threadlogging and use info, warn, debug, etc. logging procs # Run `finishLogging()` so all logs get written finishLogging()
Running initLogThread() with no params initializes a standard ConsoleLogger
Types
LogLevel = Level
- Exports Level so a level can be easily sent for initialisation lvlDebug, lvlInfo, lvlNotice, lvlWarn, lvlError, lvlFatal, lvlNone, lvlAll
Procs
proc finishLogging() {....raises: [], tags: [], forbids: [].}
- Joins the log thread which should block the program exiting until all logs are written For example, if all other threads finish processing, they may exit before all logs are written, calling this procedure should prevent this.
proc initLogThread(fmtStr: string = "[$time] - $levelname: "; levelThreshold: Level = lvlDebug; closeOnFatal: bool = true) {. ...raises: [ResourceExhaustedError, ValueError, Exception], tags: [RootEffect], forbids: [].}
- Starts a simple Console Logger with default settings
proc initLogThread(loggers: seq[Logger]; closeOnFatal: bool = true) {. ...raises: [ResourceExhaustedError, ValueError, Exception], tags: [RootEffect], forbids: [].}
- Starts the logging thread by opening a communication channel and creating a thread. Requires a sequence of Loggers to be sent i.e. newConsoleLogger, newFileLogger, newRollingFileLogger
Templates
template debug(args: varargs[string, `$`])
- Writes a Debug Log
template error(args: varargs[string, `$`])
- Writes a Error Log
template fatal(args: varargs[string, `$`])
- Writes a Fatal Log
template info(args: varargs[string, `$`])
- Writes an Info Log
template notice(args: varargs[string, `$`])
- Writes a Notice Log
template sendLog(logLevel: Level; args: varargs[string, `$`])
- Passes the log level and log strings to the logChannel
template warn(args: varargs[string, `$`])
- Writes a Warn Log