Skip to content

Commit

Permalink
Logger factory: make it possible to specify the logging level
Browse files Browse the repository at this point in the history
Change-Id: Ic6f4a82eede242c9249fb502bf567e9539eca8b0
Reviewed-on: https://codereview.kdab.com/c/kdab/kdutils/+/136365
Tested-by: Continuous Integration <[email protected]>
Reviewed-by: Mike Krus <[email protected]>
Reviewed-by: Sean Harmer <[email protected]>
  • Loading branch information
lemirep committed Jan 16, 2024
1 parent be68a7c commit e3f59a0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/KDUtils/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ namespace KDUtils {

Logger::LoggerFactoryFunction Logger::ms_loggerFactory = {};

std::shared_ptr<spdlog::logger> Logger::logger(const std::string &name)
std::shared_ptr<spdlog::logger> Logger::logger(const std::string &name, spdlog::level::level_enum defaultLevel)
{
std::shared_ptr<spdlog::logger> logger;
if (ms_loggerFactory) {
// Use the factory set by the application which should check
// its own spdlog registry first before creating a new logger.
logger = ms_loggerFactory(name);
logger = ms_loggerFactory(name, defaultLevel);
} else {
// No factory set, use the spdlog registry from KDUtils
logger = spdlog::get(name);
Expand All @@ -32,6 +32,7 @@ std::shared_ptr<spdlog::logger> Logger::logger(const std::string &name)
#else
logger = spdlog::stdout_color_mt(name);
#endif
logger->set_level(defaultLevel);
}
}
return logger;
Expand Down
4 changes: 2 additions & 2 deletions src/KDUtils/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ namespace KDUtils {
class KDUTILS_EXPORT Logger
{
public:
static std::shared_ptr<spdlog::logger> logger(const std::string &name);
static std::shared_ptr<spdlog::logger> logger(const std::string &name, spdlog::level::level_enum defaultLevel = spdlog::level::warn);

using LoggerFactoryFunction = std::function<std::shared_ptr<spdlog::logger>(const std::string &)>;
using LoggerFactoryFunction = std::function<std::shared_ptr<spdlog::logger>(const std::string &, spdlog::level::level_enum)>;

static void setLoggerFactory(const LoggerFactoryFunction &factory);
static LoggerFactoryFunction loggerFactory();
Expand Down

0 comments on commit e3f59a0

Please sign in to comment.