log4j配置记录文件中的所有错误消息

时间:2018-06-10 14:22:38

标签: java log4j

有以下log4j配置:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="5">
    <Appenders>
        <File name="MyFile" fileName="${sys:catalina.base}/logs/clinic.log" append="true">
            <PatternLayout pattern="%d %-5level [%logger{3.}] - %msg%n" />
        </File>

    </Appenders>
    <Loggers>
     <Logger name="*.*" level="ERROR" includeLocation="true">
            <AppenderRef ref="MyFile"/>
        </Logger>
       <Root level="ERROR">
            <AppenderRef ref="MyFile" level="error" />
        </Root>
    </Loggers>
</Configuration>

仍然没有在文件中记录错误消息。

这是代码段

public class AddFeedbackForm {
    private final Clinic clinic;

   private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AddFeedbackForm.class.getName());
/// some code 
 LOG.error("First Name: " + feed.getFirstName());
 LOG.error("Last Name: " + feed.getLastName());
// some code 
}

没有得到丢失的配置?请建议

1 个答案:

答案 0 :(得分:0)

在我的建议中,我修改并尝试了以下configuraiton:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration
    xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="MyFile"
        class="org.apache.log4j.RollingFileAppender">
        <param name="file" value="${sys:catalina.base}/logs/clinic.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>
    <Loggers>
        <Logger name="*.*" level="error" includeLocation="true">
            <appender-ref ref="MyFile" />
        </Logger>
        </Loggers>
    <root level="error">
        <appender-ref ref="MyFile" level="error" />
    </root>
</log4j:configuration>

这将在日志文件中打印错误消息。

希望它对你有用!!