Log4j2 configuration

Can someone tell me how I can change my log4j2.xml to add 1 log file: trace of one level and one information of level?

<?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> 
+4
source share
1 answer

You can set the TRACE level of the root logger (all messages) and place the ThresholdFilter in the Console so that only some messages are displayed on the console.

This configuration will only write ERROR messages to the console and at the same time logs all messages (TRACE, DEBUG, INFO, ERROR ...) in the debug.txt file. Error messages and above are logged both on 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 note that your template template contains conversion templates that require location information ( % C,% F and% L , to be precise). This means that for each log4j2 log message, you need to take a snapshot of the stack and then go through the stack trace to find the class and method that performed the logging.

It 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 a problem, it does not matter, but you definitely need to know. Personally, I just use% c (the name of the registrar), and the application log messages are unique enough so that I can quickly find where the message came from. Only my 2 cents.

--- update 2013-04-27 --- Since then I have found out an easier way to do this: you can set the level in 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> 
+7
source

All Articles