views:

28

answers:

2

Hi, 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?..

A: 

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?

bemace
The EclipseLoggingAppender is located in a different plugin project, and is exported to other plugins. All plugins that use the logging utility include this plugin project in their 'Required plugin' section...
Protostome
+1  A: 

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
krtek
I don't want to remove those lines since I want this appender running in the application framework... What do you mean by "Adding EclipseLoggingAppender to the classpath"?
Protostome
@Protostome - it depends on your environment. Are you running your code from command line or within application server or somehow else?
krtek
@krtek This is an eclipse RCP application
Protostome
@Protostome - then check how Eclipse works with classpath in plugin development. For example here: http://www.eclipsezone.com/articles/eclipse-vms/
krtek
@krtex Great! that article helped, solved the problem! Only needed to add Eclipse-RegisterBuddy: org.apache.log4j to my plugin manifest :)
Protostome