Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting slf4j to log message types with colors

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.

like image 559
Panshul Avatar asked Sep 17 '12 22:09

Panshul


2 Answers

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

like image 89
yegor256 Avatar answered Sep 18 '22 11:09

yegor256


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.

like image 24
Adrian Shum Avatar answered Sep 17 '22 11:09

Adrian Shum