Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No colored output (with default settings and "colors" feature activated) #64

Open
d4h0 opened this issue Sep 3, 2020 · 21 comments
Open

Comments

@d4h0
Copy link

d4h0 commented Sep 3, 2020

Hi,

Thanks for creating 'flexi_logger'!

For some reason, I don't get any colored output with the default settings (or any other settings I tried).

When I run cargo run --example colors, however, I get colored output (the output of Cargo is colored, as well).

Do you have any idea what the problem could be?

I'm using flexi_logger = "0.15.12" in Cargo.toml (so the "colors" feature is activated), and besides the defaults I have tested the following:

    let logger_config = "warn, main=trace, devtools_protocol_client=trace";
    flexi_logger::Logger::with_str(logger_config)
        .check_parser_error()
        .expect("Logger config string formatted incorrectly")
        .log_target(flexi_logger::LogTarget::StdOut)
        .format_for_stdout(flexi_logger::colored_detailed_format)
        .format_for_stderr(flexi_logger::colored_detailed_format)
        .start()
        .expect("Failed to start logger");

It would be nice if it somehow would be possible to activate colored output, because 'flexi_logger' looks really nice :)

@emabee
Copy link
Owner

emabee commented Sep 4, 2020

I can't reproduce the issue so far :-|
What environment (OS version, terminal, rustc version) are you working in?

@d4h0
Copy link
Author

d4h0 commented Sep 4, 2020

I'm using Arch Linux (which is a rolling-release Linux distribution, so all my software should pretty much be up-to-date. My Linux kernel version is 5.8.5-arch1-1).

rustc: 1.46.0 (04488afe3 2020-08-24)

I tried the following terminals: Kitty, Alacritty, and directly on a TTY (so no graphical user interface and no virtual terminal emulator).

What is odd, is that colored output works with every other logging crate that I have tested (that supports colored output).

Is there anything I do to help to debug this? Of all the logging crates I have tested, I like flexi_logger the most (but colored output is essential for me).

@emabee
Copy link
Owner

emabee commented Sep 4, 2020

Did you set the environment variable FLEXI_LOGGER_PALETTE?

@d4h0
Copy link
Author

d4h0 commented Sep 4, 2020

I have tried to set FLEXI_LOGGER_PALETTE (copied from the docs), but it didn't change anything (otherwise that variable is unset).

@emabee
Copy link
Owner

emabee commented Sep 5, 2020

Are you piping the output explicitly through other programs? Since 0.15.8, colors are switched off if the output does not go to a terminal.
You could test with 0.15.7 to see if this is the point. If so, I could add an option to force coloring.

@d4h0
Copy link
Author

d4h0 commented Sep 5, 2020

I'm not piping the output through anything. I just run 'cargo run -p test-drive' to run the 'main' function of the main binary crate in a cargo workspace. I even tested to 'ctrl-alt-F2' to a TTY (so there isn't even a terminal program or a graphical user interface (XServer in my case).

But there still is no color.

And as I wrote other logging crates (which I like much less than flexi_logger) work without a problem.

Very strange, isn't it? :)

@d4h0
Copy link
Author

d4h0 commented Sep 5, 2020

I now also tested in the popular 'rxvt-unicode' terminal emulator, but still no color.

@emabee
Copy link
Owner

emabee commented Sep 19, 2020

I still have no clue what can go wrong.
Version 0.16.0 now comes with an extended color example. It would be interesting to see its output in your case!

@d4h0
Copy link
Author

d4h0 commented Sep 25, 2020

You mean this example, right?

The output of this example has colored output.

When I execute the following code, however, there isn't colored output:

use log::{info};

fn main() {
    let logger_config = "trace";
    flexi_logger::Logger::with_str(logger_config)
        .check_parser_error()
        .expect("Logger config string formatted incorrectly")
        .log_target(flexi_logger::LogTarget::StdOut)
        .format_for_stdout(flexi_logger::colored_detailed_format)
        .format_for_stderr(flexi_logger::colored_detailed_format)
        .start()
        .expect("Failed to start logger");
    
    info!("test");
}

I even tried to run the above code from a binary within this repo (in case there is some conflict in my project).

If you mean the performance example: There is no colored output.

@emabee
Copy link
Owner

emabee commented Sep 25, 2020

I mean the colors example - it is now (version 0.16.1) extended and prints out some additional information that flexi_logger uses internally to decide if coloring should be done.
Please try with 0.16.1; the additional output might provide enough insight to forward the issue to yansi :-|

@d4h0
Copy link
Author

d4h0 commented Sep 25, 2020

Ah, okay. Before I cloned this repo and ran the colors example this way, but it seems this repo only has 0.16.0.

Now I copied the colors example to my project (which has 0.16.1) and ran it.

The output is Feature color is switched off.

@emabee
Copy link
Owner

emabee commented Sep 25, 2020

How did you run it? Did you switch off the default features?

@d4h0
Copy link
Author

d4h0 commented Sep 25, 2020

How did you run it?

I copied the source code from 'colors.rs' to 'src/bin/flexi_logger_test.rs' and ran cargo run --bin flexi_logger_test

Did you switch off the default features?

No, my cargo.toml contains 'flexi_logger = "0.16.1"'

@d4h0
Copy link
Author

d4h0 commented Sep 25, 2020

What is strange is that, as mentioned above, when I clone this repo (0.16.0), and run colors from there, it seems to work.

The output is:

Stdout is considered a tty - flexi_logger::AdaptiveFormat will use colors
Stderr is considered a tty - flexi_logger::AdaptiveFormat will use colors

This is red output like by default with err!
This is yellow output like by default with warn!
This is normal output like by default with info!
This is output like by default with debug!
This is grey output like by default with trace!

And the last lines are colored.

Also, I just have realized a potential problem: In my test with 0.16.1, I copied the 'colors' example from this repo – could this be a problem? I'm not aware of a way to run an example of an installed crate, and I don't know where the repo for 0.16.1 is (this repo only has one branch which is 0.16.0).

@emabee
Copy link
Owner

emabee commented Sep 26, 2020

Looks like I didn‘t yet push the last change to github, but that doesn’t matter. 0.16.1 improved something unrelated to the coloring.

If you copy the source file to a different crate, then the feature would have to be defined there, with the right dependencies :-|

The output you‘ve shown in the previous comment, and the colored lines below, show clearly that the „low level“ coloring mechanics work as expected.

So something more highlevel must go wrong with your flexi_logger usage, if it fails to use colors in the same environment.

@d4h0
Copy link
Author

d4h0 commented Sep 29, 2020

If you copy the source file to a different crate, then the feature would have to be defined there, with the right dependencies :-|

Oh, right, I didn't read the source code – sorry for the false alarm!

So something more highlevel must go wrong with your flexi_logger usage, if it fails to use colors in the same environment.

Is there any logging that can be activated within flexi_logger?

If not, wouldn't it be possible (for me) to add a few dbg! calls somewhere to find out what the problem is?

If so, can you recommend any places where this would make especially sense?

In the meantime, I discovered and switched to the tracing crate (which seems to be incompatible with flexy_logger), but I'm still interested in helping to debug this issue (assuming this problem could happen to other users).

@emabee
Copy link
Owner

emabee commented Oct 5, 2020

The following diff should do it (4 dbg! calls inserted, and a derive):

diff --git a/src/formats.rs b/src/formats.rs
index ec58f07..68bdd7d 100644
--- a/src/formats.rs
+++ b/src/formats.rs
@@ -267,13 +267,14 @@ lazy_static::lazy_static! {
 // Returns an error if parsing failed.
 #[cfg(feature = "colors")]
 pub(crate) fn set_palette(input: &Option<String>) -> Result<(), std::num::ParseIntError> {
-    match std::env::var_os("FLEXI_LOGGER_PALETTE") {
+    match dbg!(std::env::var_os("FLEXI_LOGGER_PALETTE")) {
         Some(ref env_osstring) => {
-            *(PALETTE.write().unwrap()) = Palette::from(env_osstring.to_string_lossy().as_ref())?;
+            *(PALETTE.write().unwrap()) =
+                dbg!(Palette::from(env_osstring.to_string_lossy().as_ref())?);
         }
         None => match input {
             Some(ref input_string) => {
-                *(PALETTE.write().unwrap()) = Palette::from(input_string)?;
+                *(PALETTE.write().unwrap()) = dbg!(Palette::from(input_string)?);
             }
             None => {}
         },
@@ -282,6 +283,7 @@ pub(crate) fn set_palette(input: &Option<String>) -> Result<(), std::num::ParseI
 }

 #[cfg(feature = "colors")]
+#[derive(Debug)]
 struct Palette {
     pub error: Style,
     pub warn: Style,
@@ -369,7 +371,7 @@ pub enum AdaptiveFormat {
 impl AdaptiveFormat {
     #[must_use]
     pub(crate) fn format_function(self, stream: Stream) -> FormatFunction {
-        if stream.is_tty() {
+        if dbg!(stream.is_tty()) {
             match self {
                 #[cfg(feature = "colors")]
                 Self::Default => colored_default_format,

@d4h0
Copy link
Author

d4h0 commented Oct 19, 2020

Sorry for the delay!

With your patch I get the following output:

[src/formats.rs:374] stream.is_tty() = true
[src/formats.rs:374] stream.is_tty() = true
[src/formats.rs:270] std::env::var_os("FLEXI_LOGGER_PALETTE") = None
[2020-10-19 18:09:53.462338 +02:00] INFO [test] examples/test.rs:14: test

I use my example from this comment.

Does this give you any clue about what's going on?

@d4h0
Copy link
Author

d4h0 commented Oct 19, 2020

And I ran this by cloning your repo, applying the patch, creating examples/test.rs, and then running cargo run --example test.

@MaxFangX
Copy link

MaxFangX commented Aug 15, 2021

I thought my colors weren't working, and then I tried the colors example in this repo as @d4h0 mentioned. Turns out that my colors actually were working, except that in my own project I was testing with info! messages (which are not colored by default), and which I initially expected to be green as is the case with env_logger.

I don't know if it might apply to you @d4h0 but thought it would be helpful to share my point of confusion in case that happened to be it.

@emabee
Copy link
Owner

emabee commented Aug 18, 2021

Thanks for pointing this out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants