We help IT Professionals succeed at work.

Configure Common Loging to use Log4J - How to?

novicer
novicer asked
on
921 Views
Last Modified: 2008-03-17
   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.
Comment
Watch Question

Author

Commented:
This problem is also documented here : http://wiki.apache.org/jakarta-commons/Logging/FrequentlyAskedQuestions

Commented:
why you put:
log4j.rootLogger=DEBUG, stdout, file   ?

Commented:
>log4j.appender.file.File=./workspace/BBFM/bbfm.log
be sure you file could be written on that directory (permission,etc.)

Author

Commented:
>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?


Commented:
from the log this seems the main reason.

Commented:
## 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.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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.

Commented:
ok. Happy for you.

Bye, Giant.

Author

Commented:
Thank for your answer Giant2.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.