Skip to content
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

Add the ability to create your own formatters #782

Open
nepridumalnik opened this issue Dec 4, 2024 · 2 comments
Open

Add the ability to create your own formatters #782

nepridumalnik opened this issue Dec 4, 2024 · 2 comments

Comments

@nepridumalnik
Copy link
Contributor

Add a description

I suggest to add the ability to create and use your own formatters for logging not only to a custom format via a callback (for example, LOG_ADD_FORMATTER(&CustomFormatCb, "custom_formatter_name"), or in another way.

@theg4sh
Copy link
Contributor

theg4sh commented Dec 4, 2024

Hi, logger in userver allows you to customize logging format for your own types.
Have you trying a suggestion from LogHelper to declare your own logging::LogHelper& operator<<(logging::LogHelper&, const auto& T)?
this one https://github.com/userver-framework/userver/blob/develop/universal/include/userver/logging/log_helper.hpp#L287

Could you please enrich your suggestion with e.g. pseudo-code and example when it could be useful for other developers?

@nepridumalnik
Copy link
Contributor Author

nepridumalnik commented Dec 5, 2024

The number of log output formats in userver is limited, and LogHelper uses tskv (Stream-like tskv-formatted log message builder), as described in its documentation. I suggest using callbacks to create your own formatters. For example (pseudocode):

// Formatter function
std::string CustomFormatCb(const SomeLogStruct& log)
{
    return fmt::format(R"("{timestamp}": "{}", "level": "{}", "message": "{}")", log.timestamp, log.level, log.message);
}

Then, set it in the code:

// ...
    LOG_ADD_FORMATTER(&CustomFormatCb, "custom_formatter_name")
// ...

This will allow you to customize the log details and output format—JSON, YAML, etc.—instead of relying on tskv or other built-in userver formats. It may be useful if you need to use some kind of log aggregation system that requires a specific log format to be displayed in the user interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants