Multiple Sinks? RotatingFile and Console for the same log? #629
Answered
by
odygrd
rhaley-starfish
asked this question in
Q&A
-
Hello, quill::Logger *new_quill_system_logger(std::string& loggerDir, std::string& name, std::string& uuid, int64_t logFileRollSize )
{
if(loggerDir.back() != '/'){
loggerDir.append("/");
}
std::string path = loggerDir + name + ".log";
// Frontend
auto rotating_file_sink = quill::Frontend::create_or_get_sink<quill::RotatingFileSink>(
path,
[&]()
{
// See RotatingFileSinkConfig for more options
quill::RotatingFileSinkConfig cfg;
cfg.set_open_mode('a');
cfg.set_filename_append_option(quill::FilenameAppendOption::StartDate);
cfg.set_rotation_time_daily("23:30"); //TODO: get from config
cfg.set_rotation_max_file_size(logFileRollSize);
return cfg;
}());
quill::Logger* logger = quill::Frontend::create_or_get_logger(
name + "-" + uuid, std::move(rotating_file_sink),
quill::PatternFormatterOptions{"%(time) [%(thread_id)] "
"LOG_%(log_level:<9) %(logger:<12) %(message)",
"%H:%M:%S.%Qns", quill::Timezone::GmtTime});
return logger;
} How do I set up both sinks to one logger? |
Beta Was this translation helpful? Give feedback.
Answered by
odygrd
Nov 22, 2024
Replies: 1 comment 1 reply
-
hey, you can do something like this: #include "quill/Backend.h"
#include "quill/Frontend.h"
#include "quill/LogMacros.h"
#include "quill/Logger.h"
#include "quill/sinks/RotatingFileSink.h"
#include "quill/sinks/ConsoleSink.h"
#include <utility>
int main()
{
// Start the backend thread
quill::BackendOptions backend_options;
quill::Backend::start(backend_options);
// Frontend
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('w');
cfg.set_filename_append_option(quill::FilenameAppendOption::StartDateTime);
cfg.set_rotation_time_daily("18:30");
cfg.set_rotation_max_file_size(1024); // small value to demonstrate the example
return cfg;
}());
auto console_sink = quill::Frontend::create_or_get_sink<quill::ConsoleSink>(
"sink_id_1");
quill::Logger* logger = quill::Frontend::create_or_get_logger(
"root", {std::move(rotating_file_sink), std::move(console_sink)},
quill::PatternFormatterOptions{"%(time) [%(thread_id)] %(short_source_location:<28) "
"LOG_%(log_level:<9) %(logger:<12) %(message)",
"%H:%M:%S.%Qns", quill::Timezone::GmtTime});
for (int i = 0; i < 20; ++i)
{
LOG_INFO(logger, "Hello from rotating logger, index is {}", i);
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
rhaley-starfish
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey, you can do something like this: