Configuring multiple log files in log4j when using categories

Here's what I'm trying to do: I want two log files: the first INFO level for logs and all parts of the applications, as well as DEBUG logs and some packages. In the second case, only ERROR and all packets are registered. I'm sure this is probably trivial, but I can't figure it out. Here is the configuration file that I am currently using:

log4j.rootLogger=INFO,console,R #console appender log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern = %t %-5p %c{2} - %m%n #file appender log4j.appender.R = org.apache.log4j.DailyRollingFileAppender log4j.appender.R.DatePattern = '.'yyyy-MM-dd log4j.appender.R.File = log/log.txt log4j.appender.R.layout = org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern = [%d{ISO8601}]%5p%6.6r[%t]%x(%F:%L) - %m%n #Specific log levels log4j.category.com.mypackage1=DEBUG log4j.category.com.mypackage2=DEBUG 

This obviously does not have a part of the error log. My main idea was to add another appender and set its log level to ERROR, but the categories seem to override it and also their information, which is not what I want. The reason they exist is because other dump packages have a lot of information that we don’t need when it is set up for debugging, and that’s how we get around it. I think there might be a better general approach to this, but this is my first log4j configuration file.

UPDATE: The employee suggested using 2 loggers, as indicated in the post which is related to kdgregory in his comment ( Different layout of log4j for debugging and errors? ), People there seemed to think this was a bad idea, but no one ever explained why. It seems a bit hacky, but it does what we need. What are the main reasons not to use this method (besides the need to support 2 different registrars)?

+3
source share
1 answer

Here is what I finally came up with that did the trick:

 log4j.rootLogger=INFO,console,stdLog,errorLog #console appender log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern = %t %-5p %c{2} - %m%n #file appender log4j.appender.stdLog=org.apache.log4j.DailyRollingFileAppender log4j.appender.stdLog.DatePattern = '.'yyyy-MM-dd log4j.appender.stdLog.File = log/log.txt log4j.appender.stdLog.layout = org.apache.log4j.PatternLayout log4j.appender.stdLog.layout.ConversionPattern = [%d{ISO8601}]%5p%6.6r[%t]%x(%F:%L) - %m%n #file appender log4j.appender.errorLog = org.apache.log4j.DailyRollingFileAppender log4j.appender.errorLog.DatePattern = '.'yyyy-MM-dd log4j.appender.errorLog.File = log/errorlog.txt log4j.appender.errorLog.layout = org.apache.log4j.PatternLayout log4j.appender.errorLog.layout.ConversionPattern = [%d{ISO8601}]%5p%6.6r[%t]%x(%F:%L) - %m%n log4j.appender.errorLog.Threshold=ERROR #Specific log levels log4j.category.com.mypackage1=DEBUG log4j.category.com.mypackage2=DEBUG 

This makes 2 logs, each of which logs everything in Warn or higher, plus in DEBUG or higher in the specified packages for the standard log and in ERROR and higher only for all packages in the error log.

+9
source

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


All Articles