spdlog icon indicating copy to clipboard operation
spdlog copied to clipboard

add custom local variable to spdlog

Open Omernn23 opened this issue 2 years ago • 7 comments

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

Omernn23 avatar Jan 12 '23 12:01 Omernn23

Local variables can only be printed by always specifying them as arguments to the logging function.

sepdlog::info("Print local var: {}, var);

tt4g avatar Jan 12 '23 13:01 tt4g

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

Omernn23 avatar Jan 12 '23 13:01 Omernn23

This seems to be the exact same issue #2546 as the one you opened before, what are you missing?

tt4g avatar Jan 12 '23 13:01 tt4g

Correct , its the same. Still I dont know how to implement this/ or how to override spdlog's built in flags

Omernn23 avatar Jan 12 '23 14:01 Omernn23

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

tt4g avatar Jan 12 '23 14:01 tt4g

First of all thanks. Can you please elaborate? How can I use this TEST_CASE in my code?

Omernn23 avatar Jan 12 '23 14:01 Omernn23

Same as the answer to #2546.

tt4g avatar Jan 12 '23 20:01 tt4g