From a5610f0c16ace8527d31dc01fdf40b86d2fbb149 Mon Sep 17 00:00:00 2001 From: Odysseas Georgoudis Date: Sat, 5 Oct 2024 02:11:32 +0100 Subject: [PATCH] Add custom json format example --- examples/CMakeLists.txt | 1 + examples/json_console_logging_custom_json.cpp | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 examples/json_console_logging_custom_json.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index f2f0a5e6..b62b55d3 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -23,6 +23,7 @@ set(EXAMPLE_TARGETS quill_example_user_defined_sink quill_example_tags_logging quill_example_json_console_logging + quill_example_json_console_logging_custom_json quill_example_csv_writing quill_example_json_file_logging quill_example_user_defined_types_logging diff --git a/examples/json_console_logging_custom_json.cpp b/examples/json_console_logging_custom_json.cpp new file mode 100644 index 00000000..824288a8 --- /dev/null +++ b/examples/json_console_logging_custom_json.cpp @@ -0,0 +1,65 @@ +#include "quill/Backend.h" +#include "quill/Frontend.h" +#include "quill/LogMacros.h" +#include "quill/Logger.h" +#include "quill/sinks/JsonSink.h" + +#include + +/** + * Overrides generate_json_message to use a custom json format + */ +class MyJsonConsoleSink : public quill::JsonConsoleSink +{ + void generate_json_message(quill::MacroMetadata const* /** log_metadata **/, uint64_t log_timestamp, + std::string_view /** thread_id **/, std::string_view /** thread_name **/, + std::string const& /** process_id **/, std::string_view /** logger_name **/, + quill::LogLevel /** log_level **/, std::string_view log_level_description, + std::string_view /** log_level_short_code **/, + std::vector> const* named_args, + std::string_view /** log_message **/, + std::string_view /** log_statement **/, char const* message_format) override + { + _json_message.append(fmtquill::format(R"({{"timestamp":"{}","log_level":"{}","message":"{}")", + std::to_string(log_timestamp), log_level_description, message_format)); + + // Add args as key-values + if (named_args) + { + for (auto const& [key, value] : *named_args) + { + _json_message.append(std::string_view{",\""}); + _json_message.append(key); + _json_message.append(std::string_view{"\":\""}); + _json_message.append(value); + _json_message.append(std::string_view{"\""}); + } + } + } +}; + +int main() +{ + // Start the backend thread + quill::BackendOptions backend_options; + quill::Backend::start(backend_options); + + // Frontend + + // Create a json sink + auto json_sink = quill::Frontend::create_or_get_sink("json_sink_1"); + + // When logging json, it is ideal to set the logging pattern to empty to avoid unnecessary message formatting. + quill::Logger* logger = quill::Frontend::create_or_get_logger( + "json_logger", std::move(json_sink), + quill::PatternFormatterOptions{"", "%H:%M:%S.%Qns", quill::Timezone::GmtTime}); + + int var_a = 123; + std::string var_b = "test"; + + // Log via the convenient LOGJ_ macros + LOGJ_INFO(logger, "A json message", var_a, var_b); + + // Or manually specify the desired names of each variable + LOG_INFO(logger, "A json message with {var_1} and {var_2}", var_a, var_b); +}