spdlog_setup
spdlog_setup copied to clipboard
Pattern takes the last entry
Hello.
It seems that different loggers cannot have different patterns anymore (they could previously, but not in the present version of the code, I updated yesterday after a few months). I couldn't spot in which commit the difference appeared.
Here is a code to reproduce the issue (root writes its output with a 'simple' pattern where it should have a 'complex' one). Apparently, the last "pattern" found in the config file is used for every 'logger' used with "console". On the contrary, the debug level works well.
#include "spdlog_setup/conf.h"
#include <iostream>
#include <string>
int main() {
try {
// spdlog_setup::setup_error thrown if file not found
spdlog_setup::from_file("log_conf.toml");
// assumes that root logger has been initialized
auto logger = spdlog::get("root");
logger->trace("trace message");
logger->debug("debug message");
logger->info("info message");
auto logger2 = spdlog::get("other");
logger2->trace("trace message");
logger2->debug("debug message");
logger2->info("info message");
// ...
} catch (const spdlog_setup::setup_error & exception) {
// ...
} catch (const std::exception & exception) {
}
}
with toml file :
[[sink]]
name = "console"
type = "stdout_sink_mt"
[[pattern]]
name = "complex"
value = "[%Y-%m-%d %T,%e %n] <%l> %v"
[[pattern]]
name = "simple"
value = "%n %v"
[[logger]]
name = "root"
sinks = ["console"]
level = "trace"
pattern = "complex"
[[logger]]
name = "other"
sinks = ["console"]
level = "debug"
pattern = "simple"
outputs
root trace message
root debug message
root info message
other debug message
other info message
Hi sorry for the late reply, I have tested the above and this is indeed true.
The last tag that works is v0.3.0-alpha.1
, but this requires a pre-v1.x spdlog
set-up. If you prefer to get a working logging output as priority and do not require spdlog
v1.x
, then the easiest way to do so is to:
git checkout v0.3.0-alpha
git submodule init && git submodule update --recursive
mkdir -p build
cd build
cmake ..
cmake --build .
sudo cmake --build . --target install
This will give you spdlog_setup
v0.3.0-alpha.1
+ spdlog
v0.16.3
in /usr/local/include
(be careful this might overwrite existing spdlog
files), which I have tested to be working with your given example.
Meanwhile I will investigate if I have committed a bug (or might be upstream) at the current tag. Thanks for reporting the find!
So I have investigated the issue, this is due to the changes made to spdlog
sinks in particular since v1.0.0
see here, where sinks are now allowed to have their own formats.
The code that causes this is here: https://github.com/gabime/spdlog/blob/v1.0.0/include/spdlog/details/logger_impl.h#L40-L51, so now setting format on the logger basically just forwards and sets all its associated sinks with the given format, which consequently also means that the ordering of setting the format on the loggers actually now matters if they share some common sinks, which is the case in your given TOML example. I must say having both loggers and sinks to be allowed setting format makes the situation pretty complicated, in particular this makes the config even less declarative.
Unfortunately currently spdlog_setup
doesn't support setting format on the sinks, which would likely become the recommended practice when it gets implemented. For now, to get back the behavior, you would have to set different sinks on each logger as shown below:
[[sink]]
name = "console_simple"
type = "stdout_sink_mt"
[[sink]]
name = "console_complex"
type = "stdout_sink_mt"
[[pattern]]
name = "complex"
value = "[%Y-%m-%d %T,%e %n] <%l> %v"
[[pattern]]
name = "simple"
value = "%n %v"
[[logger]]
name = "other"
sinks = ["console_simple"]
level = "debug"
pattern = "simple"
[[logger]]
name = "root"
sinks = ["console_complex"]
level = "trace"
pattern = "complex"
Hope this clarifies the issue.