log4j 2 adding multiple colors to console appender

左心房为你撑大大i 提交于 2019-11-27 10:47:32

问题


Hi I just downloaded and configured log4j-2. I am stuck on applying color codes to the SlowConsole console appender. My console appender is like below.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n"/>
        </Console>
        <Console name="SlowConsole" target="SYSTEM_OUT">
            <PatternLayout pattern="%highlight{%d{HH:mm:ss.SSS} %-5level %logger{36}.%M() @%L - %msg%n}{FATAL=red, ERROR=red, WARN=yellow, INFO=black, DEBUG=green, TRACE=blue}"/>
        </Console>
        <File name="File" fileName="C:\log\out.txt">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n" />
        </File>
    </Appenders>
    <Loggers>
        <logger name="org.abc.ea.web" level="ALL" additivity="false">
            <!--Log4j for the WEB MODULE -->
            <appender-ref ref="SlowConsole"/>
        </logger>
        <logger name="org.abc.ea.ejb" level="ALL" additivity="false">
            <!--Log4j for the EJB MODULE -->
            <appender-ref ref="SlowConsole"/>
        </logger>
        <Root level="ERROR">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="File" />
        </Root>
    </Loggers>
</Configuration>

I have two questions,

  1. I am new to log4j, is this the right way to write xml config file?

  2. How can i add two color codes to each log level?

    for example: DEBUG=green -> will output light green font, But i need it to be dim and bold


回答1:


I think I found the solution. I downloaded log4j2-core-sources.jar and traced the source. You can write it as below;

<Console name="SlowConsole" target="SYSTEM_OUT">
     <PatternLayout pattern="%highlight{%d{HH:mm:ss.SSS} %-5level %logger{36}.%M() @%L - %msg%n}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}"/>
</Console>

I think log4j2 documentation and its examples may need to be updated.




回答2:


For a log4j2 colored output that looks very close to Spring Boot's default logback console output:

    <Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
        <PatternLayout pattern="%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} %highlight{${LOG_LEVEL_PATTERN:-%5p}}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=green, DEBUG=green bold, TRACE=blue} %style{${sys:PID}}{magenta} [%15.15t] %style{%-40.40C{1.}}{cyan} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
    </Console>




回答3:


When using the eclipse console, its nice to see errors in red and all other logs in black. You can do this using Filters:

<Appenders>
    <Console name="ConsoleStdOut" target="SYSTEM_OUT">
        <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="ACCEPT"/>
        <PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
    </Console>
    <Console name="ConsoleStdErr" target="SYSTEM_ERR">
        <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
        <PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
    </Console>
</Appenders>
<Loggers>
    <Root level="info">
        <AppenderRef ref="ConsoleStdOut" />
        <AppenderRef ref="ConsoleStdErr" />
    </Root>
</Loggers>



回答4:


<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout
                    pattern="%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

This worked in my Spring Boot application.

But I am not sure if it will work without Spring Boot.




回答5:


1 : yes it's ok! and you can add some other options. see http://logging.apache.org/log4j/2.x/manual/configuration.html

2 : you can use a HTMLLayout for your file appender.

see http://logging.apache.org/log4j/2.x/manual/layouts.html

and http://www.tutorialspoint.com/log4j/log4j_htmllayout.htm




回答6:


Spring Boot style:

<Properties>
  <Property name="LOG_PATTERN">
    %d{yyyy-MM-dd HH:mm:ss.SSS} %highlight{${LOG_LEVEL_PATTERN:-%5p}}{FATAL=red, ERROR=red, WARN=yellow, INFO=green, DEBUG=green, TRACE=green} %style{${sys:PID}}{magenta} --- [%15.15t] %style{%-40.40c{1.}}{cyan} : %m%n%ex
  </Property>
</Properties>

<Appenders>
  <Console name="Console" target="SYSTEM_OUT">
    <PatternLayout pattern="${LOG_PATTERN}"/>
  </Console>
</Appenders>


来源:https://stackoverflow.com/questions/21979699/log4j-2-adding-multiple-colors-to-console-appender

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!