-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable log statement format overriding in Sinks
- Loading branch information
Showing
19 changed files
with
479 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
add_executable(quill_single_logger_multiple_sink_formats_1 single_logger_multiple_sink_formats_1.cpp) | ||
set_common_compile_options(quill_single_logger_multiple_sink_formats_1) | ||
target_link_libraries(quill_single_logger_multiple_sink_formats_1 quill) | ||
|
||
add_executable(quill_single_logger_multiple_sink_formats_2 single_logger_multiple_sink_formats_2.cpp) | ||
set_common_compile_options(quill_single_logger_multiple_sink_formats_2) | ||
target_link_libraries(quill_single_logger_multiple_sink_formats_2 quill) |
38 changes: 38 additions & 0 deletions
38
examples/single_logger_multiple_sink_formats/ConsoleSinkWithFormatter.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
|
||
#include "quill/backend/PatternFormatter.h" | ||
#include "quill/core/Attributes.h" | ||
#include "quill/core/Common.h" | ||
#include "quill/core/Filesystem.h" | ||
#include "quill/sinks/ConsoleSink.h" | ||
|
||
#include <string> | ||
#include <string_view> | ||
|
||
class ConsoleSinkWithFormatter : public quill::ConsoleSink | ||
{ | ||
public: | ||
ConsoleSinkWithFormatter(std::string const& format_pattern, std::string const& time_format, | ||
quill::Timezone timestamp_timezone = quill::Timezone::LocalTime, | ||
bool enable_colours = true, std::string const& stream = "stdout") | ||
: quill::ConsoleSink(enable_colours, stream), _formatter(format_pattern, time_format, timestamp_timezone) | ||
{ | ||
} | ||
|
||
void write_log(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::vector<std::pair<std::string, std::string>> const* named_args, | ||
std::string_view log_message, std::string_view) override | ||
{ | ||
std::string_view const formatted_log_statement = | ||
_formatter.format(log_timestamp, thread_id, thread_name, process_id, logger_name, | ||
quill::loglevel_to_string(log_level), *log_metadata, named_args, log_message); | ||
|
||
quill::ConsoleSink::write_log(log_metadata, log_timestamp, thread_id, thread_name, process_id, | ||
logger_name, log_level, named_args, log_message, formatted_log_statement); | ||
} | ||
|
||
private: | ||
quill::PatternFormatter _formatter; | ||
}; |
40 changes: 40 additions & 0 deletions
40
examples/single_logger_multiple_sink_formats/RotatingFileSinkWithFormatter.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
|
||
#include "quill/backend/PatternFormatter.h" | ||
#include "quill/core/Attributes.h" | ||
#include "quill/core/Common.h" | ||
#include "quill/core/Filesystem.h" | ||
#include "quill/sinks/RotatingFileSink.h" | ||
|
||
#include <string> | ||
#include <string_view> | ||
|
||
class RotatingFileSinkWithFormatter : public quill::RotatingFileSink | ||
{ | ||
public: | ||
RotatingFileSinkWithFormatter(quill::fs::path const& filename, quill::RotatingFileSinkConfig const& config, | ||
std::string const& format_pattern, std::string const& time_format, | ||
quill::Timezone timestamp_timezone = quill::Timezone::LocalTime, | ||
quill::FileEventNotifier file_event_notifier = quill::FileEventNotifier{}) | ||
: quill::RotatingFileSink(filename, config, file_event_notifier), | ||
_formatter(format_pattern, time_format, timestamp_timezone) | ||
{ | ||
} | ||
|
||
void write_log(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::vector<std::pair<std::string, std::string>> const* named_args, | ||
std::string_view log_message, std::string_view) override | ||
{ | ||
std::string_view const formatted_log_statement = | ||
_formatter.format(log_timestamp, thread_id, thread_name, process_id, logger_name, | ||
quill::loglevel_to_string(log_level), *log_metadata, named_args, log_message); | ||
|
||
quill::RotatingFileSink::write_log(log_metadata, log_timestamp, thread_id, thread_name, process_id, logger_name, | ||
log_level, named_args, log_message, formatted_log_statement); | ||
} | ||
|
||
private: | ||
quill::PatternFormatter _formatter; | ||
}; |
57 changes: 57 additions & 0 deletions
57
examples/single_logger_multiple_sink_formats/single_logger_multiple_sink_formats_1.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "quill/Backend.h" | ||
#include "quill/Frontend.h" | ||
#include "quill/LogMacros.h" | ||
#include "quill/Logger.h" | ||
|
||
#include "RotatingFileSinkWithFormatter.h" | ||
#include "quill/sinks/ConsoleSink.h" | ||
|
||
/** | ||
* This example demonstrates how to use a single Logger to output different formats for each Sink. | ||
* While the Logger class accepts a single format pattern, you can override this by creating a custom Sink with your own format pattern. | ||
*/ | ||
int main() | ||
{ | ||
// Start the backend thread | ||
quill::BackendOptions backend_options; | ||
quill::Backend::start(backend_options); | ||
|
||
// Console sink | ||
auto console_sink = quill::Frontend::create_or_get_sink<quill::ConsoleSink>("sink_id_1"); | ||
console_sink->set_log_level_filter(quill::LogLevel::Warning); | ||
std::string console_log_pattern = | ||
"%(time) [PID %(process_id)] [%(log_level)] [%(logger)] - %(message)"; | ||
std::string console_time_format = "%Y-%m-%d %H:%M:%S.%Qms"; | ||
|
||
// File sink | ||
std::string const file_log_pattern = "%(log_level);%(time);%(logger);%(message)"; | ||
std::string const file_time_format = "%Y%m%dT%H:%M:%S.%Qus"; | ||
|
||
auto rotating_file_sink = quill::Frontend::create_or_get_sink<RotatingFileSinkWithFormatter>( | ||
"rotating_file.log", | ||
[]() | ||
{ | ||
// See RotatingFileSinkConfig for more options | ||
quill::RotatingFileSinkConfig cfg; | ||
cfg.set_open_mode('a'); | ||
cfg.set_max_backup_files(10); | ||
cfg.set_rotation_max_file_size(1024 * 1024); | ||
return cfg; | ||
}(), | ||
file_log_pattern, file_time_format); | ||
rotating_file_sink->set_log_level_filter(quill::LogLevel::Info); | ||
|
||
// The Logger is using the console_log_pattern by default | ||
// To output our custom format to the file we use our own RotatingFileSinkWithFormatter that is | ||
// overwriting the default format | ||
quill::Logger* logger = quill::Frontend::create_or_get_logger( | ||
"root", {std::move(console_sink), std::move(rotating_file_sink)}, console_log_pattern, console_time_format); | ||
|
||
logger->set_log_level(quill::LogLevel::Debug); | ||
|
||
LOG_INFO(logger, "This is a log info example {}", sizeof(std::string)); | ||
LOG_WARNING(logger, "This is a log warning example {}", sizeof(std::string)); | ||
LOG_ERROR(logger, "This is a log error example {}", sizeof(std::string)); | ||
LOG_CRITICAL(logger, "This is a log critical example {}", sizeof(std::string)); | ||
return 0; | ||
} |
57 changes: 57 additions & 0 deletions
57
examples/single_logger_multiple_sink_formats/single_logger_multiple_sink_formats_2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "quill/Backend.h" | ||
#include "quill/Frontend.h" | ||
#include "quill/LogMacros.h" | ||
#include "quill/Logger.h" | ||
|
||
#include "ConsoleSinkWithFormatter.h" | ||
#include "quill/sinks/RotatingFileSink.h" | ||
|
||
/** | ||
* This example demonstrates how to use a single Logger to output different formats for each Sink. | ||
* While the Logger class accepts a single format pattern, you can override this by creating a custom Sink with your own format pattern. | ||
*/ | ||
int main() | ||
{ | ||
// Start the backend thread | ||
quill::BackendOptions backend_options; | ||
quill::Backend::start(backend_options); | ||
|
||
// Console sink | ||
std::string console_log_pattern = | ||
"%(time) [PID %(process_id)] [%(log_level)] [%(logger)] - %(message)"; | ||
std::string console_time_format = "%Y-%m-%d %H:%M:%S.%Qms"; | ||
auto console_sink = quill::Frontend::create_or_get_sink<ConsoleSinkWithFormatter>( | ||
"sink_id_1", console_log_pattern, console_time_format); | ||
console_sink->set_log_level_filter(quill::LogLevel::Warning); | ||
|
||
// File sink | ||
std::string const file_log_pattern = "%(log_level);%(time);%(logger);%(message)"; | ||
std::string const file_time_format = "%Y%m%dT%H:%M:%S.%Qus"; | ||
|
||
auto rotating_file_sink = quill::Frontend::create_or_get_sink<quill::RotatingFileSink>( | ||
"rotating_file.log", | ||
[]() | ||
{ | ||
// See RotatingFileSinkConfig for more options | ||
quill::RotatingFileSinkConfig cfg; | ||
cfg.set_open_mode('a'); | ||
cfg.set_max_backup_files(10); | ||
cfg.set_rotation_max_file_size(1024 * 1024); | ||
return cfg; | ||
}()); | ||
rotating_file_sink->set_log_level_filter(quill::LogLevel::Info); | ||
|
||
// The Logger is using the file_log_pattern by default | ||
// To output our custom format to the file we use our own ConsoleSinkWithFormatter that is | ||
// overwriting the default format | ||
quill::Logger* logger = quill::Frontend::create_or_get_logger( | ||
"root", {std::move(console_sink), std::move(rotating_file_sink)}, file_log_pattern, file_time_format); | ||
|
||
logger->set_log_level(quill::LogLevel::Debug); | ||
|
||
LOG_INFO(logger, "This is a log info example {}", sizeof(std::string)); | ||
LOG_WARNING(logger, "This is a log warning example {}", sizeof(std::string)); | ||
LOG_ERROR(logger, "This is a log error example {}", sizeof(std::string)); | ||
LOG_CRITICAL(logger, "This is a log critical example {}", sizeof(std::string)); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.