From d03d2be75274a38feee32183d3e72a91d2e2fa72 Mon Sep 17 00:00:00 2001 From: Bailey Chittle Date: Thu, 5 Jan 2023 12:11:20 -0500 Subject: [PATCH] more endpoints, removed commas from logfmt --- example/example.cpp | 8 +++--- include/spdlog/details/log_attr.h | 2 ++ include/spdlog/logger.h | 38 ++++++++++++++++++++++++++ include/spdlog/pattern_formatter-inl.h | 2 +- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 5591b3a0..b96b7041 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -394,15 +394,15 @@ void replace_default_logger_example() } void attribute_example() { - spdlog::default_logger_raw()->log(spdlog::level::warn, "EXPERIMENTAL: log with attributes", {{"attribute_key", "attribute value"}}); + spdlog::default_logger_raw()->warn("EXPERIMENTAL: log with attributes", {{"attribute_key", "attribute value"}}); // logfmt structured logging using attributes // auto logfmt_logger = spdlog::basic_logger_mt("logfmt_logger", "logs/mylog.txt"); - auto logfmt_logger = spdlog::default_logger_raw(); + auto logfmt_logger = spdlog::stdout_color_mt("logfmt_logger"); - std::string logfmt_pattern = "time=%Y-%m-%dT%H:%M:%S.%f%z, name=%n, level=%^%l%$, process=%P, thread=%t, message=\"%v\", "; + std::string logfmt_pattern = "time=%Y-%m-%dT%H:%M:%S.%f%z name=%n level=%^%l%$ process=%P thread=%t message=\"%v\" "; logfmt_logger->set_pattern(logfmt_pattern); - logfmt_logger->log(spdlog::level::info, "logfmt structured logging", spdlog::attribute_list{{"key\n1", "value\n1"}, {"key\r\n2", "value\r\n2"}}); + logfmt_logger->info("logfmt structured logging", spdlog::attribute_list{{"key\n1", "value\n1"}, {"key\r\n2", "value\r\n2"}}); } \ No newline at end of file diff --git a/include/spdlog/details/log_attr.h b/include/spdlog/details/log_attr.h index 3a59492d..4dfd919e 100644 --- a/include/spdlog/details/log_attr.h +++ b/include/spdlog/details/log_attr.h @@ -36,6 +36,8 @@ public: value = std::string{v.data(), v.size()}; } + attr(std::string k, std::string v) : key{k}, value{v} {} + template attr(string_view_t k, T const &v) : value{std::to_string(v)} diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 15934bf5..6160cef8 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -281,6 +281,44 @@ public: } #endif + // attributes endpoint + template + void trace(const T &msg, attribute_list attrs) + { + log(level::trace, msg, attrs); + } + + template + void debug(const T &msg, attribute_list attrs) + { + log(level::debug, msg, attrs); + } + + template + void info(const T &msg, attribute_list attrs) + { + log(level::info, msg, attrs); + } + + template + void warn(const T &msg, attribute_list attrs) + { + log(level::warn, msg, attrs); + } + + template + void error(const T &msg, attribute_list attrs) + { + log(level::err, msg, attrs); + } + + template + void critical(const T &msg, attribute_list attrs) + { + log(level::critical, msg, attrs); + } + + // default endpoint template void trace(const T &msg) { diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index b2edcb93..ba866c68 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -1085,7 +1085,7 @@ SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory details::fmt_helper::append_string_view(a.key, dest); details::fmt_helper::append_string_view("=\"", dest); details::fmt_helper::append_string_view(a.value, dest); - details::fmt_helper::append_string_view("\", ", dest); + details::fmt_helper::append_string_view("\" ", dest); } // write eol