Skip to content

Commit

Permalink
Use common formatter for console and file sinks
Browse files Browse the repository at this point in the history
The severity level of the file sink is now set to trace so that
it logs to file regardless of the severity level of the console.

This patch also adds an API to set the severity level of just the
console sink.

Signed-off-by: Addisu Z. Taddese <addisu@openrobotics.org>
  • Loading branch information
azeey committed Aug 28, 2024
1 parent 2a10be0 commit c8c4abb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
4 changes: 4 additions & 0 deletions log/include/gz/utils/log/Logger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class GZ_UTILS_LOG_VISIBLE Logger
/// \return The spdlog logger.
public: [[nodiscard]] std::shared_ptr<spdlog::logger> 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)
};
Expand Down
29 changes: 24 additions & 5 deletions log/src/Logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -33,6 +38,9 @@ class Logger::Implementation
public: explicit Implementation(const std::string &_loggerName)
: consoleSink(std::make_shared<SplitConsoleSink>()),
sinks(std::make_shared<spdlog::sinks::dist_sink_mt>()),
formatter(std::make_unique<spdlog::pattern_formatter>(
kDefaultLogFormat.data(),
spdlog::pattern_time_type::local, std::string(""))),
logger(std::make_shared<spdlog::logger>(_loggerName, sinks))
{
}
Expand All @@ -46,6 +54,9 @@ class Logger::Implementation
/// \brief A sink distribution storing multiple sinks.
std::shared_ptr<spdlog::sinks::dist_sink_mt> sinks {nullptr};

/// \brief Common formatter for both all sinks
std::unique_ptr<spdlog::pattern_formatter> formatter;

/// \brief The underlying spdlog logger.
std::shared_ptr<spdlog::logger> logger {nullptr};
};
Expand All @@ -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_formatter>(
"%+", spdlog::pattern_time_type::local, std::string(""));
this->dataPtr->logger->set_formatter(std::move(f));
this->dataPtr->logger->set_formatter(this->dataPtr->formatter->clone());
}

/////////////////////////////////////////////////
Expand All @@ -77,6 +85,8 @@ void Logger::SetLogDestination(const std::string &_filename)
{
this->dataPtr->fileSink =
std::make_shared<spdlog::sinks::basic_file_sink_mt>(_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);
}
}
Expand All @@ -103,4 +113,13 @@ std::shared_ptr<spdlog::logger> 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

0 comments on commit c8c4abb

Please sign in to comment.