Log4j output to different files depending on class and level

What I want to do is very simple, but I cannot get it to work with log4j.

I need 2 log files. The first should contain all debug statements of debug level or higher, but only from classes in my.app. *

The second log file should contain all warning messages of a level and higher, regardless of the source class.

I tried the following configuration, but it does not work:

log4j.rootLogger=warn,root log4j.appender.root=org.apache.log4j.FileAppender log4j.appender.root.layout = org.apache.log4j.PatternLayout log4j.appender.root.layout.conversionPattern = %d [%t] %-5p %c - %m%n log4j.appender.root.file = /tmp/logs/warn.file log4j.appender.root.append = true log4j.appender.root.MaxFileSize=10MB log4j.appender.root.MaxBackupIndex=7 log4j.logger.my.app=debug,debugAppender log4j.appender.debugAppender = org.apache.log4j.RollingFileAppender log4j.appender.debugAppender.file = /tmp/logs/my.debug.file log4j.appender.debugAppender.layout = org.apache.log4j.PatternLayout log4j.appender.debugAppender.layout.conversionPattern = %d [%t] %-5p %c - %m%n log4j.appender.debugAppender.append = true log4j.appender.debugAppender.MaxFileSize=10MB log4j.appender.debugAppender.MaxBackupIndex=7 log4j.additivity.my.app=false 

I think the problem is with the last line. If I set additivity to true, debug messages and level information will also be added to my warn.file. But if I set it to false, warnings from my.app only get logged in debug.file.

+4
source share
1 answer

You want to use threshold values ​​for each application - exactly as described in the Log Level for each user for one registrar .

So, leave the additivity value true, and then add:

 log4j.appender.root.Threshold=WARN 

Read more about http://logging.apache.org/log4j/1.2/faq.html#a2.9 : "Can I directly output log output to different applications by level?"

Just my extra $ 0.02: I would recommend switching to XML instead of a property format that will be well prepared for switching to SLF4J / Logback , as log4j is quickly replaced by this decent successor combination.

+4
source

Source: https://habr.com/ru/post/1388757/


All Articles