Log4j debug to file - Info to console

北城余情 提交于 2019-12-13 19:54:09

问题



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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!