Logging always goes to redirected file.
I have the following config:
let my_level = LevelFilter::Debug;
let mut root_builder = Root::builder().appender("stderr");
let stdout = ConsoleAppender::builder()
.target(Target::Stderr)
.encoder(Box::new(PatternEncoder::new(
"{d(%Y-%m-%d %H:%M:%S)} {h({l})} {t}: {m}{n}",
)))
.build();
let mut config_builder = LogConfig::builder().appender(
Appender::builder()
.filter(Box::new(ThresholdFilter::new((&my_level).into())))
.build("stderr", Box::new(stdout)),
);
let root = root_builder.build(LevelFilter::Trace);
let handle = log4rs::init_config(config_builder.build(root)?)?;
Now, I've tried to run my program in Windows in the following ways:
./myprogram > stdout.log./myprogram 2> stderr.log./myprogram > stdout.log 2> stderr.log
In both versions 1 and 2, the logging is written to the output log file and not to the console. Version 3 is as expected, and the logging is only written to stderr.log.
Windows 11 23H2
Hi @jmigual sorry for the delay. What shell are you using? The first case I would expect to log to the console since you're only redirecting stdout. The second case I would think should work. No idea about the third.
I created a playground here of a standalone version: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=1350f921e006ce689564c0f83e8926be
@jmigual do you know if there is an equivalent of annotate-output for windows? https://linux.die.net/man/1/annotate-output
Hi! I was using PowerShell Core 7 on Windows 11 as my shell. I'll test the playground version in a couple of weeks since I'm not available right now. Remember me if I forget 😓
I didn't find an equivalent of annotate-output on Windows. A quick search on ChatGPT gave me this:
function Annotate-Output {
param (
[Parameter(Mandatory=$true)]
[scriptblock]$Command
)
$CommandOutput = & $Command | ForEach-Object {
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff"
"$timestamp $_"
}
$CommandOutput
}
# Example usage
Annotate-Output { ping google.com -n 4 }
Which seems to work fine
Great, let me know how you go