log4rs icon indicating copy to clipboard operation
log4rs copied to clipboard

Logging always goes to redirected file.

Open jmigual opened this issue 1 year ago • 4 comments

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:

  1. ./myprogram > stdout.log
  2. ./myprogram 2> stderr.log
  3. ./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.

jmigual avatar Dec 11 '24 21:12 jmigual

Windows 11 23H2

jmigual avatar Dec 31 '24 09:12 jmigual

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

estk avatar May 25 '25 03:05 estk

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

jmigual avatar May 25 '25 13:05 jmigual

Great, let me know how you go

estk avatar May 25 '25 17:05 estk