fern

Struct Output

Source
pub struct Output(/* private fields */);
Expand description

Configuration for a logger output.

Implementations§

Source§

impl Output

Source

pub fn file<T: Into<Cow<'static, str>>>(file: File, line_sep: T) -> Self

Returns a file logger using a custom separator.

If the default separator of \n is acceptable, an fs::File instance can be passed into Dispatch::chain directly.

fern::Dispatch::new().chain(std::fs::File::create("log")?)
fern::Dispatch::new().chain(fern::log_file("log")?)

Example usage (using fern::log_file):

fern::Dispatch::new().chain(fern::Output::file(fern::log_file("log")?, "\r\n"))
Source

pub fn writer<T: Into<Cow<'static, str>>>( writer: Box<dyn Write + Send>, line_sep: T, ) -> Self

Returns a logger using arbitrary write object and custom separator.

If the default separator of \n is acceptable, an Box<Write + Send> instance can be passed into Dispatch::chain directly.

// Anything implementing 'Write' works.
let mut writer = std::io::Cursor::new(Vec::<u8>::new());

fern::Dispatch::new()
    // as long as we explicitly cast into a type-erased Box
    .chain(Box::new(writer) as Box<std::io::Write + Send>)

Example usage:

let writer = Box::new(std::io::Cursor::new(Vec::<u8>::new()));

fern::Dispatch::new().chain(fern::Output::writer(writer, "\r\n"))
Source

pub fn stdout<T: Into<Cow<'static, str>>>(line_sep: T) -> Self

Returns an stdout logger using a custom separator.

If the default separator of \n is acceptable, an io::Stdout instance can be passed into Dispatch::chain() directly.

fern::Dispatch::new().chain(std::io::stdout())

Example usage:

fern::Dispatch::new()
    // some unix tools use null bytes as message terminators so
    // newlines in messages can be treated differently.
    .chain(fern::Output::stdout("\0"))
Source

pub fn stderr<T: Into<Cow<'static, str>>>(line_sep: T) -> Self

Returns an stderr logger using a custom separator.

If the default separator of \n is acceptable, an io::Stderr instance can be passed into Dispatch::chain() directly.

fern::Dispatch::new().chain(std::io::stderr())

Example usage:

fern::Dispatch::new().chain(fern::Output::stderr("\n\n\n"))
Source

pub fn sender<T: Into<Cow<'static, str>>>( sender: Sender<String>, line_sep: T, ) -> Self

Returns a mpsc::Sender logger using a custom separator.

If the default separator of \n is acceptable, an mpsc::Sender<String> instance can be passed into Dispatch:: chain() directly.

Each log message will be suffixed with the separator, then sent as a single String to the given sender.

use std::sync::mpsc::channel;

let (tx, rx) = channel();
fern::Dispatch::new().chain(tx)
Source

pub fn call<F>(func: F) -> Self
where F: Fn(&Record<'_>) + Sync + Send + 'static,

Returns a logger which simply calls the given function with each message.

The function will be called inline in the thread the log occurs on.

Example usage:

fern::Dispatch::new().chain(fern::Output::call(|record| {
    // this is mundane, but you can do anything here.
    println!("{}", record.args());
}))

Trait Implementations§

Source§

impl Debug for Output

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&'static dyn Log> for Output

Source§

fn from(log: &'static dyn Log) -> Self

Creates an output logger forwarding all messages to the custom logger.

Source§

impl From<Box<dyn Log>> for Output

Source§

fn from(log: Box<dyn Log>) -> Self

Creates an output logger forwarding all messages to the custom logger.

Source§

impl From<Box<dyn Write + Send>> for Output

Source§

fn from(writer: Box<dyn Write + Send>) -> Self

Creates an output logger which writes all messages to the writer with \n as the separator.

This does no buffering and it is up to the writer to do buffering as needed (eg. wrap it in BufWriter). However, flush is called after each log record.

Source§

impl From<Dispatch> for Output

Source§

fn from(log: Dispatch) -> Self

Creates an output logger forwarding all messages to the dispatch.

Source§

impl From<File> for Output

Source§

fn from(file: File) -> Self

Creates an output logger which writes all messages to the file with \n as the separator.

File writes are buffered and flushed once per log record.

Source§

impl From<Manual> for Output

Source§

fn from(config: Manual) -> Self

Create an output logger which defers to the given manual logger. Use configuration methods on Manual to set line separator and filename.

Source§

impl From<Panic> for Output

Source§

fn from(_: Panic) -> Self

Creates an output logger which will panic with message text for all messages.

Source§

impl From<Sender<String>> for Output

Source§

fn from(stream: Sender<String>) -> Self

Creates an output logger which writes all messages to the given mpsc::Sender with ‘\n’ as the separator.

All messages sent to the mpsc channel are suffixed with ‘\n’.

Source§

impl From<Stderr> for Output

Source§

fn from(stream: Stderr) -> Self

Creates an output logger which writes all messages to stderr with the given handle and \n as the separator.

Source§

impl From<Stdout> for Output

Source§

fn from(stream: Stdout) -> Self

Creates an output logger which writes all messages to stdout with the given handle and \n as the separator.

Auto Trait Implementations§

§

impl Freeze for Output

§

impl !RefUnwindSafe for Output

§

impl Send for Output

§

impl !Sync for Output

§

impl Unpin for Output

§

impl !UnwindSafe for Output

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.