pub struct Output(_);
Expand description
Configuration for a logger output.
Implementations§
source§impl Output
impl Output
sourcepub fn file<T: Into<Cow<'static, str>>>(file: File, line_sep: T) -> Self
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"))
sourcepub fn writer<T: Into<Cow<'static, str>>>(
writer: Box<dyn Write + Send>,
line_sep: T
) -> Self
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"))
sourcepub fn reopen<T: Into<Cow<'static, str>>>(
reopen: Reopen<File>,
line_sep: T
) -> Self
pub fn reopen<T: Into<Cow<'static, str>>>( reopen: Reopen<File>, line_sep: T ) -> Self
Returns a reopenable logger, i.e., handling SIGHUP.
If the default separator of \n
is acceptable, a Reopen
instance can be passed into Dispatch::chain
directly.
This function is not available on Windows, and it requires the reopen-03
feature to be enabled.
use std::fs::OpenOptions;
let reopenable = reopen03::Reopen::new(Box::new(|| {
OpenOptions::new()
.create(true)
.write(true)
.append(true)
.open("/tmp/output.log")
}))
.unwrap();
fern::Dispatch::new().chain(fern::Output::reopen(reopenable, "\n"))
sourcepub fn reopen1<T: Into<Cow<'static, str>>>(
reopen: Reopen<File>,
line_sep: T
) -> Self
pub fn reopen1<T: Into<Cow<'static, str>>>( reopen: Reopen<File>, line_sep: T ) -> Self
Returns a reopenable logger, i.e., handling SIGHUP.
If the default separator of \n
is acceptable, a Reopen
instance can be passed into Dispatch::chain
directly.
This function is not available on Windows, and it requires the reopen-03
feature to be enabled.
use std::fs::OpenOptions;
let reopenable = reopen1::Reopen::new(Box::new(|| {
OpenOptions::new()
.create(true)
.write(true)
.append(true)
.open("/tmp/output.log")
}))
.unwrap();
fern::Dispatch::new().chain(fern::Output::reopen1(reopenable, "\n"))
sourcepub fn stdout<T: Into<Cow<'static, str>>>(line_sep: T) -> Self
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"))
sourcepub fn stderr<T: Into<Cow<'static, str>>>(line_sep: T) -> Self
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"))
sourcepub fn sender<T: Into<Cow<'static, str>>>(
sender: Sender<String>,
line_sep: T
) -> Self
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)
sourcepub fn syslog_5424<F>(
logger: Logger<LoggerBackend, (i32, HashMap<String, HashMap<String, String>>, String), Formatter5424>,
transform: F
) -> Selfwhere
F: Fn(&Record<'_>) -> (i32, HashMap<String, HashMap<String, String>>, String) + Sync + Send + 'static,
pub fn syslog_5424<F>( logger: Logger<LoggerBackend, (i32, HashMap<String, HashMap<String, String>>, String), Formatter5424>, transform: F ) -> Selfwhere F: Fn(&Record<'_>) -> (i32, HashMap<String, HashMap<String, String>>, String) + Sync + Send + 'static,
Returns a logger which logs into an RFC5424 syslog.
This method takes an additional transform method to turn the log data into RFC5424 data.
I’ve honestly got no clue what the expected keys and values are for this kind of logging, so I’m just going to link the rfc instead.
If you’re an expert on syslog logging and would like to contribute an example to put here, it would be gladly accepted!
This requires the "syslog-4"
feature.
sourcepub fn syslog6_5424<F>(
logger: Logger<LoggerBackend, Formatter5424>,
transform: F
) -> Selfwhere
F: Fn(&Record<'_>) -> (u32, HashMap<String, HashMap<String, String>>, String) + Sync + Send + 'static,
pub fn syslog6_5424<F>( logger: Logger<LoggerBackend, Formatter5424>, transform: F ) -> Selfwhere F: Fn(&Record<'_>) -> (u32, HashMap<String, HashMap<String, String>>, String) + Sync + Send + 'static,
Returns a logger which logs into an RFC5424 syslog (using syslog version 6)
This method takes an additional transform method to turn the log data into RFC5424 data.
I’ve honestly got no clue what the expected keys and values are for this kind of logging, so I’m just going to link the rfc instead.
If you’re an expert on syslog logging and would like to contribute an example to put here, it would be gladly accepted!
This requires the "syslog-6"
feature.
sourcepub fn call<F>(func: F) -> Selfwhere
F: Fn(&Record<'_>) + Sync + Send + 'static,
pub fn call<F>(func: F) -> Selfwhere 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 From<Box<Logger, Global>> for Output
impl From<Box<Logger, Global>> for Output
source§fn from(log: Box<Logger>) -> Self
fn from(log: Box<Logger>) -> Self
Creates an output logger which writes all messages to the given syslog output.
Log levels are translated trace => debug, debug => debug, info => informational, warn => warning, and error => error.
Note that while this takes a Box<Logger>
for convenience (syslog
methods return Box
es), it will be immediately unboxed upon storage
in the configuration structure. This will create a configuration
identical to that created by passing a raw syslog::Logger
.
This requires the "syslog-3"
feature.
source§impl From<Logger<LoggerBackend, Formatter3164>> for Output
impl From<Logger<LoggerBackend, Formatter3164>> for Output
source§fn from(log: Logger<LoggerBackend, Formatter3164>) -> Self
fn from(log: Logger<LoggerBackend, Formatter3164>) -> Self
Creates an output logger which writes all messages to the given syslog.
Log levels are translated trace => debug, debug => debug, info => informational, warn => warning, and error => error.
Note that due to https://github.com/Geal/rust-syslog/issues/41, logging to this backend requires one allocation per log call.
This is for RFC 3164 loggers. To use an RFC 5424 logger, use the
Output::syslog_5424
helper method.
This requires the "syslog-4"
feature.
source§impl From<Logger<LoggerBackend, String, Formatter3164>> for Output
impl From<Logger<LoggerBackend, String, Formatter3164>> for Output
source§fn from(log: Logger<LoggerBackend, String, Formatter3164>) -> Self
fn from(log: Logger<LoggerBackend, String, Formatter3164>) -> Self
Creates an output logger which writes all messages to the given syslog.
Log levels are translated trace => debug, debug => debug, info => informational, warn => warning, and error => error.
Note that due to https://github.com/Geal/rust-syslog/issues/41, logging to this backend requires one allocation per log call.
This is for RFC 3164 loggers. To use an RFC 5424 logger, use the
Output::syslog_5424
helper method.
This requires the "syslog-4"
feature.