From c3dac2fa5d793c688c1968ba4cee05e00dc1eb86 Mon Sep 17 00:00:00 2001 From: M4rFri Date: Fri, 5 Jul 2024 12:57:25 +0200 Subject: [PATCH] Updated logger to accept and log attributes --- include/spdlog/logger.h | 16 ++++++++++++---- src/logger.cpp | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index dbd932fa..c0627424 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -19,6 +19,7 @@ #include "details/err_helper.h" #include "details/log_msg.h" #include "sinks/sink.h" +#include "attributes.h" namespace spdlog { @@ -63,20 +64,20 @@ public: // log with no format string, just string message void log(const source_loc &loc, level lvl, string_view_t msg) noexcept { if (should_log(lvl)) { - sink_it_(details::log_msg(loc, name_, lvl, msg)); + sink_it_(details::log_msg(loc, name_, lvl, msg, attributes)); } } void log(level lvl, string_view_t msg) noexcept { if (should_log(lvl)) { - sink_it_(details::log_msg(source_loc{}, name_, lvl, msg)); + sink_it_(details::log_msg(source_loc{}, name_, lvl, msg, attributes)); } } // support for custom time void log(log_clock::time_point log_time, const source_loc &loc, level lvl, string_view_t msg) noexcept { if (should_log(lvl)) { - sink_it_(details::log_msg(log_time, loc, name_, lvl, msg)); + sink_it_(details::log_msg(log_time, loc, name_, lvl, msg, attributes)); } } @@ -160,9 +161,16 @@ public: // create new logger with same sinks and configuration. std::shared_ptr clone(std::string logger_name); + void push_attribute(log_attributes::attr_map_t const &attributes); + void push_attribute(log_attributes::key_t const &key, log_attributes::value_t const &value); + void push_attribute(log_attributes const &attributes); + void remove_attribute(const log_attributes::key_t &key); + void clear_attribute(); + private: std::string name_; std::vector sinks_; + log_attributes attributes; atomic_level_t level_{level::info}; atomic_level_t flush_level_{level::off}; details::err_helper err_helper_; @@ -178,7 +186,7 @@ private: try { memory_buf_t buf; fmt::vformat_to(std::back_inserter(buf), format_string, fmt::make_format_args(args...)); - sink_it_(details::log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()))); + sink_it_(details::log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()), attributes)); } catch (const std::exception &ex) { err_helper_.handle_ex(name_, loc, ex); } catch (...) { diff --git a/src/logger.cpp b/src/logger.cpp index dc480bae..4da695af 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -81,4 +81,20 @@ void logger::flush_() noexcept { } } } + +void logger::push_attribute(const log_attributes::attr_map_t &attributes) { + this->attributes.attr_ctx(attributes.begin(), attributes.end()); +} +void logger::push_attribute(const log_attributes::key_t &key, const log_attributes::value_t &value) { + attributes.put(key, value); +} +void logger::push_attribute(const log_attributes &attributes) { + const auto &map = attributes.get_map(); + this->attributes.attr_ctx(map.begin(), map.end()); +} + +void logger::remove_attribute(const log_attributes::key_t &key) { attributes.remove(key); } + +void logger::clear_attribute() { attributes.clear(); } + } // namespace spdlog