Formatting slf4j to log message types with colors

前端 未结 5 2237
情书的邮戳
情书的邮戳 2021-02-07 04:55

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 w

相关标签:
5条回答
  • 2021-02-07 04:59

    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.

    0 讨论(0)
  • 2021-02-07 05:04

    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

    0 讨论(0)
  • 2021-02-07 05:10

    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.

    0 讨论(0)
  • 2021-02-07 05:12

    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

    0 讨论(0)
  • 2021-02-07 05:17

    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>
    
    0 讨论(0)
提交回复
热议问题