Attributes: Removing impact on logger

The logger interface should not be bloated by additional functionality. Reducing it to a nested call to `.attrs().put()` achieves the same nesting as `.put_attribute()` but requires a smaller interface change on the logger class.
This commit is contained in:
Felix Heitmann 2024-07-05 14:54:55 +02:00 committed by M4rFri
parent ffec5f3c07
commit e8924a3d47
3 changed files with 3 additions and 20 deletions

View File

@ -161,11 +161,7 @@ public:
// create new logger with same sinks and configuration. // create new logger with same sinks and configuration.
std::shared_ptr<logger> clone(std::string logger_name); std::shared_ptr<logger> clone(std::string logger_name);
void push_attribute(log_attributes::attr_map_t const &attributes); log_attributes &attrs();
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: private:
std::string name_; std::string name_;

View File

@ -82,19 +82,6 @@ void logger::flush_() noexcept {
} }
} }
void logger::push_attribute(const log_attributes::attr_map_t &attributes) { log_attributes &logger::attrs() { return 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 } // namespace spdlog

View File

@ -11,7 +11,7 @@ TEST_CASE("Attribute test") {
log_a.set_pattern("[%n] [%*]"); log_a.set_pattern("[%n] [%*]");
log_b.set_pattern("[%n] [%*]"); log_b.set_pattern("[%n] [%*]");
log_a.push_attribute("my_key", "my_value"); log_a.attrs().put("my_key", "my_value");
log_a.info("Hello"); log_a.info("Hello");
log_b.info("Hello"); log_b.info("Hello");