-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
LogLevel off has the wrong enum value #3121
Comments
I think the log level is designed to achieve log filtering by comparison, and the behavior of Since importing the suggested changes will break the existing behavior (logging forced by |
I'm not sure I follow you. I do want my logger to output logs, but there's a category of messages that I want to turn off in certain cases. Something like
So the logger will log some stuff, like the first line should always log if I want logging at all. But sometimes I want to set I don't see how the change would break things. I'm only checking if the message log level is off before comparing it to the logger log level. I assume, given how things work, that nobody is calling |
It means that If you just don't want to output logs under certain conditions, why not just not call the logging function? if (<COND>) {
logger.info("I don't want to see this sometimes, regardless of log level");
} |
I see what you mean. However, since I think the enum log level is then abused a bit, being used for both message and logger. I suppose conceptually there should be two enums then, one for the message level and one for the logger level. Then, off should be a valid value for the logger log level, but not for the message log level. Alternatively, off should not be a value at all, and users should do something like Anyhow, I think what you suggest is probably the best solution I currently have to avoid printing that kind of messages. |
Yes. I understand the opinion that the use of the log level is confusing to users. |
Another way to avoid logging when logging level is Example: #include "spdlog/sinks/base_sink.h"
#include "spdlog/sinks/dist_sink.h"
#include "spdlog/details/null_mutex.h"
#include <memory>
#include <mutex>
#include <vector>
#include <utility>
template<typename Mutex>
class filter_off_sink : public spdlog::sinks::dist_sink<Mutex>
{
public:
filter_off_sink() = default;
explicit filter_off_sink(std::vector<std::shared_ptr<sink>> sinks)
: spdlog::sinks::dist_sink(std::move(sinks)) {}
protected:
void sink_it_(const spdlog::details::log_msg& msg) override
{
if (msg.level == spdlog::level::off) {
return;
}
spdlog::sinks::dist_sink<Mutex>::sink_it_(msg);
}
};
using filter_off_sink_mt = filter_off_sink<std::mutex>;
using filter_off_sink_st = filter_off_sink<spdlog::details::null_mutex>; |
Since off=6, calling
log (off, "msg")
will always print the message, sinceoff
is greater than whatever the logger log level is. I changedlogger::should_log
impl asand I get the expected behavior, namely that
logger.log(level::off, "blah");
does not log.I think the log_level "off" should have value 6 when it comes to the logger log level, but should have the value -1 when it comes to the message log level, since it must be lower than the log level of the logger (regardless of what the logger verbosity is).
Some context: I have a logger with log level "info" (say). There is certain category of messages that in some cases I want to disable completely, regardless of the logger log level. I suppose I could use 'trace' for these messages, but the level "off" seemed so appropriate. And yet, it does exactly the opposite, since it bumps the msg level to the max verbosity possible.
The text was updated successfully, but these errors were encountered: