spdlog
spdlog copied to clipboard
add custom local variable to spdlog
Hi, Is it possible to add local variable to spdlog format that way it will be in its own column? I implemented the custom format, but it seems like it can only add a fixed string for the custom flag, or global variable like this:
void format(const spdlog::details::log_msg &, const std::tm &, spdlog::memory_buf_t &dest) override
{
auto str = std::to_string(global_variable);
dest.append(some_txt.data(), some_txt.data() + some_txt.size());
}
The question is how to do the same with local variable?? That way that in the rest of the files in the projet I can do something like this:
LOG_INFO(LOG_NAME, local_var ,"random_msg");
Right now I got:
[logger_name1] [custom-flag] random_msg
[logger_name2] [custom-flag] blablabla
I want to get:
[logger_name1] [local_var] random_msg
[logger_name2] [ocal_var2] blablabla
Local variables can only be printed by always specifying them as arguments to the logging function.
sepdlog::info("Print local var: {}, var);
I want the local variable to be in different column from the message. So unfortunately the answere you gave me doesnt help for me. Is it possible to override spdlog's built in flags ? I saw this is possible in the spdlog's wiki but dont know how to do it
This seems to be the exact same issue #2546 as the one you opened before, what are you missing?
Correct , its the same. Still I dont know how to implement this/ or how to override spdlog's built in flags
Built-in flags can be overridden by custom formatter.
There is a test to override the builtin flag %t:
https://github.com/gabime/spdlog/blob/05e3a73b162705b37ed464ceb4644addfb03f25b/tests/test_pattern_formatter.cpp#L476-L501
First of all thanks. Can you please elaborate? How can I use this TEST_CASE in my code?
Same as the answer to #2546.