How to get the current spdlog level?
Asked Answered
S

3

8

I need to turn off the spdlog level before some code then return it to the previous value after.

How do I get the current level before turning it off?

Saccharin answered 10/5, 2018 at 15:11 Comment(1)
Hello Howaida... please consider accepting an answer to this question.Tales
T
6

Scenario 1: User-constructed logger

If you have a spdlog::logger object you're using (say, my_logger), then:

  • You can obtain the level with: my_logger.level().
  • If you just want to know whether a certain-level message would be logged, then use my_logger.should_log(some_level) where some_level could be, for example spdlog::level::debug.

Scenario 2: The global logger

Now suppose you're using the global logger (e.g. you emit log messages using spdlog::info(), spdlog::error() and such).

spdlog version 1.8.0 and later

You can obtain the global log level with a call to spdlog::get_level() (which is a freestanding function, not a method).

spdlog versions before 1.8.0

You need to get your hand on the implicit logger object - by callingspdlog::default_logger_raw() (it gets you a pointer.) Now just proceed as in Scenario 1 above.

Tales answered 22/7, 2020 at 21:25 Comment(0)
S
4

To get current level of logger use logger::level().

To set new level use logger::set_level().

Samos answered 18/6, 2018 at 9:41 Comment(0)
A
2

There seems now to be a function to get the global logging level:

spdlog::get_level();
Almira answered 9/3, 2021 at 21:16 Comment(3)
spdlog::get_level() was added in v1.8.0.Chumley
I took the liberty of integrating your answer and @Corey's comment into my answer; but +1.Tales
@Tales feel free :)Almira

© 2022 - 2024 — McMap. All rights reserved.