java 格式化 slf4j 以记录带有颜色的消息类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12468141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:01:02  来源:igfitidea点击:

Formatting slf4j to log message types with colors

javaloggingslf4j

提问by Panshul

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.

我正在使用 slf4j 登录我的 Java 应用程序。它涉及很多日志记录和日志监控。
当整个日志以黑色打印时,有时真的很难从日志中读取和查找信息。
只是为了使其更具可读性,是否可以以不同颜色记录不同类型的消息?
例如,所有错误级别消息为红色或不同字体大小,所有信息级别消息为蓝色和不同字体大小。

Any suggestions or help is welcome. Thnx.

欢迎任何建议或帮助。谢谢。

采纳答案by daniel

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

  1. 在您的 File Appender 配置中,您可以配置模式以包含日志级别(错误、警告等)。然后你可以 grep 文件,按级别过滤消息。

  2. 您可以配置具有不同级别阈值的两个文件附加程序(用于两个单独的日志文件)。例如,一个将所有高于调试级别的日志(如信息、警告、错误)记录到我们说的 logs.txt 中,另一个将仅将错误记录记录到 errors.txt 中

Hope it helps.

希望能帮助到你。

回答by Adrian Shum

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)

首先,SLF4J 只是一个日志门面。实际日志消息的处理方式取决于它使用的绑定。因此,您的问题无效,相反,您应该引用要使用的实现(LogBack?Log4J?等)

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).

其次,“着色”在大多数情况下没有意义。例如,如果您引用纯文本日志文件,则我们无法控制颜色,因为它们都是纯文本(除非您的编辑器为您的日志消息格式内置了特殊的语法突出显示)。如果您想在控制台/终端中查看颜色,或者如果您将日志输出为允许您包含颜色信息的文件格式(例如 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#coloringin 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.

LogBack 内置支持在控制台输出中对http://logback.qos.ch/manual/layouts.html#coloring进行着色。如果您正在寻找在控制台输出中查看颜色的方法,并且您可以使用 LogBack,那么这就是您要寻找的。

回答by yegor256

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

无法更改slf4j日志的颜色,因为没有格式化程序。SLF4J 是您的应用程序和某些日志记录工具(例如 Log4j 或 Logback)之间的中间件。

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

你可以改变颜色的Log4j输出,说明在这里。我会建议使用MulticolorLayoutjcabi日志

回答by Hymankobec

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

将 next appender 添加到 logback.xml 以对日志输出进行着色:

<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>

回答by Pomagranite

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

我对日志记录级别和包使用过滤器。这个例子来自 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

这样我只能看到我想看到的消息