icinga2/lib/base/journaldlogger.hpp
Tobias Deiminger 173caa42aa Add a JournaldLogger
As proposed in #8857, this adds a Logger subclass that writes structured
log messages via journald's native protocol by calling sd_journal_sendv.
The feature therefore depends on the systemd library. sd_journal_sendv is
available since the early days (systemd v38), so a version check is
probably superflous.

We add the following fields to each record:
- MESSAGE: The log message
- PRIORITY (aka severity): Numeric severity as in RFC5424 section 6.2.1
- SYSLOG_FACILITY: Numeric facility as in RFC5424 section 6.2.1
- SYSLOG_IDENTIFIER: If provided, use value from configuration.
  Else use systemd's default behaior, which is to determine the field
  by using libc's program_invocation_short_name, resulting in "icinga2".
- ICINGA2_FACILITY: Facility as in Log::Log(..., String facility, ...),
  e.g. "ApiListener"
- some more fields are added automatically by systemd

Fields are stored indexed, so we can do fast queries for certain field
values. Example:

$ journalctl -t icinga2 ICINGA2_FACILITY=ApiListener -n 5

Syslog compatiblity is ratained because good old tag, severity and facility
is stored along, and systemd can forward to syslog daemons.

See also https://systemd.io/JOURNAL_NATIVE_PROTOCOL/.
2021-09-23 16:08:11 +02:00

45 lines
1.0 KiB
C++

/* Icinga 2 | (c) 2021 Icinga GmbH | GPLv2+ */
#ifndef JOURNALDLOGGER_H
#define JOURNALDLOGGER_H
#include "base/i2-base.hpp"
#if !defined(_WIN32) && defined(HAVE_SYSTEMD)
#include "base/journaldlogger-ti.hpp"
#include <sys/uio.h>
namespace icinga
{
/**
* A logger that logs to systemd journald.
*
* @ingroup base
*/
class JournaldLogger final : public ObjectImpl<JournaldLogger>
{
public:
DECLARE_OBJECT(JournaldLogger);
DECLARE_OBJECTNAME(JournaldLogger);
static void StaticInitialize();
static void StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata);
void OnConfigLoaded() override;
void ValidateFacility(const Lazy<String>& lvalue, const ValidationUtils& utils) override;
protected:
void SystemdJournalSend(const std::vector<String>& varJournalFields) const;
static struct iovec IovecFromString(const String& s);
std::vector<String> m_ConfiguredJournalFields;
void ProcessLogEntry(const LogEntry& entry) override;
void Flush() override;
};
}
#endif /* !_WIN32 && HAVE_SYSTEMD */
#endif /* JOURNALDLOGGER_H */