Nageswaran
Nageswaran

Reputation: 7651

Log4j is not writing the log into the file

Log4j.xml

  <appender name="U_R_A" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="Threshold" value="fatal" />
    <param name="Append" value="true" />
    <param name="File" value="/logs/log_file.log" />
    <param name="DatePattern" value="'.'yyyy-MM-dd-HH" />
    <layout class="org.apache.log4j.PatternLayout" />
   </appender>

  <logger name="U_Q_R" additivity="false">
    <!-- Overridding from fatal to error -->
    <level value="ERROR" />
    <appender-ref ref="U_R_A" />
  </logger>

Main.java

public class Main{
  private static Logger logger = Logger.getLogger("U_Q_R");
  public static void main(String[] args) {
    DOMConfigurator.configure("log4j.xml"); 
    logger.error("Some error happened!!");
  }
}

Log4j is not writing the error into the file log_file.log. If I change the Threshold into error, then it is writing the content into the file. Please help me to understand.

Upvotes: 0

Views: 161

Answers (1)

Kent
Kent

Reputation: 195029

the logger has level ERROR, it won't overwrite the appender's threshold.

Log messages with >= ERROR will be accepted by the logger, since you have defined level as ERROR. Then your logger has only one appender defined, with threshold fatal, which means the appender will handle logs with level >=fatal. that's why error level logs won't be saved in your log file.

when you changed the threshold <= logger's level, all logs accepted by your logger will be handled by the appender. That's why it "worked" if you change the threshold into error.

Upvotes: 1

Related Questions