Per-thread default logger or "current" logger #2755
Replies: 6 comments 20 replies
-
I don't see any difference between the scope logger API and using |
Beta Was this translation helpful? Give feedback.
-
you could register logger per thread with name=thread id and the use spdlog::get(..) when needed. Not pretty but will work. When not needed, call spdlog::drop(logger_name) |
Beta Was this translation helpful? Give feedback.
-
No, but it is not related to your question. You can: <at start of scope>
auto logger = <create logger with thread_id as the name>
spdlog::register_logger(logger);
...
<use the logger from any thread, using spdlog::get(thread_id)>
..
<at end of scope>
spdlog::drop(logger->name()) Or (preferred), just store logger per thread in the thread local storage. |
Beta Was this translation helpful? Give feedback.
-
where is the code? |
Beta Was this translation helpful? Give feedback.
-
Right, your tag solution is elegant and light . Well done 👍🏼 Few comments:
|
Beta Was this translation helpful? Give feedback.
-
The thread id is copied from the call site and passed along the log message to the queue so its fine. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm looking for a solution to implement a concept I call current logger. It is similar to the default logger but local to each thread.
Basically, I want shared pieces of code to log to a specific logger defined somewhere up the call chain without having to pass logger instances down – or in any other way polluting the interface – using the
SPDLOG_<LOG_LEVEL>
macros.In addition, I propose a RAII-style mechanism to set the current logger like so:
Is there an existing solution I've overlooked?
Otherwise, I suggest introducing a new macro to make the
default_logger_
member (effectively)thread_local
and adding the proposed RAII-mechanism.@gabime I'd be happy to submit a PR if you're interested. Some details are to be ironed out but nothing major AFAICT from a cursory look at the code.
Edit: Of course, the initial idea of simply declaring the
default_logger_
memberthread_local
isn't possible. Thus, the change will have to be slightly more invasive than hoped for.Beta Was this translation helpful? Give feedback.
All reactions