mirror of
https://github.com/gabime/spdlog.git
synced 2025-03-04 13:15:48 +08:00
Updated logger to accept and log attributes
This commit is contained in:
parent
f24c55016c
commit
c3dac2fa5d
@ -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<logger> 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<sink_ptr> 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 (...) {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user