Replies: 3 comments 1 reply
-
hey, you can do something like this I guess #include "quill/Backend.h"
#include "quill/Frontend.h"
#include "quill/LogMacros.h"
#include "quill/Logger.h"
#include "quill/sinks/ConsoleSink.h"
#include "quill/sinks/FileSink.h"
#include "quill/std/FilesystemPath.h"
/**
* Trivial logging example to console
* Note: You can also pass STL types by including the relevant header files from quill/std/
*/
int main()
{
quill::BackendOptions backend_options;
quill::Backend::start(backend_options);
// Frontend
auto console_sink = quill::Frontend::create_or_get_sink<quill::ConsoleSink>("sink_id_1");
auto file_sink = quill::Frontend::create_or_get_sink<quill::FileSink>(
"example_file_logging.log",
[]()
{
quill::FileSinkConfig cfg;
cfg.set_open_mode('w');
cfg.set_filename_append_option(quill::FilenameAppendOption::StartDateTime);
return cfg;
}(),
quill::FileEventNotifier{});
quill::Logger* logger =
quill::Frontend::create_or_get_logger("root", {std::move(console_sink), std::move(file_sink)});
// 1. retrieve existing sink by id
{
auto sink = quill::Frontend::get_sink("sink_id_1");
// print the filename if it is a StreamSink
if (auto* stream_sink = dynamic_cast<quill::StreamSink*>(sink.get()); stream_sink)
{
LOG_INFO(logger, "Stream Sink file: `{}`", stream_sink->get_filename());
}
}
// 2. retrieve all logger sinks
{
auto& logger_sinks = logger->get_sinks();
for (auto& sink : logger_sinks)
{
// print the filename of each sink
if (auto* stream_sink = dynamic_cast<quill::StreamSink*>(sink.get()); stream_sink)
{
LOG_INFO(logger, "Logger `{}` has Stream Sink file: `{}`", logger->get_logger_name(),
stream_sink->get_filename());
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
@odygrd Is it possible to enhance logger to add one interface to save the file name directly? The method you provided works usually. But in my code, it's hard to use. My code uses "the exec path + fixed name" as the path (sink name). I don't know it's run path when I write the code. |
Beta Was this translation helpful? Give feedback.
-
Method 2 proved to be effective in my scenario. Once I retrieved all the file names, I was able to locate the target file by searching for the . In my case, I utilized a global map to maintain the mapping between loggers and their corresponding file names. This design allows me to easily retrieve the file name directly from the logger instance. The implementation is straightforward and achieves the desired functionality efficiently. |
Beta Was this translation helpful? Give feedback.
-
I set the base name when creating the sink with datetime appended.
I know the rule of appending. So actually I could get the its name.
Do we have the direct method to output this?
Beta Was this translation helpful? Give feedback.
All reactions