Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j2 configuration

Someone can say me how I can change my log4j2.xml to add 1 log file: one level trace and one level info?

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="debug">
    <appenders>
        <Console name="CONSOLE" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %-5p %C{2} (%F:%L) - %m%n" />
        </Console>
        <File name="DEBUG_FILE" fileName="debug.txt">
            <PatternLayout pattern="%d %-5p %C{2} (%F:%L) - %m%n" />
        </File>
    </appenders>
    <loggers>
        <root level="debug">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="DEBUG_FILE" />
        </root>
    </loggers> 
</configuration>
like image 617
Guz Avatar asked Mar 24 '23 07:03

Guz


1 Answers

You could make the root logger TRACE level (all messages) and put a ThresholdFilter on the Console so that only some messages are displayed on the console.

This configuration will log only ERROR messages to the console, and at the same time will log all messages (TRACE, DEBUG, INFO, ERROR...) to the debug.txt file. Error and higher messages are logged both in the console and in the file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="ERROR">
    <appenders>
        <Console name="CONSOLE" target="SYSTEM_OUT">
            <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d %-5p %C{2} (%F:%L) - %m%n" />
        </Console>
        <File name="DEBUG_FILE" fileName="debug.txt">
            <PatternLayout pattern="%d %-5p %C{2} (%F:%L) - %m%n" />
        </File>
    </appenders>
    <loggers>
        <root level="trace">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="DEBUG_FILE" />
        </root>
    </loggers> 
</configuration>

By the way, please be aware that your pattern layout contains conversion patterns that require location information (%C, %F and %L, to be exact). This means that for every log message, log4j2 needs to take a snapshot of the stack, and then walk the stack trace to find the class and method that did the logging.

This is very, very slow.

Logging will be 2-5 times slower for synchronous logging and 4-20 times slower for asynchronous logging.

If performance is not an issue then it doesn't matter, but it is definitely something to be aware of. Personally I just use %c (the logger name) and my app's log messages are unique enough that I can quickly find where the message came from. Just my 2 cents.

--- update 2013-04-27 --- I have since learned a simpler way to do this: you can set a level on the appender-ref.

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="debug">
    <appenders>
        <Console name="CONSOLE" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %-5p %c{2} - %m%n" /> <!--without location-->
        </Console>
        <File name="DEBUG_FILE" fileName="debug.txt">
            <PatternLayout pattern="%d %-5p %c{2} - %m%n" />
        </File>
    </appenders>
    <loggers>
        <root level="debug">
            <appender-ref ref="CONSOLE" level="ERROR" />
            <appender-ref ref="DEBUG_FILE" />
        </root>
    </loggers> 
</configuration>
like image 67
Remko Popma Avatar answered Apr 06 '23 07:04

Remko Popma