gabime / spdlog

Fast C++ logging library.
Other
23.93k stars 4.5k forks source link

[Question] Is there a way to dynamically adjust the log levels of spdlog during runtime? #3102

Open chenyongquan2 opened 3 months ago

chenyongquan2 commented 3 months ago

I had asked a question earlier: https://github.com/gabime/spdlog/issues/3099

But I still have some questions: During the execution of the program, I modify the value of the environment variable SPDLOG_LEVEL, and I also have a thread that constantly executes spdlog::cfg::load_env_levels(); Can I use the above method to change the log_level of my spdlog at runtime? Or are there other ways to dynamically adjust the output log_level value during program runtime?

In my current project, I expect to dynamically enable the output of trace-level logs only in some exceptional cases (this log printing has a very high frequency, so I hope to print it only when I need it, in order to avoid the situation of overly large logs), while in normal cases, I expect to only output info-level logs.

tt4g commented 3 months ago

Although spdlog::cfg::load_env_levels() can be called multiple times, operations that update and read environment variables at runtime are not guaranteed to be thread-safe by the C language specification. If a bug occurs, it is not the responsibility of spdlog. See: https://stackoverflow.com/a/50479023

Other ways can be implemented freely in your project. spdlog does not restrict you from calling spdlog::logger::set_level() to change the log level.

smohanty92 commented 3 months ago

I use SIGUSR2 signal and do it