From f52d526e1ee2bad54d0234dc6c8c4855b321d796 Mon Sep 17 00:00:00 2001 From: Sean Farrell Date: Sun, 28 Nov 2021 13:55:14 +0100 Subject: [PATCH] Add example to replace default logger. Close #2193 --- README.md | 14 ++++++++++++++ example/example.cpp | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/README.md b/README.md index d1f228c2..0ad56df5 100644 --- a/README.md +++ b/README.md @@ -391,6 +391,20 @@ void file_events_example() } ``` +--- +#### Replace the Default Logger +```c++ +void replace_default_logger_example() +{ + auto new_logger = spdlog::basic_logger_mt("new_default_logger", "logs/new-default-log.txt", true); + spdlog::set_default_logger(new_logger); + spdlog::set_level(spdlog::level::info); + spdlog::debug("This message should not be displayed!"); + spdlog::set_level(spdlog::level::trace); + spdlog::debug("This message should be displayed.."); +} +``` + --- ## Benchmarks diff --git a/example/example.cpp b/example/example.cpp index 6ba5c4c7..3e33e284 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -23,6 +23,7 @@ void syslog_example(); void udp_example(); void custom_flags_example(); void file_events_example(); +void replace_default_logger_example(); #include "spdlog/spdlog.h" #include "spdlog/cfg/env.h" // support for loading levels from the environment variable @@ -80,6 +81,7 @@ int main(int, char *[]) udp_example(); custom_flags_example(); file_events_example(); + replace_default_logger_example(); // Flush all *registered* loggers using a worker thread every 3 seconds. // note: registered loggers *must* be thread safe for this to work correctly! @@ -326,3 +328,19 @@ void file_events_example() spdlog::logger my_logger("some_logger", file_sink); my_logger.info("Some log line"); } + +void replace_default_logger_example() +{ + // store the old logger so we don't break other examples. + auto old_logger = spdlog::default_logger(); + + auto new_logger = spdlog::basic_logger_mt("new_default_logger", "logs/new-default-log.txt", true); + spdlog::set_default_logger(new_logger); + spdlog::set_level(spdlog::level::info); + spdlog::debug("This message should not be displayed!"); + spdlog::set_level(spdlog::level::trace); + spdlog::debug("This message should be displayed.."); + + spdlog::set_default_logger(old_logger); +} +