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

How can I get this output format? #2424

Closed
nyckmaia opened this issue Jul 5, 2022 · 3 comments
Closed

How can I get this output format? #2424

nyckmaia opened this issue Jul 5, 2022 · 3 comments

Comments

@nyckmaia
Copy link

nyckmaia commented Jul 5, 2022

Hi,
I would like to know how can I set different formats for each spdlog level.
By default, I got this output format:

[2022-07-05 14:25:26.685] [info] Welcome to spdlog!

What I would like

  • For level: info
Welcome to spdlog!
  • For level: warning
[warning] Welcome to spdlog!
  • For levels: error, critical and debug
[filename:lineNumber] [error] Welcome to spdlog!

Second question

Is there a way to add the function name (PRETTY_FUNCTION) in the log?
Like:

[filename:lineNumber] [functionName] [error] Welcome to spdlog!

Could you help me?

Thank you,

@tt4g
Copy link
Contributor

tt4g commented Jul 5, 2022

Per-log-level formatting is not supported. However, you can create your own custom sink and format it specifically for each log level.
Wiki: https://github.com/gabime/spdlog/wiki/4.-Sinks#implementing-your-own-sink

Use the format flags %@ and %! . Note that these flags will not show anything unless you use the logging macro such as SPDLOG_INFO and SPDLOG_LOGGER_INFO.
Wiki: https://github.com/gabime/spdlog/wiki/3.-Custom-formatting

@oct15demo
Copy link

If you don't like the awkwardness of typing the ALL CAPS macro (I don't), you can write your own macro, like so:

#define debug(...) log(spdlog::source_loc{FILE, LINE, static_cast<const char *>(FUNCTION)}, spdlog::level::debug, VA_ARGS)

This will work with
logger.debug("Hello macro");

or even
logger.debug(format("Hello {} macro", "formatted"));
with
#define format fmt::format
just to save some keystrokes.

Getting my logger
static spdlog::logger logger = getLog();

So I end up with a very log4j like syntax that gives line numbers, file, method.

See also related open issue #1823. Output with file:linenumber is extremely useful in an IDE in order for the log output to link to the source. Wouldn't take a big effort to make something like the above configurable in spdlog if they thought it was important, one would think. Meaning just to make the file and line number accessible with less awkward macro identifier.

@nyckmaia
Copy link
Author

@thank you so much @oct15demo ! Great!

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

3 participants