Skip to content

Commit

Permalink
Add custom json format example
Browse files Browse the repository at this point in the history
  • Loading branch information
odygrd committed Oct 5, 2024
1 parent ad9158f commit a5610f0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 65 additions & 0 deletions examples/json_console_logging_custom_json.cpp
Original file line number Diff line number Diff line change
@@ -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 <string>

/**
* 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<std::pair<std::string, std::string>> 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<MyJsonConsoleSink>("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);
}

0 comments on commit a5610f0

Please sign in to comment.