Search code examples
multithreadingloggingnim-langprologue

Prologue -Webframework - How to set thread-local variables for logging when compiling with `--threads:on` flag?


I am writing a web-application in nim, using the web-framework prologue and the ORM norm. I've found that the log-messages of prologue and norm that normally appear in the terminal when starting up the application disappear, when you compile with the --threads:on flag.

That is because log-message-handlers and log levels are set as thread-local variables, so when a new thread is created the log-level must be set for that thread again etc.

However, prologue is the one instantiating the threads, so how do I properly set this up for every thread that prologue creates?


Solution

  • I found the answer thanks to the help of prologue's creator: xflywind.

    The answer is prologue-events. When prologue creates a thread, it triggers a list of procs, so called events, that are registered on startup. All you need to do is define an event that sets the log-level and provides a handler.

    proc setLoggingLevel() =
      addHandler(newConsoleLogger())
      logging.setLogFilter(lvlDebug)
    
    let 
      event = initEvent(setLoggingLevel)
    
    var
      app = newApp(settings = settings, startup = @[event])