No description
Find a file
2023-11-06 16:32:49 +00:00
docs@c1c7d0447c added new docs 2023-11-05 20:33:36 +00:00
scripts docs 2023-11-04 14:48:21 +00:00
src added new docs 2023-11-05 20:33:36 +00:00
tests adjust tests and readme for custom loggers 2023-11-05 20:17:03 +00:00
.gitmodules docs 2023-11-04 14:49:07 +00:00
LICENSE add basic readme and license 2023-11-04 13:05:50 +00:00
readme.md updated readme 2023-11-06 16:32:49 +00:00
threadlogging.log adjust tests and readme for custom loggers 2023-11-05 20:17:03 +00:00
threadlogging.nimble allow for use of custom loggers 2023-11-05 20:06:13 +00:00

Nim 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.

API Reference

What's this for?

Even though you can use Nim's own logging module for logging in threads, you need to either pass a logging var into the thread or start a new Logger for each thread.
This threadlogging module gets around this requirement by starting up a logger in its own thread and opens a channel which any other thread can communicate with. This all happens within the threadlogging module meaning all you need to do is call initLogThread() once, then import threadlogging in any file requiring it where you'll then be able to call the normal info, warn, debug, error, fatal and notice procedures to write logs

How to use

Your application needs to initialize the logger once, this opens the channel and starts listening.

Create loggers as per Nim's logging Reference

import threadlogging

if isMainModule:
  # Create a logger to output debug into to the console
  var consoleLogger = newConsoleLogger(lvlDebug, "[$time] - $levelname: ")
  # And a file logger to output errors and above to file
  var fileLogger = newFileLogger("errors.log", levelThreshold = lvlError, fmtStr = "[$time] - $levelname: ")
  var loggers = @[consoleLogger, fileLogger]
  # Initialize the log thread
  initLogThread(loggers)
  # And you're good to log!
  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()

Then, you can use the standard names of Nim's own logging module to write logs

import threadlogging

proc someOtherThread() =
  sendLog lvlDebug, "You can use the `sendLog` procedure with the log level type"
  debug "Or call the log level type procedure directly"

Defaults to use a consoleLogger

Running initLogThread() with no params initializes a standard ConsoleLogger

Fatal logs close the channel and thread

When a Fatal log is received, the log is written and the thread then closes and also closes the channel. If you don't want this, simply initLogThread with initLogThread(loggers, closeOnFatal = false)

Waiting for logging to finish

If all other threads end, then it's likely not all logs will be written before the application closes.
To prevent this, call the finishLogging() procedure (which is really just a wrapper for joinThread logThread which causes the main thread to block until a Fatal log is written