?
Solved

How to set DOMConfigurator of log4j and call it from any class

Posted on 2009-04-24
31
Medium Priority
?
3,470 Views
Last Modified: 2013-12-22
Hello:

How can I set the DOMConfigurator in a way that all the loggers created in all the classes know the path where the log file will be created?

 I dont want to write every class DOMConfigurator.configure.

Thank you
0
Comment
Question by:Fionageo80
  • 15
  • 11
  • 5
31 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24225417
There's only one configuration file and the location of the log file is set there, so there is no problem
0
 

Author Comment

by:Fionageo80
ID: 24225483
Hi:

What I meant was: If there any way to set log4n up so that I do NOT have to call
DOMConfigurator.Configure()  before the logging happen? Set it once so it can be read by all my classes
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24225503
There's actually no need to call it *at all* as long as your log4j.xml is in the classpath
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Fionageo80
ID: 24226021
ok, now I added the file in the classpath using Eclipse and I got errors.
log4j:ERROR A "org.apache.log4j.xml.DOMConfigurator" object is not assignable to a "org.apache.log4j.spi.Configurator" variable.
log4j:ERROR The class "org.apache.log4j.spi.Configurator" was loaded by 
log4j:ERROR [junit.runner.TestCaseClassLoader@13c7378] whereas object of type 
log4j:ERROR "org.apache.log4j.xml.DOMConfigurator" was loaded by [sun.misc.Launcher$AppClassLoader@11b86e7].
log4j:ERROR Could not instantiate configurator [org.apache.log4j.xml.DOMConfigurator].

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24226216
Have you removed all references to DOMConfigurator from your code?
0
 

Author Comment

by:Fionageo80
ID: 24226245
yes, because I have to write in all my classes DOMConfigurator.Configure() and that is my question how I declared once and read it  many times for all my classes ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24226271
Make sure all references are really gone and also clean up all imports too
0
 
LVL 92

Expert Comment

by:objects
ID: 24229776
> and also clean up all imports too

why?  what have imports got to do with it
0
 
LVL 92

Expert Comment

by:objects
ID: 24229792
Have a read of "Default Initialization Procedure" in the following page
http://logging.apache.org/log4j/1.2/manual.html
it explains the startup process

0
 

Author Comment

by:Fionageo80
ID: 24240777
Well, I have really read the manual, but I still get some problems I don´t why, because I set the xml file with the log4j properties in the path of my project in eclipse.

I call it like this: DOMConfigurator.configure("log4j.xml");

It seems that I get errors when I call the log4j inside of my DBunit test.

I don´t know what else can I do or try. Any hint?
log4j:ERROR A "org.apache.log4j.xml.DOMConfigurator" object is not assignable to a "org.apache.log4j.spi.Configurator" variable.
log4j:ERROR The class "org.apache.log4j.spi.Configurator" was loaded by 
log4j:ERROR [junit.runner.TestCaseClassLoader@90832e] whereas object of type 
log4j:ERROR "org.apache.log4j.xml.DOMConfigurator" was loaded by [sun.misc.Launcher$AppClassLoader@11b86e7].
log4j:ERROR Could not instantiate configurator [org.apache.log4j.xml.DOMConfigurator].
log4j:WARN No appenders could be found for logger (org.dbunit.DatabaseTestCase).
log4j:WARN Please initialize the log4j system properly.

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24240897
>>because I set the xml file with the log4j properties in the path of my project

You use either the xml file or the properties file - not both. Prefer the former
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24240901
>>I call it like this: DOMConfigurator.configure("log4j.xml");

I've already said above - you don't need to do that
0
 

Author Comment

by:Fionageo80
ID: 24241306
Ok, I dont call it anymore, but still get the error and this happen when I make the call TestRunner.run(Test.class); then I get the error that I posted above.

The Test class looks like this:

import org.dbunit.DBTestCase;
import org.dbunit.PropertiesBasedJdbcDatabaseTester;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;



public class Test extends DBTestCase
{  
   public static final String TABLE_LOGIN ="admin";
   private FlatXmlDataSet loadedDataSet;
    public SampleTest(String name)
    {
        super( name );
        System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "com..jdbc.IfxDriver" );
        System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "jdbc:://host:/database:=server" );
        System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "" );
        System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "" );
      // System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_SCHEMA, "" );
    }

    protected IDataSet getDataSet() throws Exception
    {
           loadedDataSet = new FlatXmlDataSet(this.getClass()
            .getClassLoader().getResourceAsStream("dataset.xml"));
                return loadedDataSet;

     //   return new FlatXmlDataSet(new FileInputStream("dataset.xml"));
       
    }
    protected DatabaseOperation getSetUpOperation() throws Exception
    {
        return DatabaseOperation.REFRESH;
    }

    protected DatabaseOperation getTearDownOperation() throws Exception
    {
        return DatabaseOperation.NONE;
    }
 
    public void testCheckLoginDataLoaded()
   
    throws Exception{
          
    assertNotNull(loadedDataSet);
    int rowCount = loadedDataSet.getTable(TABLE_LOGIN).getRowCount();
    assertEquals(1, rowCount);
    }

}  
 
   
 
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
 
public class PrintJob implements Job {
 
 private Boolean successful = Boolean.TRUE;
 /**
	 * Log4j logger
	 */
 
 private static final Logger log = Logger.getLogger(PrintJob.class);
 
 public Boolean getSuccessful() {
        return this.successful;
 }
 
 public void setSuccessful(final Boolean successful) {
        this.successful = successful;
 }
 public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException{
 
log.info("About to start job: " + new Date());
JobDataMap data = jobExecutionContext.getJobDetail().getJobDataMap();
TestRunner.run(Test.class);
log.debug("JobDataMap: " + data);
 
 }
 
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24241933
OK, you need to make sure there is *only one* config file *anywhere* in your class path
0
 

Author Comment

by:Fionageo80
ID: 24242705
I only have one log4j.xml file my Project is like this:

 D:\Performance_test
                                 |  - src
                                 |  - log4j\ log4j.xml
                                 | .classpath
                                 | .project

In Eclipse Project->Properties->Java Build Path->Libraries->Add Class Folder-> I add the folder log4j cuz I don´t find another way to add it to the path.

The problem comes out when I make the call TestRunner(Test.java) ; by code and it doesn´t create the log file, but when I run the Test.java using Run->Run-> Junit creates me without problems the log file.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24242782
TestRunner needs to have the log4j.xml in its classpath too. Does it?
0
 

Author Comment

by:Fionageo80
ID: 24242902
Yes, I think so and because of that I add the folder in Run->Run->JunitTest->Classpath->log4j-\Performance_test\ but I think I have to do the same but programatically, but I dont know how so set the path by code to the Testrunner
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24242933
Let's examine the situation. Place this as the first line of main in the JUnit app:
System.out.println(System.getProperty("java.class.path"));

Open in new window

0
 

Author Comment

by:Fionageo80
ID: 24243013
This is the result:

D:\Users\Test\Func\log4j;
D:\Users\Test\Func\bin;
D:\Users\Test\lib\dbunit-2.4.4.jar;
D:\Users\Test\lib\dbunit-2.4.4-prj.zip;
D:\Users\Test\lib\ifxjdbc.jar;
D:\Users\eclipse_current\plugins\org.junit_3.8.2.v20080602-1318\junit.jar;
D:\Users\Test\lib\slf4j-1.5.6\slf4j-1.5.6\slf4j-log4j12-1.5.6.jar;
D:\Users\Test\lib\slf4j-1.5.6\slf4j-1.5.6\slf4j-api-1.5.6.jar;
D:\Users\Test\lib\slf4j-1.5.6\slf4j-1.5.6\slf4j-jcl-1.5.6.jar;
D:\Users\Test\Func\lib\apache-log4j-1.2.15\log4j-1.2.15.jar
;D:\Users\lib\jcalendar-1.3.2.jar;
D:\Users\lib\looks-2.0.1.jar;
D:\Users\Zql\classes\Zql;
D:\Users\eclipse_current\plugins\quartz-1.4.5\lib\servlet.jar;
D:\Users\eclipse_current\plugins\quartz-1.4.5\lib\activation.jar;
D:\Users\eclipse_current\plugins\quartz-1.4.5\lib\commons-beanutils.jar;
D:\Users\eclipse_current\plugins\quartz-1.4.5\lib\commons-collections.jar;
D:\Users\eclipse_current\plugins\quartz-1.4.5\lib\commons-dbcp-1.1.jar;
D:\Users\eclipse_current\plugins\quartz-1.4.5\lib\commons-digester.jar;
D:\Users\eclipse_current\plugins\quartz-1.4.5\lib\commons-logging.jar;
D:\Users\eclipse_current\plugins\quartz-1.4.5\lib\commons-pool-1.1.jar;
D:\Users\eclipse_current\plugins\quartz-1.4.5\lib\ejb.jar;
D:\Users\eclipse_current\plugins\quartz-1.4.5\lib\examples.jar;
D:\Users\eclipse_current\plugins\quartz-1.4.5\lib\javamail.jar;
D:\Users\eclipse_current\plugins\quartz-1.4.5\lib\jdbc2_0-stdext.jar;
D:\Users\eclipse_current\plugins\quartz-1.4.5\lib\jta.jar;
D:\Users\eclipse_current\plugins\quartz-1.4.5\lib\quartz.jar;
D:\Users\Test\Func\Core\classes
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24243050
>>D:\Users\Test\Func\log4j;

You definitely have log4j.xml in there do you/
0
 

Author Comment

by:Fionageo80
ID: 24243065
yes, there it is log4j.xml
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24243323
OK. In that particular case, DOMConfigurator might need to be called, so try


DOMConfigurator.configure("D:/Users/Test/Func/log4j/log4j.xml");

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24244010
(Normally you would want to put that in a static block) So either a. below or, perhaps better, b.
//a.
 
static {
    DOMConfigurator.configure("D:/Users/Test/Func/log4j/log4j.xml");
}
...
 
//b.
 
static {
    DOMConfigurator.configure(PrintJob.class.getResource("/log4j/log4j.xml"));
}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 24246258
you do *not* need to call DOMConfigurator

If its not getting picked up authomativally then specify its location with the log4j.configuration system property

-Dlog4j.configuration=/log4j/log4j.xml

You can do that in the run configuration

0
 

Author Comment

by:Fionageo80
ID: 24249973
Hello,

I had to define all the appenders in the class becuase nothing was working.
Thank all of you for your help.
0
 
LVL 92

Expert Comment

by:objects
ID: 24250002
we use log4j on all our projects (and our clients) and have never had to call DOMConfigurator. I suspect your log4j.xml in not in your classpath in which case you just need to specify location in run configuration.
And make sure you are using the latest version.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24250096
>>I had to define all the appenders in the class becuase nothing was working.

That's a pity. Is it possible to post your complete project?
0
 

Author Comment

by:Fionageo80
ID: 24250257
I will have to delete many things first most of them, I will try to post a smaller version
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24250626
OK. As long as it compiles
0
 
LVL 92

Accepted Solution

by:
objects earned 1500 total points
ID: 24255611
the problem has nothing to do with the contents of your project.
did you try specifying the location exlicitly with log4j.configuration system property?

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24303961
Fionageo80, can you explain how you resolved the problem for the benefit of future visitors please?
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month16 days, 4 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question