Log4j File created but empty

1.6k views Asked by At

Tried other question answers but couldn't overcome the problem

I have xml file for configure the log4j. Two log files are created but files are empty. I cannot write to a file.

This is my xml

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


<log4j:configuration
 xmlns:log4j="http://jakarta.apache.org/log4j/">

 <appender name="ErrorAPPENDER"
  class="org.apache.log4j.DailyRollingFileAppender">
  <param name="File" value="D://Error.log" />
  <param name="Append" value="true" />
  <param name="DatePattern" value="'.'yyyy-MM-dd" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
    value="%d{yyyy-MM-dd,HH:mm:ss},%m%n" />
  </layout>
 </appender>

 <appender name="EventAPPENDER"
  class="org.apache.log4j.DailyRollingFileAppender">
  <param name="File" value="D://Event.log" />
  <param name="Append" value="true" />
  <param name="DatePattern" value="'.'yyyy-MM-dd" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
    value="%d{yyyy-MM-dd,HH:mm:ss},%m%n" />
  </layout>
 </appender>
 
 <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
  <param name="Target" value="System.out" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%m%n" />
  </layout>
 </appender>

 <logger name="Event" additivity="false">
  <level value="debug" />
  <appender-ref ref="EventAPPENDER" />
 </logger>

 <logger name="Error" additivity="false">
  <level value="debug" />
  <appender-ref ref="ErrorAPPENDER" />
 </logger>



 <root>
  <priority value="debug" />
  <appender-ref ref="ConsoleAppender" />
 </root>
</log4j:configuration>

And this is the initialize class

package com.mobios.common;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Log4JUtil {

    public static final Logger logger=   Logger.getLogger(Log4JUtil.class);

}

And this is how i logging

Log4JUtil.logger.getLogger("ErrorAPPENDER").error("sdsffd");

But nothing writes on the log file. I cannot see any error and i tried the solutions mentioned in other questions.

1

There are 1 answers

1
pvpks On

A logger is always identified by a unique name across all the loggers initialized until now

Logging.getLogger(Log4jUtil.class) creates a logger with name "com.mobios.common.Log4jUtil" (Log4jUtil.class.getName()) or returns an already created logger with that name https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Logger.html#getLogger(java.lang.Class)

In your log4j.xml file, you have not created any logger with the name "com.mobios.common.Log4jUtil". You created loggers with names "Event", "Error"

You can add a section in your log4j.xml file with "com.mobios.common.Log4jUtil" and add the "Event" and "Error" handlers to it so that it logs to the corresponding files

<logger name="com.mobios.common.Log4jUtil" additivity="false">
    <level value="debug" />
    <appender-ref ref="EventAPPENDER" />
    <appender-ref ref="ErrorAPPENDER" />
</logger>

While logging, you don't need to log the messages with getLogger() call again. In your Log4jUtil.class, you can simply do,

logger.error("sdsffd")

In other classes, if you want to use the logger from Log4jUtil.class, you can do Logger.getLogger(Log4JUtil.class) and this returns an already instantiated logger with the handlers attached

package com.mobios.common;

import org.apache.log4j.Logger;

public class AnotherClass {

    public static final Logger logger=   Logger.getLogger(Log4JUtil.class);

}