Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

java - log4j 2 adding multiple colors to console appender

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:logout.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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 disableAnsi="false"  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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...