Struct CollectdLoggerBuilder

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

Bridges the gap between collectd and rust logging. Terminology and filters methods found here are from env_logger.

It is recommended to instantiate the logger in PluginManager::plugins.

The use case of multiple rust plugins that instantiate a global logger is supported. Each plugin will have its own copy of a global logger. Thus plugins won’t interefere with others and their logging.

§Example

use collectd_plugin::{ConfigItem, PluginManager, PluginRegistration, CollectdLoggerBuilder};
use log::LevelFilter;
use std::error;

#[derive(Default)]
struct MyPlugin;
impl PluginManager for MyPlugin {
    fn name() -> &'static str {
        "myplugin"
    }

    fn plugins(_config: Option<&[ConfigItem]>) -> Result<PluginRegistration, Box<error::Error>> {
       CollectdLoggerBuilder::new()
           .prefix_plugin::<Self>()
           .filter_level(LevelFilter::Info)
           .try_init()
           .expect("really the only thing that should create a logger");
        unimplemented!()
    }
}

Implementations§

Source§

impl CollectdLoggerBuilder

Source

pub fn new() -> Self

Initializes the log builder with defaults

Source

pub fn try_init(&mut self) -> Result<(), SetLoggerError>

Initializes the global logger with the built collectd logger.

§Errors

This function will fail if it is called more than once, or if another library has already initialized a global logger.

Source

pub fn prefix_plugin<T: PluginManager>(&mut self) -> &mut Self

Prefixes all log messages with a plugin’s name. This is recommended to aid debugging and gain insight into collectd logs.

Source

pub fn filter_level(&mut self, level: LevelFilter) -> &mut Self

Source

pub fn filter_module(&mut self, module: &str, level: LevelFilter) -> &mut Self

Source

pub fn filter(&mut self, module: Option<&str>, level: LevelFilter) -> &mut Self

Source

pub fn parse(&mut self, filters: &str) -> &mut Self

Source

pub fn format<F>(&mut self, format: F) -> &mut Self
where F: Fn(&mut dyn Write, &Record<'_>) -> Result<()> + Sync + Send + 'static,

Sets the format function for formatting the log output.

Trait Implementations§

Source§

impl Default for CollectdLoggerBuilder

Source§

fn default() -> CollectdLoggerBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.