Formatting slf4j to log message types with colors

2019-03-24 17:19发布

I am using slf4j for logging in my Java Application. It involves a lot logging and log monitoring.
Sometimes it is really difficult to read and find information from logs when the entire log is printed in the black color with.
Just to make it more readable, is it possible to log the different kinds of messages in different colors?
For example all Error level messages in Red color or a different font size and all Info level messages in blue color and a different font size.

Any suggestions or help is welcome. Thnx.

5条回答
淡お忘
2楼-- · 2019-03-24 17:38

Add next appender into logback.xml to colorize logs output:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <Pattern>%d %highlight(%-5level) [%thread] %cyan(%logger{15}): %msg%n</Pattern>
        </encoder>
</appender>
查看更多
我命由我不由天
3楼-- · 2019-03-24 17:46

Two solutions come to my mind. They are not colors, but alternative solutions:

  1. In your File Appender configuration, you can configure the pattern to include the log level (error, warn, etc). Then you can grep the file, to filter messages by level.

  2. You can configure two file appenders (for two separate log files) with different level threshold. For instance, one would log all the logs above debug level (so info, warn, error) into let's say logs.txt and the other would log only the errors logs into errors.txt

Hope it helps.

查看更多
家丑人穷心不美
4楼-- · 2019-03-24 17:48

I use filters for both logging level and package. This example comes from Spring boot application.properties

   logging.level.root=warn
   logging.level.org.springframework=warn
   logging.level.org.hibernate=warn
   logging.level.org.starmonkey.brown=DEBUG

This way I see only messages I want to see

查看更多
forever°为你锁心
5楼-- · 2019-03-24 17:57

It's not possible to change colors of slf4j logging, because there are no formatters. SLF4J is a middleware between your application and some logging facility, for example, Log4j or Logback.

You can change colors in Log4j output, as explained here. I would recommend to use MulticolorLayout from jcabi-log

查看更多
淡お忘
6楼-- · 2019-03-24 17:59

Something you have to bear in mind.

First, SLF4J is only a logging facade. How the actual log message is handled depends on the binding it used. Therefore your question is invalid, instead, you should quote which implementation you want to use (LogBack? Log4J? etc)

Second, "Coloring" is not something meaningful in most case. For example, if you are referring a plain text log file, there is nothing that we can control the coloring because they are all plain text (unless your editor have special syntax highlighting built-in for your log message format). It may be meaningful if you want to see the color in the console/terminal, or if you are outputting your log into file format that allow you to contain color information (e.g. HTML).

With these two idea in mind, here is my suggestion.

LogBack has built-in support for coloring http://logback.qos.ch/manual/layouts.html#coloring in console output. If you are looking for way to see color in console output, and you are allowed to use LogBack, this is what you are looking for.

查看更多
登录 后发表回答