Struct Logger

Source
pub struct Logger { /* private fields */ }
Expand description

Logger for sending log-messages

A Logger instance can be either used as a standalone object to log directly to a log-server or it can be installed as a log-crate log-handler (with Logger::install).

By default all encountered errors will be silently ignored. If you want the logger to panic when an error occurs, you can change the behaviour with Logger::enable_panic_on_error.

Implementations§

Source§

impl Logger

Source

pub fn new(backend: Box<dyn Backend>) -> Result<Self>

Construct a new Logger instance

The backend needs to be boxed for usage as a logger with the log-crate. This constructor tries to determine the local hostname (required by GELF) with the help of the hostname-crate. If you want to set a custom hostname check out the Logger::new_with_hostname constructor.

Source

pub fn new_with_hostname(backend: Box<dyn Backend>, hostname: &str) -> Logger

Construct a new Logger instance with predetermined hostname

The backend needs to be boxed for usage as a logger with the log-crate. It uses the passed hostname for the GELF host field

Source

pub fn install<T: Into<LevelFilter>>(self, log_level: T) -> Result<()>

Install a logger instance as a log-Logger

This method wraps log::set_logger as a convenience function as required by the log-crate. log_level defines the maximum log-level the logger should log.

Note that installing the logger consumes it. To uninstall you need to call log::shutdown_logger which returns the boxed, original Logger instance.

Source

pub fn log_message(&self, msg: Message<'_>)

Log a message via the logger’s transport to a GELF server.

The logger will automatically add default_metadata fields to the message if missing in the passed Message.

Source

pub fn hostname(&self) -> &String

Return the hostname used for GELF’s host-field

Source

pub fn set_hostname<S: Into<String>>(&mut self, hostname: S) -> &mut Self

Set the hostname used for GELF’s host-field

Source

pub fn default_metadata(&self) -> &HashMap<String, String>

Return all default metadata

Source

pub fn set_default_metadata<S, T>(&mut self, key: S, value: T) -> &mut Self
where S: Into<String>, T: Into<String>,

Set a default metadata field

Every logged Message is checked for every default_metadata field. If it contains an entry with the key, the default is ignored. But if there is no additional information present, the default is added to the message.

This can be used for example to add a facility to every message:

logger.set_default_metadata(String::from("facility"), String::from("my_awesome_rust_service"));

logger.log_message(Message::new(String::from("This is important information")));
// -> The message will contain an additional field "_facility" with the value "my_awesome_rust_service"
Source

pub fn panic_on_error(&self) -> bool

Return a flag whether the logger panics when it encounters an error

Source

pub fn enable_panic_on_error(&mut self) -> &mut Self

Force the logger to panic when it encounters an error

Source

pub fn disable_panic_on_error(&mut self) -> &mut Self

Force the logger to ignore an encountered error silently

Trait Implementations§

Source§

impl Log for Logger

Source§

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

Determines if a log message with the specified metadata would be logged.

See docs for more details

Source§

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

Logs the LogRecord. See docs for more details

Source§

fn flush(&self)

Flushes any buffered records.

Auto Trait Implementations§

§

impl Freeze for Logger

§

impl !RefUnwindSafe for Logger

§

impl Send for Logger

§

impl Sync for Logger

§

impl Unpin for Logger

§

impl !UnwindSafe for Logger

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V