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

Adding setLogLevel feature #264

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

moleksy
Copy link
Collaborator

@moleksy moleksy commented Nov 22, 2024

  • Features:
    • Use mesh::log::setLogLevel(mesh::log::Level) to dynamically adjust log filtering.
    • Supported log levels: info, warn, error, debug, fatal.
    • Messages below the set log level will be ignored.

 * Features:
 *  - Use `mesh::log::setLogLevel(mesh::log::Level)` to dynamically adjust log filtering.
 *  - Supported log levels: `info`, `warn`, `error`, `debug`, `fatal`.
 *  - Messages below the set log level will be ignored.
@moleksy
Copy link
Collaborator Author

moleksy commented Nov 22, 2024

How it works:
image

Logger(const Logger&) = delete;
Logger& operator=(const Logger&) = delete;
~Logger();

template<typename T>
Logger& operator()(const char *key, const T& value) {
if (formatter)
if (formatter && level >= currentLogLevel)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still print the log message here no matter which log level is set, right? You only avoid formatter methods to be called if the level is disabled.

Comment on lines +118 to +120
void setLogLevel(Level level) {
currentLogLevel = level;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is not thread-safe. Since there is no meaning in changing the log level at runtime, I suggest to leave this function as it is but say in the comment block that this is to be called only once the logger is created in the app.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually there was a meaning for me - for different tests I wanted to have control over what is visible where, I'll make it thread safe.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you make it thread safe, you'll make currentLogLevel thread safe. It will add latency to printing messages. Please don't.


if (!ostream.str().empty())
Logger::~Logger() {
if (!ostream.str().empty() && level >= currentLogLevel) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!ostream.str().empty() && level >= currentLogLevel) {
if (level >= currentLogLevel && !ostream.str().empty()) {

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

Successfully merging this pull request may close these issues.

2 participants