plog
plog copied to clipboard
Multiple loggers with dynamic instance Ids
Hi,
I am trying to init a logger with a dynamic instance Id but it looks like it does not work.
int instanceId = 1;
plog::Logger<instanceId>& test = plog::init<loggerId>(plog::verbose, &consoleAppender);
Seems that instanceId must be a constant.
Is there a workaround to have a dynamic number of loggers?
Many thanks :)
Hi!
No, by design they are compile-time constants. What is your use-case so you need a dynamic instance ids?
Thank you very much for your answer.
I want to run a certain number of different scenarios (for which I don't know at compiler time how many they will be). For each scenario, I want to log what happens in its own corresponding file.
Like Scenario 1 -> log in file1.txt Scenario 2 -> log in file2.txt etc
If you run them sequentially then you can change the output file name on the beginning of each scenario.
Unfortunately I want to run them in parallel. Thinking of changing the output file name each time I want to log, and adding a mutex on top of that to be sure to write in the right file.
I am not a fan of this approach and it might affect the performances if I log too much. But I may give it a try.
Probably there can be a mix of static and dynamic instance ids. I'll think about possible ways to implement it.
Nice! I tried my idea but it was a bad one. It started creating appenders each time I wanted to log.
@bogoss2012 Dynamic logger instances is ready. Take a look at the sample.
Thanks a lot!
Sample works fine. I am having issues to use dynamic instances branch in my own project... trying to figure out how to fix them.
I have two questions:
- Do you need a LoggerContext class in your sample?
- Why do you not use the init function to init the logger?
Many thanks
LoggerContext holds all objects required by the logger. At least you need one appender and one logger. It's up to you how to store them. LoggerContext is just one of the possible solutions.
Init function initializes a logger with the static instance id. So it can't be used.
Ok that's clear. So as long as the instance I pass has a get() function that ends up on my logger, it works.
I solved my issue, which was totally on my side, the object manipulating the logger was badly managed.
It works great! I am gonna use that branch now untill it is merged and released. Thanks a lot!