Drakulix / simplelog.rs

Simple Logging Facility for Rust
https://docs.rs/simplelog/
Apache License 2.0
423 stars 71 forks source link

Proposal: colors and styles #83

Closed manio closed 2 years ago

manio commented 2 years ago

Hi! I really like use your simplelog as my main logger for projects :) I am now experimenting with adding support for bold/italic and colors in my log messages. I am not aware of this with the simplelog (correct me if I am wrong), so I am using an ansi_term crate for this.

The downsides of this approach:

  1. A plenty of additional calls are needed, eg to make a text bold green I have to call: Style::new().bold().fg(Green).paint(err.to_string())
  2. the writelogger is not aware of the control characters and put it "as is" in the files - making it less readable (this is currently also a problem when using some other crates which is using a colored text, eg rocket).

I want to ask what is your opinion to have in simplelog something similar as in paris, ie parser for the following: log.info("<blue><on-bright-red> This text is blue on a bright red background</> it's a pain"); I am not sure if the above parsing is even possible when using error!, info! macros from rust...?

Is it in a scope of this project?

Drakulix commented 2 years ago

So what I think would be in scope is some kind of filter for control sequences at least. Maybe an additional wrapping-logger could be provided, that does remove control sequences and can then be used in conjunction with e.g. WriteLogger.

paris looks like a really nice crate from an api-perspective. But I am not sure, how much code this would add to simplelog and if it would not be better to implement a new crate similar to paris, but with log support. (Essentially just proxy-ing the log-macros.)

That said, I would definitely consider having a tight integration with a crate like that in simplelog. I am just not sure, if I want to maintain all that parsing code myself.

manio commented 2 years ago

I see, thank you for fast answer. Because this is not a issue "per se" so I am closing it for now... will reopen when necessary.

manio commented 2 years ago

Just for future reference... I did it like this:

use paris::formatter::colorize_string;
//standard simplelog initialize
let t = colorize_string("<blue><on-bright-red> This text is blue on a bright red background</> it's a pain");
info!("{}", t);
//or:
warn!("{}", colorize_string("<b><green>test string"));

I will now search how can I wrap it in some macro (proxy as you mention) to be able to use it like before using just info!, warn!, etc

Drakulix commented 2 years ago

If you manage to do that, I would accept those macros (assuming they would be rather simple) in simplelog behind a (by default disabled) optional paris feature.

manio commented 2 years ago

Great, added a PR #84. Let's move discussion there...