pub struct LogWriter { /* private fields */ }
Expand description
Wrapper around a Write-capable type that filters the types of messages that are written to it.
This can be used to customize the output source of the HumanLogger.
§Examples
A logger that writes to stdout only, instead of mixed stdout/stderr (like usual).
use humanlog::{ColourChoice, DebugMode, HumanLogger, LogWriter};
use log::{debug, info, error, Level};
let logger: LogWriter = LogWriter::new(std::io::stdout(), ColourChoice::Auto, vec![ Level::Error, Level::Warn, Level::Info, Level::Debug, Level::Trace ], "stdout");
if let Err(err) = HumanLogger::new(vec![ logger ], DebugMode::Debug).init() {
eprintln!("WARNING: Failed to initialize logger: {err} (no logging enabled for this session)");
}
A logger that writes to a file instead of stdout/stderr.
use std::fs::File;
use humanlog::{ColourChoice, DebugMode, HumanLogger, LogWriter};
use log::{debug, info, error, Level};
// Open a file
match File::create("output.log") {
Ok(handle) => {
// Use that to create a writer that receives everything
let logger: LogWriter = LogWriter::new(handle, ColourChoice::No, vec![ Level::Error, Level::Warn, Level::Info, Level::Debug, Level::Trace ], "file");
if let Err(err) = HumanLogger::new(vec![ logger ], DebugMode::Debug).init() {
eprintln!("WARNING: Failed to initialize logger: {err} (no logging enabled for this session)");
}
},
Err(err) => {
eprintln!("WARNING: Failed to initialize logger: Failed to create file 'output.log': {err} (no logging enabled for this session)");
},
}
A logger that writes to a file in addition to the standard stdout/stderr logging.
use std::fs::File;
use humanlog::{ColourChoice, DebugMode, HumanLogger, LogWriter};
use log::{debug, info, error, Level};
// Open a file
match File::create("output.log") {
Ok(handle) => {
// Create three LogWriters: one per output stream
let stdout_logger: LogWriter = LogWriter::stdout();
let stderr_logger: LogWriter = LogWriter::stderr();
// Note the repeated levels; the logger will simply log to all LogWriters that want that particular level
let file_logger: LogWriter = LogWriter::new(handle, ColourChoice::No, vec![ Level::Error, Level::Warn, Level::Info, Level::Debug, Level::Trace ], "file");
// Finally, we put it all in one logger
if let Err(err) = HumanLogger::new(vec![ stdout_logger, stderr_logger, file_logger ], DebugMode::Debug).init() {
eprintln!("WARNING: Failed to initialize logger: {err} (no logging enabled for this session)");
}
},
Err(err) => {
eprintln!("WARNING: Failed to initialize logger: Failed to create file 'output.log': {err} (no logging enabled for this session)");
},
}
Implementations§
Source§impl LogWriter
impl LogWriter
Sourcepub fn stdout() -> Self
pub fn stdout() -> Self
Default constructor for the LogWriter that initializes it for stdout.
By default, will use automatic colour selection and only logs trace, debug and info-messages.
§Returns
A new LogWriter instance that can be used to log to stdout.
§Examples
use humanlog::{DebugMode, HumanLogger, LogWriter};
// Will emulate the default behaviour of writing `Level::Error` and `Level::Warn` to stderr, the rest to stdout.
if let Err(err) = HumanLogger::new(vec![ LogWriter::stdout(), LogWriter::stderr() ], DebugMode::Debug).init() {
eprintln!("WARNING: Failed to initialize logger: {err} (no logging enabled for this session)");
}
Sourcepub fn stderr() -> Self
pub fn stderr() -> Self
Default constructor for the LogWriter that initializes it for stderr.
By default, will use automatic colour selection and only logs warning and error message.
§Returns
A new LogWriter instance that can be used to log to stderr.
§Examples
use humanlog::{DebugMode, HumanLogger, LogWriter};
// Will emulate the default behaviour of writing `Level::Error` and `Level::Warn` to stderr, the rest to stdout.
if let Err(err) = HumanLogger::new(vec![ LogWriter::stdout(), LogWriter::stderr() ], DebugMode::Debug).init() {
eprintln!("WARNING: Failed to initialize logger: {err} (no logging enabled for this session)");
}
Sourcepub fn new(
writer: impl 'static + Send + Sync + Write,
colour: ColourChoice,
filter: impl Into<Vec<Level>>,
label: impl Into<String>,
) -> Self
pub fn new( writer: impl 'static + Send + Sync + Write, colour: ColourChoice, filter: impl Into<Vec<Level>>, label: impl Into<String>, ) -> Self
Constructor for the LogWriter that wraps it around the given Write
r.
§Arguments
writer
: The handle or other object that implementWrite
and we will write to.colour_choice
: Whether to enable ANSI colours for this file or not.filter
: The list of Levels that are only allowed to be written to this writer.label
: Some description of the writer for debugging purposes.
§Returns
A new LogWriter instance that wraps around the given writer
.
§Examples
use humanlog::{ColourChoice, DebugMode, HumanLogger, LogWriter};
use log::Level;
// Will only ever write to stdout, regardless of the log type
let logger: LogWriter = LogWriter::new(std::io::stdout(), ColourChoice::Auto, vec![ Level::Error, Level::Warn, Level::Info, Level::Debug, Level::Trace ], "stdout");
if let Err(err) = HumanLogger::new(vec![ logger ], DebugMode::Debug).init() {
eprintln!("WARNING: Failed to initialize logger: {err} (no logging enabled for this session)");
}