Skip to content

Commit

Permalink
Added attribute formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
M4rFri committed Jul 4, 2024
1 parent 80c5024 commit fe1cd03
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/pattern_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,41 @@ class mdc_formatter final : public flag_formatter {
}
};

template <typename ScopedPadder>
class attribute_formatter final : public flag_formatter {
public:
explicit attribute_formatter(padding_info padinfo)
: flag_formatter(padinfo) {}

void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override {
if (msg.attributes.empty()) {
ScopedPadder p(0, padinfo_, dest);
return;
} else {
auto &attributes = msg.attributes.get_map();
format_attributes(attributes, dest);
}
}

void format_attributes(const spdlog::log_attributes::attr_map_t &attributes, memory_buf_t &dest) {
auto last_element = --attributes.end();
for (auto it = attributes.begin(); it != attributes.end(); ++it) {
auto &attribute = *it;
const auto &key = attribute.first;
const auto &value = attribute.second;
size_t content_size = key.size() + value.size() + 1; // 1 for ':'

if (it != last_element) content_size++; // 1 for ' '

ScopedPadder p(content_size, padinfo_, dest);
fmt_helper::append_string_view(key, dest);
fmt_helper::append_string_view(":", dest);
fmt_helper::append_string_view(value, dest);
if (it != last_element) fmt_helper::append_string_view(" ", dest);
}
}
};

// Full info formatter
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v
class full_formatter final : public flag_formatter {
Expand Down Expand Up @@ -854,6 +889,15 @@ class full_formatter final : public flag_formatter {
dest.push_back(' ');
}

// add attributes if present
if (!msg.attributes.empty()) {
dest.push_back('[');
auto &attributes = msg.attributes.get_map();
attribute_formatter_.format_attributes(attributes, dest);
dest.push_back(']');
dest.push_back(' ');
}

// add mdc if present
auto &mdc_map = mdc::get_context();
if (!mdc_map.empty()) {
Expand All @@ -862,12 +906,14 @@ class full_formatter final : public flag_formatter {
dest.push_back(']');
dest.push_back(' ');
}

fmt_helper::append_string_view(msg.payload, dest);
}

private:
std::chrono::seconds cache_timestamp_{0};
memory_buf_t cached_datetime_;
attribute_formatter<null_scoped_padder> attribute_formatter_{padding_info{}};
mdc_formatter<null_scoped_padder> mdc_formatter_{padding_info{}};
};
} // namespace details
Expand Down Expand Up @@ -1150,6 +1196,10 @@ void pattern_formatter::handle_flag_(char flag, details::padding_info padding) {
formatters_.push_back(std::make_unique<details::mdc_formatter<Padder>>(padding));
break;

case ('*'):
formatters_.push_back(std::make_unique<details::attribute_formatter<Padder>>(padding));
break;

default: // Unknown flag appears as is
auto unknown_flag = std::make_unique<details::aggregate_formatter>();

Expand Down

0 comments on commit fe1cd03

Please sign in to comment.