pub struct JournalLog { /* private fields */ }
Expand description

A systemd journal logger.

Journal access

Standard fields

The journald logger always sets the following standard journal fields:

  • PRIORITY: The log level mapped to a priority (see below).
  • MESSAGE: The formatted log message (see log::Record::args()).
  • SYSLOG_PID: The PID of the running process (see std::process::id()).
  • CODE_FILE: The filename the log message originates from (see log::Record::file(), only if present).
  • CODE_LINE: The line number the log message originates from (see log::Record::line(), only if present).

It also sets SYSLOG_IDENTIFIER if non-empty (see JournalLog::with_syslog_identifier).

Additionally it also adds the following non-standard fields:

Log levels and Priorities

log::Level gets mapped to journal (syslog) priorities as follows:

Higher priorities (crit, alert, and emerg) are not used.

Custom fields and structured record fields

In addition to these fields the logger also adds all structures key-values (see log::Record::key_values) from each log record as journal fields, and also supports global extra fields via Self::with_extra_fields.

Journald allows only ASCII uppercase letters, ASCII digits, and the underscore in field names, and limits field names to 64 bytes. See upstream’s journal_field_valid for the precise validation rules.

This logger mangles the keys of additional key-values on records and names of custom fields according to the following rules, to turn them into valid journal fields:

  • If the key is entirely empty, use EMPTY.
  • Transform the entire value to ASCII uppercase.
  • Replace all invalid characters with underscore.
  • If the key starts with an underscore or digit, which is not permitted, prepend ESCAPED_.
  • Cap the result to 64 bytes.

Errors

The logger tries to connect to journald when constructed, to provide early on feedback if journald is not available (e.g. in containers where the journald socket is not mounted into the container).

Later on, the logger simply ignores any errors when sending log records to journald, simply because the log interface does not expose faillible operations.

Implementations§

source§

impl JournalLog

source

pub fn new() -> Result<Self>

Create a journal log instance with a default syslog identifier.

source

pub fn empty() -> Result<Self>

Create an empty journal log instance, with no extra fields and no syslog identifier.

See Self::with_syslog_identifier and Self::with_extra_fields to set either. It’s recommended to at least set the syslog identifier.

source

pub fn install(self) -> Result<(), SetLoggerError>

Install this logger globally.

See log::set_boxed_logger.

source

pub fn add_extra_field<K: AsRef<str>, V: AsRef<[u8]>>( self, name: K, value: V ) -> Self

Add an extra field to be added to every log entry.

name is the name of a custom field, and value its value. Fields are appended to every log entry, in order they were added to the logger.

Restrictions on field names

name should be a valid journal file name, i.e. it must only contain ASCII uppercase alphanumeric characters and the underscore, and must start with an ASCII uppercase letter.

Invalid keys in extra_fields are escaped according to the rules documented in JournalLog.

It is not recommended that name is any of the standard fields already added by this logger (see JournalLog); though journald supports multiple values for a field, journald clients may not handle unexpected multi-value fields properly and perhaps only show the first value. Specifically, even journalctl will only shouw the first MESSAGE value of journal entries.

Restrictions on values

There are no restrictions on the value.

source

pub fn with_extra_fields<I, K, V>(self, extra_fields: I) -> Selfwhere I: IntoIterator<Item = (K, V)>, K: AsRef<str>, V: AsRef<[u8]>,

Set extra fields to be added to every log entry.

Remove all previously added fields.

See Self::add_extra_field for details.

source

pub fn with_syslog_identifier(self, identifier: String) -> Self

Set the given syslog identifier for this logger.

The logger writes this string in the SYSLOG_IDENTIFIER field, which can be filtered for with journalctl -t.

Use current_exe_identifier() to obtain the standard identifier for the current executable.

source

pub fn journal_send(&self, record: &Record<'_>) -> Result<()>

Send a single log record to the journal.

Extract all fields (standard and custom) from record (see [JournalLog]), append all extra_fields` given to this logger, and send the result to journald.

Trait Implementations§

source§

impl Log for JournalLog

The Log interface for JournalLog.

source§

fn enabled(&self, _metadata: &Metadata<'_>) -> bool

Whether this logger is enabled.

Always returns true.

source§

fn log(&self, record: &Record<'_>)

Send the given record to the systemd journal.

Errors

Ignore any errors which occur when sending record to journald because we cannot reasonably handle them at this place.

See JournalLog::journal_send for a function which returns any error which might have occurred while sending the record to the journal.

source§

fn flush(&self)

Flush log records.

A no-op for journal logging.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.