开发者

Why is log4j logging to the console?

This is a standalone java application.

I am using the configuration file below and having two problems.

1) I'm getting logs to stdout and I don't know why.

2) I'm getting ALL log messages in my error log even though I have tried to direct only error and higher to the error log.

I am using the BasicConfigurator without specifying an explicit path to the log4j.xml file. The xml file is in the same jar as my classes. It is creating and writing to the appropriate logs in addition to these problems so the configuration is being applied.

3) In addition, I have had no luck having the log4j.xml file outside of the jar so I can change it at runtime. How do I do that?

<!--appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
    <param name="Threshold" value="DEBUG"/>
    &l开发者_StackOverflowt;layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%-5p [%F:%L] - %m%n"/>
    </layout>
</appender-->

<!-- working dir is $CATALINA_TMPDIR. send logs to log dir. -->
<appender name="ROLL" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="/var/log/company/application.log"/>
    <param name="MaxFileSize" value="5MB"/>
    <param name="MaxBackupIndex" value="9"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c %x - %m%n"/>
    </layout>
</appender>

<appender name="ERRORLOG" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="/var/log/rocketvox/company/error.log"/>
    <param name="MaxFileSize" value="5MB"/>
    <param name="MaxBackupIndex" value="9"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c %x - %m%n"/>
    </layout>
</appender>

<category name="com.company">
    <priority value="ALL"/>
    <appender-ref ref="ROLL"/>
</category>

<category name="com.mattx">
    <priority value="ALL"/>
    <appender-ref ref="ROLL"/>
</category>

<root>
    <priority value="error"/>
    <appender-ref ref="ERRORLOG"/>
</root>


Add -Dlog4j.debug to the application's JVM args to see exactly what log4j is doing and which configuration file it uses.

Your problem is using BasicConfigurer - for configuring using a file named log4j.xml, you don't need to use any explicit log4j Configurer in your application code, as the default log4j initialization logic will kick in to pick log4j.xml (or log4j.properties, if no xml is found) from the classpath.

See Default Initialization Procedure from the log4j manual.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜