简体   繁体   中英

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.

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.

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 (eg 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.

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

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

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>

In addition to using a colored ConsoleAppender , you could also color the statement itself. Note this has limitations and may not work with all systems. It worked well for internal projects, particularly when you want to log that a major task in a chain is complete or simply while debugging errors.

To quote an external link :

System.out.println("\u001B[31m" + "the text is red" + "\u001B[0m");

[31m is the ANSI code to color the output red and [31m is the ANSI reset command.

public class ColorLogger {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(ColorLogger.class);
    
    public void logDebug(String logging) {
        LOGGER.debug("\u001B[34m" + logging + "\u001B[0m");
    }
    public void logInfo(String logging) {
        LOGGER.info("\u001B[32m" + logging + "\u001B[0m");
    }
    
    public void logError(String logging) {
        LOGGER.error("\u001B[31m" + logging + "\u001B[0m");
    }
}

In your case, it sounds like you can try to write the contents of the aggregated log out and optionally color each statement by finding each string that concerns you.

This can alternatively just be done by printing it to an HTML page and using CSS selectively on your text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM