Link to home
Start Free TrialLog in
Avatar of novicer
novicer

asked on

Configure Common Loging to use Log4J - How to?

   Hi every one.
I've configured the commons-logging.properties file like this:
org.apache.commons.logging.Log = org.apache.log4j.Logger

And the log4j.properties like this:
## Init logger for Dating DAL
log4j.rootLogger=DEBUG, stdout, file
###########################################################################
## console output
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
###########################################################################
## file output                                          
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./workspace/BBFM/bbfm.log
log4j.appender.file.MaxFileSize=10000KB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%p %t %c - %m%n
###########################################################################
I also put the 2 jar file commons-logging-1.0.4.jar and log4j-1.2.8.jar in the same directory and add them to the Build Path (on Eclipse 3.1.2). I think it should run fine, but didn't, here is the errors I've got:
There were 4 errors:
1) testCreate(be.belga.belgadirect.invoicing.persistence.test.ClientDAOTest)java.lang.ExceptionInInitializerError
      at be.belga.belgadirect.invoicing.persistence.test.ClientDAOTest.testCreate(ClientDAOTest.java:36)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at be.belga.belgadirect.invoicing.persistence.test.ClientDAOTest.main(ClientDAOTest.java:21)
Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: Class org.apache.log4j.Logger does not implement 'org.apache.commons.logging.Log'. (Caused by org.apache.commons.logging.LogConfigurationException: Class org.apache.log4j.Logger does not implement 'org.apache.commons.logging.Log'.) (Caused by org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: Class org.apache.log4j.Logger does not implement 'org.apache.commons.logging.Log'. (Caused by org.apache.commons.logging.LogConfigurationException: Class org.apache.log4j.Logger does not implement 'org.apache.commons.logging.Log'.))
      at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
      at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
      at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
      at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
      at be.belga.belgadirect.invoicing.persistence.util.HibernateUtil.<clinit>(HibernateUtil.java:55)
      ... 18 more
Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: Class org.apache.log4j.Logger does not implement 'org.apache.commons.logging.Log'. (Caused by org.apache.commons.logging.LogConfigurationException: Class org.apache.log4j.Logger does not implement 'org.apache.commons.logging.Log'.)
      at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:397)
      at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
      ... 22 more
Caused by: org.apache.commons.logging.LogConfigurationException: Class org.apache.log4j.Logger does not implement 'org.apache.commons.logging.Log'.
      at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:392)
      ... 23 more
2) testUpdate(be.belga.belgadirect.invoicing.persistence.test.ClientDAOTest)java.lang.NoClassDefFoundError
      at be.belga.belgadirect.invoicing.persistence.test.ClientDAOTest.testUpdate(ClientDAOTest.java:55)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at be.belga.belgadirect.invoicing.persistence.test.ClientDAOTest.main(ClientDAOTest.java:21)
3) testDelete(be.belga.belgadirect.invoicing.persistence.test.ClientDAOTest)java.lang.NoClassDefFoundError
      at be.belga.belgadirect.invoicing.persistence.test.ClientDAOTest.testDelete(ClientDAOTest.java:75)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at be.belga.belgadirect.invoicing.persistence.test.ClientDAOTest.main(ClientDAOTest.java:21)
4) testFindById(be.belga.belgadirect.invoicing.persistence.test.ClientDAOTest)java.lang.NoClassDefFoundError
      at be.belga.belgadirect.invoicing.persistence.test.ClientDAOTest.testFindById(ClientDAOTest.java:92)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at be.belga.belgadirect.invoicing.persistence.test.ClientDAOTest.main(ClientDAOTest.java:21)
I think the main cause for this exception is the failure of the initialization of the Logger framework, but I don't know how to fix it.
Avatar of novicer
novicer

ASKER

why you put:
log4j.rootLogger=DEBUG, stdout, file   ?
>log4j.appender.file.File=./workspace/BBFM/bbfm.log
be sure you file could be written on that directory (permission,etc.)
Avatar of novicer

ASKER

>why you put:
>log4j.rootLogger=DEBUG, stdout, file   ?
Because I want to log the message to a file, I myself think that it's not a good way to do this. I would appriciate a better method to do this.
>be sure you file could be written on that directory (permission,etc.)
Sure, all the neccessary permission were granted.
By the way, I found that the class org.apache.log4j.Logger did not implement the org.apache.commons.logging.Log interface. This may be the main cause?


from the log this seems the main reason.
## Init logger for Dating DAL
log4j.rootLogger=DEBUG, stdout, R
###########################################################################
## console output
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
###########################################################################
## file output                                  
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=./workspace/BBFM/bbfm.log
log4j.appender.R.MaxFileSize=10000KB
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
###########################################################################

like documentation suggest.
ASKER CERTIFIED SOLUTION
Avatar of Giant2
Giant2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of novicer

ASKER

I've solve this problem like this: put only this line in the commons-logging.properties file:
org.apache.commons.logging.LogFactory = org.apache.commons.logging.impl.LogFactoryImpl
Everything works perfectly.
ok. Happy for you.

Bye, Giant.
Avatar of novicer

ASKER

Thank for your answer Giant2.