You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
env_logger doesn't support redirecting the output to custom pipe. They have Target but it's broken for a year now and they doesn't seem to care much about it.
simplelog is able to redirect the messages to custom pipes. This would allow me to capture logs in integration tests (not sute if it's a good idea yet). I would be also easier to redirect logs to the file.
Example
This example shows combined logger where ERROR and WARN messages are printed to the console with colors while INFO, WARN and ERROR messages are additionaly captured and stored in a channel waiting to be extracted later.
// [dependencies]// log = "0.4.0"// simplelog = "0.11.1"#[macro_use]externcrate log;externcrate simplelog;use simplelog::*;use std::{
io,
sync::mpsc::{channel,Sender},};structWriteAdapter{sender:Sender<u8>,}impl io::WriteforWriteAdapter{fnwrite(&mutself,buf:&[u8]) -> io::Result<usize>{for chr in buf {self.sender.send(*chr).unwrap();}Ok(buf.len())}fnflush(&mutself) -> io::Result<()>{Ok(())}}fnsetup_logger() -> std::sync::mpsc::Receiver<u8>{let(rx, tx) = channel();let my_adapter = WriteAdapter{sender: rx };CombinedLogger::init(vec![TermLogger::new(LevelFilter::Warn,Config::default(),TerminalMode::Mixed,ColorChoice::Auto),WriteLogger::new(LevelFilter::Info,Config::default(), my_adapter),]).unwrap();return tx;}fnmain(){let tx = setup_logger();trace!("some trace log");// won't be printeddebug!("some debug log");// won't be printedinfo!("some information log");// from pipewarn!("some warning log");// console & from pipeerror!("some error log");// console & from pipe// Collect all messages send to the channel and parse the result as a stringString::from_utf8(tx.try_iter().collect::<Vec<u8>>()).unwrap()// Split the result into lines so a prefix can be added to each line.split('\n').for_each(|msg| {// Print the message with a prefix if it has any contentif !msg.is_empty(){println!("from pipe: {}", msg)}});}
The text was updated successfully, but these errors were encountered:
env_logger doesn't support redirecting the output to custom pipe. They have
Target
but it's broken for a year now and they doesn't seem to care much about it.simplelog is able to redirect the messages to custom pipes. This would allow me to capture logs in integration tests (not sute if it's a good idea yet). I would be also easier to redirect logs to the file.
Example
This example shows combined logger where ERROR and WARN messages are printed to the console with colors while INFO, WARN and ERROR messages are additionaly captured and stored in a channel waiting to be extracted later.
The text was updated successfully, but these errors were encountered: