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

Multi-sink logger behavior with File and line number set in the pattern #2043

Open
python152 opened this issue Aug 11, 2021 · 4 comments
Open

Comments

@python152
Copy link

python152 commented Aug 11, 2021

hi all -

Using the multi-sink logger example, I'd like to add %g %# in the file_sink pattern to print out file and line number.
However, it seems the only way to call this is through SPDLOG_LOGGER_TRACE(logger, ...). If I use logger->trace(), it will not honor the pattern. Is this correct behavior? or am I missing something here?

// A logger with multiple sinks (stdout and file) - each with a different format and log level.
void multi_sink_example()
{
    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    console_sink->set_level(spdlog::level::warn);
    console_sink->set_pattern("[multi_sink_example] [%^%l%$] %v");

    auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/multisink.txt", true);
    file_sink->set_level(spdlog::level::trace);

    spdlog::logger logger("multi_sink", {console_sink, file_sink});
    logger.set_level(spdlog::level::debug);
    logger.warn("this should appear in both console and file");
    logger.info("this message should not appear in the console, only in the file");
}

TIA

@python152 python152 changed the title File and line number not showing Multi-sink logger behavior with File and line number set in the pattern Aug 11, 2021
@tt4g
Copy link
Contributor

tt4g commented Aug 12, 2021

Right. This is a specification.
See Wiki: https://github.com/gabime/spdlog/wiki/3.-Custom-formatting#pattern-flags

Another way. you can use logger->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, spdlog::level::trace, other arguments...) instead of logging macro such as SPDLOG_LOGGER_TRACE and SPDLOG_LOGGER_DEBUG.

https://github.com/gabime/spdlog/blob/v1.9.1/include/spdlog/spdlog.h#L291-L299

@python152
Copy link
Author

@tt4g thanks for the clarification. A quick follow up, If I call my logger directly for tracing (writing to file), can I still compile away? (like SPDLOG_XXX). I can't set spdlog::level::off because I want to info to show up; and if I set to spdlog::level::info, am I paying the performance penalty for tracing calls? TIA

@tt4g
Copy link
Contributor

tt4g commented Aug 12, 2021

Logging macros (SPDLOG_TRACE, SPDLOG_LOGGING_TRACE, etc...) less than or equal to the log level of the SPDLOG_ACTIVE_LEVEL macro are converted to (void) 0 in preprocessing, so there is no cost to call the function.
https://github.com/gabime/spdlog/wiki/0.-FAQ#how-to-remove-all-debug-statements-at-compile-time-

Calling the log function in any other way incurs costs such as comparing log levels.

@python152
Copy link
Author

super, in that case, I think I can do the same by defining my own macros using mylogger, and compile away it.
thanks a lot!

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