diff --git a/log/include/gz/utils/log/Logger.hh b/log/include/gz/utils/log/Logger.hh index 3e3a491..d2cdd9a 100644 --- a/log/include/gz/utils/log/Logger.hh +++ b/log/include/gz/utils/log/Logger.hh @@ -57,6 +57,10 @@ class GZ_UTILS_LOG_VISIBLE Logger /// \return The spdlog logger. public: [[nodiscard]] std::shared_ptr RawLoggerPtr() const; + /// \brief Set the severity level of the Console sink + /// \param [in] _level Severity level + public: void SetConsoleSinkLevel(spdlog::level::level_enum _level); + /// \brief Implementation Pointer. GZ_UTILS_UNIQUE_IMPL_PTR(dataPtr) }; diff --git a/log/src/Logger.cc b/log/src/Logger.cc index e28ec39..8866c26 100644 --- a/log/src/Logger.cc +++ b/log/src/Logger.cc @@ -25,6 +25,11 @@ namespace gz::utils::log { +namespace { + /// \brief Default log format + /// Example output + constexpr std::string_view kDefaultLogFormat{"%^(%Y-%m-%d %T.%e) [%l] %v%$"}; +} /// \brief Private data for the Logger class. class Logger::Implementation { @@ -33,6 +38,9 @@ class Logger::Implementation public: explicit Implementation(const std::string &_loggerName) : consoleSink(std::make_shared()), sinks(std::make_shared()), + formatter(std::make_unique( + kDefaultLogFormat.data(), + spdlog::pattern_time_type::local, std::string(""))), logger(std::make_shared(_loggerName, sinks)) { } @@ -46,6 +54,9 @@ class Logger::Implementation /// \brief A sink distribution storing multiple sinks. std::shared_ptr sinks {nullptr}; + /// \brief Common formatter for both all sinks + std::unique_ptr formatter; + /// \brief The underlying spdlog logger. std::shared_ptr logger {nullptr}; }; @@ -58,13 +69,10 @@ Logger::Logger(const std::string &_loggerName) this->dataPtr->sinks->add_sink(this->dataPtr->consoleSink); // Configure the logger. - this->dataPtr->logger->set_level(spdlog::level::err); + this->dataPtr->consoleSink->set_level(spdlog::level::err); this->dataPtr->logger->flush_on(spdlog::level::err); - // Disable eol. - auto f = std::make_unique( - "%+", spdlog::pattern_time_type::local, std::string("")); - this->dataPtr->logger->set_formatter(std::move(f)); + this->dataPtr->logger->set_formatter(this->dataPtr->formatter->clone()); } ///////////////////////////////////////////////// @@ -77,6 +85,8 @@ void Logger::SetLogDestination(const std::string &_filename) { this->dataPtr->fileSink = std::make_shared(_filename, true); + this->dataPtr->fileSink->set_formatter(this->dataPtr->formatter->clone()); + this->dataPtr->fileSink->set_level(spdlog::level::trace); this->dataPtr->sinks->add_sink(this->dataPtr->fileSink); } } @@ -103,4 +113,13 @@ std::shared_ptr Logger::RawLoggerPtr() const return this->dataPtr->logger; } +///////////////////////////////////////////////// +void Logger::SetConsoleSinkLevel(spdlog::level::level_enum _level) +{ + if (this->dataPtr->consoleSink) + { + this->dataPtr->consoleSink->set_level(_level); + } +} + } // namespace gz::utils::log