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

improve systemd journald support #1292

Merged
merged 2 commits into from
Oct 30, 2019
Merged

improve systemd journald support #1292

merged 2 commits into from
Oct 30, 2019

Conversation

jktjkt
Copy link
Contributor

@jktjkt jktjkt commented Oct 30, 2019

These three patches which make it easier to use systemd/journald's structured logging. When everything is in place, one can, for example, watch the log stream via this user-friendly :) command:

journlctl -f -u my-app.service \
  PRIORITY=1 PRIORITY=2 PRIORITY=3 PRIORITY=4 \
    SYSLOG_IDENTIFIER=only-important-messages-are-shown-now \
 + \
 PRIORITY=1 PRIORITY=2 PRIORITY=3 PRIORITY=4 PRIORITY=5 PRIORITY=6 PRIORITY=7 \
    SYSLOG_IDENTIFIER=I-want-details-from-this

That command will follow journal stream from the my-app.service. The logger whose name is only-important-messages-are-shown-now is rather heavily filtered, but everything from a logger called I-want-details-from-this is shown. These options are documented in journalctl's manpage. Sadly, there is neither range-checking nor negative matching in there.

I'm doing this mostly for use cases described in #1290 ...

journald: wider range of log severities

The previous code made it impossible to distinguish between the debug and trace severities. So instead of trying hard to preserve the equivalence of spdlog::level::debug with LOG_DEBUG, which is -- in the end -- just an arbitrary name, make sure that functionality is not lost.

Removed per explicit request; here's how to implement this:

#include <spdlog/sinks/systemd_sink.h>

template<typename Mutex>
class journald_sink : public spdlog::sinks::systemd_sink<Mutex> {
public:
    journald_sink()
    {
        this->syslog_levels_ = {/* spdlog::level::trace      */ LOG_DEBUG,
              /* spdlog::level::debug      */ LOG_INFO,
              /* spdlog::level::info       */ LOG_NOTICE,
              /* spdlog::level::warn       */ LOG_WARNING,
              /* spdlog::level::err        */ LOG_ERR,
              /* spdlog::level::critical   */ LOG_CRIT,
              /* spdlog::level::off        */ LOG_ALERT};
    }
};

journald: structured output for logger's name

Previously, the logger name was effectively lost. There were two choices on how to add it:

  • Via a formatter, which would mean that journalctl would not be able to filter against that. That would be suboptimal.

  • As a "syslog identifier". This means that journalctl will, by default, stop showing the daemon's executable name and replace that via the logger name. The PID is still shown, and if one would like to go back to the previous behavior, it is still possible via journalctl -o with-unit.

I think that the second option is strictly better than the first one.

fixes #1289

journald: fix source file location

This is what my manpage says, and what the original blog post says as well.

This is what my manpage says, and what the original blog post [1] says
as well.

Also, `sd_journal_send` can add the location of its own invocation to
the log. That's typically not what we want, so we have to suppress that
feature and instead put whatever is inside the spdlog message into the
journal.

[1] http://0pointer.de/blog/projects/journal-submit.html
Previously, the logger name was effectively lost. There were two choices
on how to add it:

- Via a formatter, which would mean that `journalctl` would not be able
to filter against that. That would be suboptimal.

- As a "syslog identifier". This means that `journalctl` will, by
default, stop showing the daemon's executable name and replace that via
the logger name. The PID is still shown, and if one would like to go
back to the previous behavior, it is still possible via `journalctl -o
with-unit`.

I think that the second option is strictly better than the first one.

fixes gabime#1289
@gabime
Copy link
Owner

gabime commented Oct 30, 2019

Thanks @jktjkt . Merged.

@jktjkt jktjkt deleted the journald branch October 30, 2019 19:45
bachittle pushed a commit to bachittle/spdlog that referenced this pull request Dec 22, 2022
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.

systemd-journald sink does not log logger name
2 participants