rust-cli / env_logger

A logging implementation for `log` which is configured via an environment variable.
https://docs.rs/env_logger
Apache License 2.0
803 stars 125 forks source link

Target::Pipe doesn't work #208

Closed oxalica closed 1 year ago

oxalica commented 3 years ago

In #204, we introduce Target::Pipe to redirect log messages into custom writer. And it's included in 0.8.4 now. But when I try the example in that PR, it doesn't print the message from pipe at all, and not even call WriteAdapter::write. The log::error message just go straight to stderr. How to make it catch all log message into my custom writer object?

TilCreator commented 3 years ago

I also just noticed that. I read the code (of #204), but never actually tested it... I kind of just assumed that it would work, because it didn't change much of my code (#195). I'm currently looking into it (I also already found at least one error, but it's just in the documentation)

jplatte commented 3 years ago

Thanks for looking into it, and sorry for breaking things.

TilCreator commented 3 years ago

I have found the error in fmt::writer::termcolor::extern_impl::BufferWriter The change removed the target_pipe which is good, less convoluted. In fmt::writer::termcolor::extern_impl::BufferWriter::print the if for the pipe was removed and integrated into the match a few lines down, but that only works if there is a test_target, because BufferWriter::inner (a termcolor::BufferWriter) is unable to write to the pipe.

TilCreator commented 3 years ago

A easy solution would be to change BufferWriter::pipe to always set test_target, but that feels quite hacky... (it also removes color, so not really a solution)

jplatte commented 3 years ago

I think we should no longer unconditionally hold that BufferWriter, it doesn't make sense for the pipe target.

it also removes color, so not really a solution

Removes color in general, or when using a Pipe writer? For the latter, I would expect colorization to be turned off. You could print the escape sequences, but that's unix-specific and would lead to very ugly output if it's not expected on the "other side" of the pipe.

TilCreator commented 3 years ago

I explicitly enabled colors, else the default for pipes would be to disable color.

TilCreator commented 3 years ago

I will try the idea without the BufferWriter

TilCreator commented 3 years ago

I think we should no longer unconditionally hold that BufferWriter, it doesn't make sense for the pipe target.

Something like this? https://github.com/TilCreator/env_logger/commit/fbeebcf1081815b61568fb410b8dbb9747c57e0b

tubzby commented 3 years ago

Same problem here.

Polochon-street commented 3 years ago

Can report having the same problem - that would be a really awesome addition if that worked though :smile:

TilCreator commented 3 years ago

Ups, it has been a few days... Sry, I opened #211 which fixes this (but it's not perfect).

mainrs commented 3 years ago

I do also think that colors disabled makes more sense for pipes (by default). We could think about adding some kind of way to enable them back again.

TilCreator commented 3 years ago

It is already disabled by default for pipes, the example just enables them to show that it's possible.

harlanc commented 3 years ago

Is this problem fixed now? Both 0.8.4 and 0.9 versions have problems..

jplatte commented 3 years ago

Seems like #211 would fix this.

harlanc commented 3 years ago

when will this patch be merged and released? @TilCreator

TilCreator commented 3 years ago

No idea, but I hope soon

tubzby commented 2 years ago

Any news on this? I think this is a critical issue because I can't imagine a process without a log file in the production environment.

jplatte commented 2 years ago

217 contains an incomplete fix for this. I'm not sure it can really be completed without breaking changes though.

TilCreator commented 2 years ago

I'm quite busy right now, so I will sadly not be of much help, sry (also my non critical applications currently work fine with #211)

yihuaf commented 2 years ago

@jplatte Any update? Can we re-open #211 if it fix the issue? Being able to log to file is a critical feature.

jplatte commented 2 years ago

I think you'd be better off just using a different logger like simplelog if you need file output. If you really want it work in env_logger, you could try working on #217.

hasezoey commented 2 years ago

just encountered this issue as well, it is confusing that this feature (Target::Pipe) does not work (silently) without having documentation mentioning that it does not work

also is there any progress on #217 to be ready for merge?

jplatte commented 2 years ago

Yeah, I suppose some docs plus a deprecated attribute (because that is the only way you can make usage of it warn, AFAIK) would be a good interim solution. There has been no progress on #217, help wanted.

SolraBizna commented 2 years ago

I just burned some debugging time trying to figure out why my custom pipe writer wasn't working, only to stumble upon this issue. There has, according to my testing, never been a published version of this crate where this feature worked. I am vexed!

"Make your own logger" isn't a good solution, because then we give up env_logger's flagship (and titular) feature, which is its simple and powerful system by which the user specifies which log messages they're interested in.

I intend to put my money where my mouth is and throw myself at fixing this today or tomorrow.

niklasha commented 2 years ago

Heh, I too burnt hours debugging this, and made a fix of my own before checking the issues. This really ought to be fixed, either by correcting the bug (easy enough), or.. if it is easier, just by releasing a new version with fixed documentation (where it is noted that Pipe does not work unless in test context). If it is documented as working, people will continue to trip on this.

niklasha commented 2 years ago

and for the record: this was how I fixed it locally: https://github.com/niklasha/env_logger/tree/issue-208-pipe-fix This is basically just making the uncolored case be used always with Pipe, be it in testing context or not. I know there is already a rejected proposal, but i thought I'd at least share what I did, should anyone like it.

whirm commented 2 years ago

I've also been a victim of this. I spent hours debugging this until I realized not even the example worked.

walkie commented 2 years ago

@niklasha:

this was how I fixed it locally: https://github.com/niklasha/env_logger/tree/issue-208-pipe-fix This is basically just making the uncolored case be used always with Pipe, be it in testing context or not. I know there is already a rejected proposal, but i thought I'd at least share what I did, should anyone like it.

I've read through all of the various issues and pull requests, and there seems to be a consensus that disabling color for pipe targets (at least initially) is acceptable.

For example, this comment from @mainrs:

I do also think that colors disabled makes more sense for pipes (by default). We could think about adding some kind of way to enable them back again.

Is there any reason that @niklasha's patch is not a valid (perhaps interim) solution to this issue?

If not, I'm happy to create a small PR updating the documentation to clarify that this feature is only supported in test contexts. It's definitely confusing in its current state, as the last few commenters here can attest. :-)

jplatte commented 2 years ago

That approach sounds reasonable, I'd be happy about a PR.

SolraBizna commented 2 years ago

My PR over at the termcolor repo will make a simple path for enabling color for pipe targets down the road. But it doesn't do us any good until it's on crates.io.

TilCreator commented 2 years ago

@walkie I thinks that color output should be disabled be default, but I think it still is an important feature, for example I use the color output to combine the indicatif progress bar and pretty env logger in https://gitlab.com/TilCreator/rustycomicscraper/-/blob/master/src/main.rs

jplatte commented 1 year ago

I've published version 0.9.1 that deprecates Target::Pipe because nobody is working on fixing it.

niklasha commented 1 year ago

I've published version 0.9.1 that deprecates Target::Pipe because nobody is working on fixing it.

I offer to fix it, if I only knew what the requirements on a solution are. I have already devised a solution that "works for me" above: https://github.com/env-logger-rs/env_logger/issues/208#issuecomment-1173592691 but I understand you have issues with it?

jplatte commented 1 year ago

I already told you that I would be happy to review a PR with your patch, but I don't think you created one?

niklasha commented 1 year ago

Eh, you did? I must have misunderstood then. I will prepare a PR.

epage commented 1 year ago

@jplatte did 84b701dac94e4135f59f069a8c155bb9b7922788 resolve this Issue?

jplatte commented 1 year ago

It should have, yes.