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

Terminal coloration #3

Closed
Nemikolh opened this issue Aug 12, 2015 · 13 comments
Closed

Terminal coloration #3

Nemikolh opened this issue Aug 12, 2015 · 13 comments

Comments

@Nemikolh
Copy link

Hello there!

I have realised log4rs does not provide terminal coloration by default. Would you be interested in the feature?

For comparison, loggers such as log4js and winston provides terminal coloration by default.

If you like the idea but don't have the time for it, I would gladly work on it. :)

@sfackler
Copy link
Collaborator

Yep, definitely seems reasonable to implement! I will probably not have a ton of time to work on it, but if you were willing to that'd be great.

@ryantaylor
Copy link

I really like this library but I needed colorization, so I went ahead and hacked in super basic, super dirty functionality. No config or anything, just hard-coded colour values for different log levels. I wrote it for my own use until someone smarter than me found the time to implement a proper solution, but I figured I'd post it here in case it helps. If nobody else finds the time to work on this functionality I might look into cleaning up the code and doing it right.

ryantaylor@4ded253

@Nemikolh
Copy link
Author

Nemikolh commented Sep 7, 2015

To be fair, I haven't found much time to work on this. Thanks for working on it !

As a comparison this is the default in log4js:

log4js

With your solution, it currently looks like this:
log4s

In my opinion, this is already a big improvement !
If you're willing to write a cleaner solution for it, feel free to do so.

@nicokoch
Copy link
Contributor

nicokoch commented Nov 4, 2015

Is anyone still working on this? If not, I am willing to pick it up.

I will need some pointers on how one would configure this in the .toml file

@sfackler
Copy link
Collaborator

sfackler commented Nov 4, 2015

I don't think anyone's working on it right now. Logback's pattern syntax might be a place to look for configuration syntax inspiration: http://logback.qos.ch/manual/layouts.html#coloring

nicokoch added a commit to nicokoch/log4rs that referenced this issue Nov 4, 2015
This implementation is based on the prototype provided by @ryantaylor in estk#3.

Adds a new color syntax to the pattern parser which looks like this:
%c:color(...colored text...)

Also changes the default pattern to `%c:highlight(%d %l %t -) %m`.

closes estk#3
sfackler added a commit that referenced this issue Apr 9, 2016
@sfackler
Copy link
Collaborator

sfackler commented Apr 9, 2016

I've started working on this.

sfackler added a commit that referenced this issue Apr 9, 2016
@sfackler
Copy link
Collaborator

sfackler commented Apr 9, 2016

Progress! The encoder::Write trait has been extended with a set_style method, which can control the foreground and background colors as well as the foreground intensity of text it outputs. The console appender now colors text if attached to a tty/console. I've also added a new "highlight" formatter to the pattern syntax, which sets the color based on the log level. For example, the test crate now has the pattern {d} [{t}] {h({l})} {M}:{m}{n}, which produces output that looks like this when it runs:

capture

I copied the color selections from logback: intense red for error, red for warning, blue for info, and default for everything else.

I'd like to add a pattern formatter or group of them that lets you pick a specific style, but I'm not sure about the syntax right now.

@sfackler
Copy link
Collaborator

I've released v0.4.3 with color support. All that the pattern encoder supports right now is level-based styling. I've opened a new issue to figure out syntax for configuration of specific styles: #17

@ivnsch
Copy link

ivnsch commented Mar 3, 2021

Hi! I started using log4s and not seeing any colors in the terminal (I see them e.g. with pretty_env_logger). Do I have to enable it somewhere?

@estk
Copy link
Owner

estk commented Mar 3, 2021

see #204 , its in progress but seems to be stalled

@OvermindDL1
Copy link

What's keeping it stalled currently?

@Creative-Difficulty
Copy link

Is there any progress at all @estk ?

@Pr0gm4n
Copy link

Pr0gm4n commented Mar 8, 2024

@Creative-Difficulty you may want to try using the formatter {h} documented in the log4rs::encode::pattern, i.e., here.

As an example, you could use this in your yaml_config:

appenders:
  # An appender named "stdout" that writes to stdout
  stdout:
    kind: console
    target: stdout
    encoder:
      pattern: " {h({l:<5}} {M} > {m}{n}"

# Set the default logging level to "warn" and attach the "stdout" appender to the root
root:
  level: info
  appenders:
    - stdout

This should look somewhat similar to the output produced by the pretty_env_logger crate, although not with its neat alignment.

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

Successfully merging a pull request may close this issue.

9 participants