问题
I'm using log4j for logs.
I have a class that i want to print to the console only log.info level,
And print to some file log.info + log.debug levels (The same class).
I tried to setup the log4j.xml as follows :
<!--appender to parser file-->
<appender name="parserFile" class="org.apache.log4j.FileAppender">
<param name="File" value="/var/log/java/parser/parser.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{E MMM dd HH:mm:ss} %c : %m%n" />
</layout>
</appender>
<!--appender to the stdout-->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
<logger name="parser.ParserMainJava">
<level value="debug" />
<appender-ref ref="parserFile" />
</logger>
<logger name="parser.ParserMainJava">
<level value="info" />
<appender-ref ref="console" />
</logger>
This is my configuration but I can't use 2 loggers with the same package name.
Any suggestions ?
Thanks,
Or.
回答1:
Just define one logger for "parser.ParserMainJava" with debug level, and add the following parameter to your console appender definition:
<param name="Threshold" value="INFO"/>
来源:https://stackoverflow.com/questions/21512629/log4j-debug-to-file-info-to-console