From 4f2b3d52f947ed0628b6fe37a024e74018cdcbb2 Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 16 Jul 2025 08:59:51 +0300 Subject: [PATCH] Update README.md (#3437) * Update README.md add example showcasing 2 loggers and `spdlog::set_level()` which set level not only to default logger, but to all registed loggers * Update README.md * simplify * simplify --- README.md | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e8c8c31d..a558a6e4 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ int main() spdlog::info("Positional args are {1} {0}..", "too", "supported"); spdlog::info("{:<30}", "left aligned"); - spdlog::set_level(spdlog::level::debug); // Set global log level to debug + spdlog::set_level(spdlog::level::debug); // Set *global* log level to debug spdlog::debug("This message should be displayed.."); // change log pattern @@ -224,7 +224,7 @@ void binary_example() ```c++ // create a logger with 2 targets, with different log levels and formats. -// The console will show only warnings or errors, while the file will log all. +// The console will show only warnings or errors, while the file will log all. void multi_sink_example() { auto console_sink = std::make_shared(); @@ -241,6 +241,29 @@ void multi_sink_example() } ``` +--- +#### Register several loggers - change global level +```c++ + +// Creation of loggers. Set levels to all registered loggers. +void set_level_example() +{ + auto logger1 = spdlog::basic_logger_mt("logger1", "logs/logger1.txt"); + auto logger2 = spdlog::basic_logger_mt("logger2", "logs/logger2.txt"); + + spdlog::set_default_logger(logger2); + spdlog::default_logger()->set_level(spdlog::level::trace); // set level for the default logger (logger2) to trace + + spdlog::trace("trace message to the logger2 (specified as default)"); + + spdlog::set_level(spdlog::level::off) // (sic!) set level for *all* registered loggers to off (disable) + + logger1.warn("warn message will not appear because the level set to off"); + logger2.warn("warn message will not appear because the level set to off"); + spdlog::warn("warn message will not appear because the level set to off"); +} +``` + --- #### User-defined callbacks about log events ```c++