-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom Asynchronous Loggers #2742
Comments
It is kind of intentional, for performance reasons. What do you want to achieve specifically? |
In short, our objective is to prepend a thread-local message to every log call of asynchronous loggers. Here's the context: We successfully replaced our previous logging library with spdlog, which brought us notable performance and readability improvements. However, we encountered an obstacle while converting one specific functionality: spdlog lacks support for Nested Diagnostic Context (NDC). To address this, we created a custom implementation of NDC based on thread-local stacks. To achieve seamless integration with the thread pool's worker, we need to access the NDC within the log call and forward it as a string from the corresponding thread to the queue, as our previous logging library did. Unfortunately, we haven't found a viable approach to accomplish this in the current setup. Consequently, we are left with the challenge of manually prepending NDC information to each log message, which presents its own set of difficulties. FYI, Our first draft was implemented like this: class NDCFormatter : public spdlog::custom_flag_formatter
{
public:
void format(const spdlog::details::log_msg& msg, const std::tm& tm_time, spdlog::memory_buf_t& dest) override
{
if (!NDC::empty())
{
const auto& ndc_context = NDC::get();
dest.append(ndc_context.data(), ndc_context.data() + ndc_context.size());
}
}
std::unique_ptr<custom_flag_formatter> clone() const override;
};
inline std::unique_ptr<spdlog::pattern_formatter> createNDCPatternFormatter(std::string pattern)
{
auto f = std::make_unique<spdlog::pattern_formatter>();
f->template add_flag<NDCFormatter>('x').set_pattern(std::move(pattern));
return f;
} However, as you know, this kind of formatting happens from within the thread_pool's worker thread. This means that the NDC stack and a call to |
So how would inheriting the async logger solve this? |
It would unlock overriding the While it may not be a perfect solution as it requires rebuilding the My aim is to achieve the same goal regardless of the inheritability of the |
Yes, that might work. If you change the lib code, you can achieve this. Another (a bit dirty, but avoids changing the lib) option would be to use 2 loggers and a custom sink: Synchronous Logger with custom sink that adds MDC as a string to the message and forward to to a second asynchronous logger. |
Related: #2595 |
Hi, I also encountered the problem of custom logger and was stumped on this issue. For custom asynchronous logger, is it possible to consider abstracting Then Finally, modify I guess this would allow users to customize
|
As the current implementation stands, the
logger
class is the only logging class that supports polymorphism, while theasync_logger
class is marked as final. This limitation poses challenges for creating custom asynchronous loggers.Could you please clarify if this restriction is intentional? If so, it would be helpful to understand alternative approaches to achieve asynchronous logging without the need to rewrite or wrap the existing implementation functionalities.
The text was updated successfully, but these errors were encountered: