How to share the same file sink between different loggers (cateogories) #2377
-
Possibly related to this, but I am not sure I grasped whether this is possible or it is not. I know that the idea is to share a common sink between two loggers, but how can I share that sink between different classes? I would like to avoid having to pass it to every class (e.g.: in its constructor). The goal is to have different classes writing to the same log file, but with different categories, for instance:
Any suggestion would be greatly appreciated, thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 11 replies
-
You can get sink from logger. // get another_logger from registry.
auto another_logger = spdlog::get("another");
// get sink by `spdlog::logger::sinks()`.
auto sinks = another_logger->sinks():
// create new_logger.
auto new_logger = std::make_shared<spdlog::logger>("new_logger", sinks.begin(), sinks.end());
// clone logger (share sinks).
auto clone_logger = another_logger->clone("clone_logger"): Wiki: https://github.com/gabime/spdlog/wiki/4.-Sinks#adding-sinks-to-the-logger-after-creation |
Beta Was this translation helpful? Give feedback.
You can get sink from logger.
Wiki: https://github.com/gabime/spdlog/wiki/4.-Sinks#adding-sinks-to-the-logger-after-creation