Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j not finding custom appender using a property file

I'm trying to configure log4j in an Eclipse plugin project using the following XML property file, that includes a custom appender called EclipseLoggingAppender:

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

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
  <appender name="eclipseErrorView" class="com.lior.ibd.utils.logging.EclipseLoggingAppender"/> 

<appender name="console" class="org.apache.log4j.ConsoleAppender"> 
    <param name="Target" value="System.out"/> 
    <layout class="org.apache.log4j.PatternLayout"> 
      <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/> 
    </layout> 
  </appender>

  <root> 
    <priority value ="debug" /> 
    <appender-ref ref="console" /> 
  </root>
  <logger name="com.lior">
    <level value ="warn" /> 
    <appender-ref ref="eclipseErrorView" />
  </logger> 

</log4j:configuration>

I pass this property file to the following statement in the code:

DOMConfigurator.configure(filename);

But when loading the application I get the following error message:

log4j:ERROR Could not create an Appender. Reported error follows.
java.lang.ClassNotFoundException: com.lior.ibd.utils.logging.EclipseLoggingAppender

Anyone knows what's the deal? could be a classpath issue?..

like image 446
Protostome Avatar asked Oct 20 '10 14:10

Protostome


People also ask

Can Log4j2 use log4j properties file?

Log4j 2 doesn't support the Log4j v1 ". properties" format anymore (yet, since v2. 4, Log4j supports a Property format, but its syntax is totally different from v1 format). New formats are XML, JSON, and YAML, see the documentation (note: if you used one of these formats in a file called ".

Does log4j need a properties file?

It's optional if the file name is log4j. properties and it's in the project classpath. We have to configure it before using the logger. Here is a simple program showing how to configure and use log4j logging.

Where do I put log4j properties file?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file. The level of the root logger is defined as INFO and attaches the ROLLINGFILE appender to it.


2 Answers

Yes, this is a classpath issue. Log4j is looking for class com.lior.ibd.utils.logging.EclipseLoggingAppender. (probably appender that wrote someone in your organisation?)

If you remove lines:

 <appender name="eclipseErrorView" class="com.lior.ibd.utils.logging.EclipseLoggingAppender"/>

and

 <logger name="com.lior">
   <level value ="warn" /> 
   <appender-ref ref="eclipseErrorView" />
 </logger> 

log4j should handle it.

Or add EclipseLoggingAppender to classpath by locating a appropriate jar file and add it to the classpath. I.e. run

java -cp appender.jar com.mypackage.MyClass
like image 95
krtek Avatar answered Sep 28 '22 07:09

krtek


for starters you can only have one <root> element. You want something more like

<appender name="eclipseErrorView" class="com.mypackage.EclipseLoggingAppender"> 
  <filter class="org.apache.log4j.varia.LevelRangeFilter Source code of org.apache.log4j.varia.LevelRangeFilter">
    <param name="LevelMin" value="WARN" />
  </filter>
</appender>

<root>
  <priority value ="debug" /> 
  <appender-ref ref="console" />
  <appender-ref ref="eclipseErrorView" />
</root> 

How have you added your custom logger to the classpath?

like image 22
Brad Mace Avatar answered Sep 28 '22 06:09

Brad Mace