简体   繁体   中英

log4j configuration to log all error messages in file

have following log4j configuration:

<?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>

still the error messages are not getting logged in the file.

here is code snippet

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 
}

not getting what is missing configuration ? please suggest

In my suggestion, I modified and tried below 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>

This will print error message in log file.

Hope it work for you!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM