-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
write error and critical logs to stderr #345
Comments
You can add an additional stderr sink with its level to level::error. |
The wiki seems to lack the level-setting part, unless I missed it completely. Could you show how this would be done? |
Just call set_level(..) on any sink: sink->set_level(spdlog::level::err); |
Ah, thanks. I've come thus far: auto setup_logger()
{
std::vector<spdlog::sink_ptr> sinks;
auto stdout_sink = spdlog::sinks::stdout_sink_mt::instance();
stdout_sink->set_level(spdlog::level::info);
auto stderr_sink = spdlog::sinks::stderr_sink_mt::instance();
auto color_stderr_sink = std::make_shared<spdlog::sinks::ansicolor_sink>(stderr_sink);
color_stderr_sink->set_level(spdlog::level::err);
sinks.push_back(stdout_sink);
sinks.push_back(color_stderr_sink);
auto logger = std::make_shared<spdlog::logger>("main", std::begin(sinks), std::end(sinks));
logger->set_pattern("%l: %v");
return logger;
} As wanted, error logs are now printed to On a side note: if I want |
To answer my own questions:
And I gather from |
Yes, you got it right, but note that if for example the logger has "info" level then debug messages won't get forwarded by the logger to the underlying sinks, regardless of their own level. I am not sure that |
On 7 January 2017 10:51:25 CET, Gabi Melman ***@***.***> wrote:
Yes, you got it right, but note that if for example the logger has
"info" level then debug messages won't get forwarded by the logger to
the underlying sinks, regardless of their own level.
I am not sure that ```limit_level()``` would worth the extra cost just
for this rare use case though.. Maybe you could implement a custon sink
that wraps those 2 sinks by forwarding the messages accoriding to the
level. should be easy I think.
--
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#345 (comment)
Alright, makes sense.
A custom sink does sound like a better idea. A singular if-statement should probably suffice. I'll see about writing and posting the solution here.
|
The log message level is available in class split_sink : public spdlog::sinks::sink {
void log(const details::log_msg &msg) override
{
if (msg.level <= spdlog::level::warn)
std::cout << msg.formatted.str();
else
std::cerr << msg.formatted.str();
}
void flush()
{
std::cout << std::flush;
}
}; is what I came up with. auto setup_logger()
{
auto sink = std::make_shared<split_sink>();
auto logger = std::make_shared<spdlog::logger>("main", sink);
logger->set_pattern("%l: %v");
logger->set_level(spdlog::level::err); // as default
return logger;
} Thanks for your help! |
Looks good. Dont forget protect with mutex if you need thread safety. |
In a project I work on I use spdlog to report errors to the user. To match the behavior of other cli-programs, I want that output written to
stderr
. Couldlevel::{err,critical}
logging be changed to write tostderr
, or could a define perhaps be exposed intweakme.h
?Cheers.
The text was updated successfully, but these errors were encountered: