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.log 4j.Console Appender
log4j.appender.stdout.layo ut=org.apa che.log4j. PatternLay out
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layo ut.Convers ionPattern =%5p [%t] (%F:%L) - %m%n
########################## ########## ########## ########## ########## #########
## file output
log4j.appender.file=org.ap ache.log4j .RollingFi leAppender
log4j.appender.file.File=. /workspace /BBFM/bbfm .log
log4j.appender.file.MaxFil eSize=1000 0KB
log4j.appender.file.MaxBac kupIndex=1 0
log4j.appender.file.layout =org.apach e.log4j.Pa tternLayou t
log4j.appender.file.layout .Conversio nPattern=% 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.belgad irect.invo icing.pers istence.te st.ClientD AOTest)jav a.lang.Exc eptionInIn itializerE rror
at be.belga.belgadirect.invoi cing.persi stence.tes t.ClientDA OTest.test Create(Cli entDAOTest .java:36)
at sun.reflect.NativeMethodAc cessorImpl .invoke0(N ative Method)
at sun.reflect.NativeMethodAc cessorImpl .invoke(Na tiveMethod AccessorIm pl.java:39 )
at sun.reflect.DelegatingMeth odAccessor Impl.invok e(Delegati ngMethodAc cessorImpl .java:25)
at be.belga.belgadirect.invoi cing.persi stence.tes t.ClientDA OTest.main (ClientDAO Test.java: 21)
Caused by: org.apache.commons.logging .LogConfig urationExc eption: org.apache.commons.logging .LogConfig urationExc eption: org.apache.commons.logging .LogConfig urationExc eption: Class org.apache.log4j.Logger does not implement 'org.apache.commons.loggin g.Log'. (Caused by org.apache.commons.logging .LogConfig urationExc eption: Class org.apache.log4j.Logger does not implement 'org.apache.commons.loggin g.Log'.) (Caused by org.apache.commons.logging .LogConfig urationExc eption: org.apache.commons.logging .LogConfig urationExc eption: Class org.apache.log4j.Logger does not implement 'org.apache.commons.loggin g.Log'. (Caused by org.apache.commons.logging .LogConfig urationExc eption: Class org.apache.log4j.Logger does not implement 'org.apache.commons.loggin g.Log'.))
at org.apache.commons.logging .impl.LogF actoryImpl .newInstan ce(LogFact oryImpl.ja va:543)
at org.apache.commons.logging .impl.LogF actoryImpl .getInstan ce(LogFact oryImpl.ja va:235)
at org.apache.commons.logging .impl.LogF actoryImpl .getInstan ce(LogFact oryImpl.ja va:209)
at org.apache.commons.logging .LogFactor y.getLog(L ogFactory. java:351)
at be.belga.belgadirect.invoi cing.persi stence.uti l.Hibernat eUtil.<cli nit>(Hiber nateUtil.j ava:55)
... 18 more
Caused by: org.apache.commons.logging .LogConfig urationExc eption: org.apache.commons.logging .LogConfig urationExc eption: Class org.apache.log4j.Logger does not implement 'org.apache.commons.loggin g.Log'. (Caused by org.apache.commons.logging .LogConfig urationExc eption: Class org.apache.log4j.Logger does not implement 'org.apache.commons.loggin g.Log'.)
at org.apache.commons.logging .impl.LogF actoryImpl .getLogCon structor(L ogFactoryI mpl.java:3 97)
at org.apache.commons.logging .impl.LogF actoryImpl .newInstan ce(LogFact oryImpl.ja va:529)
... 22 more
Caused by: org.apache.commons.logging .LogConfig urationExc eption: Class org.apache.log4j.Logger does not implement 'org.apache.commons.loggin g.Log'.
at org.apache.commons.logging .impl.LogF actoryImpl .getLogCon structor(L ogFactoryI mpl.java:3 92)
... 23 more
2) testUpdate(be.belga.belgad irect.invo icing.pers istence.te st.ClientD AOTest)jav a.lang.NoC lassDefFou ndError
at be.belga.belgadirect.invoi cing.persi stence.tes t.ClientDA OTest.test Update(Cli entDAOTest .java:55)
at sun.reflect.NativeMethodAc cessorImpl .invoke0(N ative Method)
at sun.reflect.NativeMethodAc cessorImpl .invoke(Na tiveMethod AccessorIm pl.java:39 )
at sun.reflect.DelegatingMeth odAccessor Impl.invok e(Delegati ngMethodAc cessorImpl .java:25)
at be.belga.belgadirect.invoi cing.persi stence.tes t.ClientDA OTest.main (ClientDAO Test.java: 21)
3) testDelete(be.belga.belgad irect.invo icing.pers istence.te st.ClientD AOTest)jav a.lang.NoC lassDefFou ndError
at be.belga.belgadirect.invoi cing.persi stence.tes t.ClientDA OTest.test Delete(Cli entDAOTest .java:75)
at sun.reflect.NativeMethodAc cessorImpl .invoke0(N ative Method)
at sun.reflect.NativeMethodAc cessorImpl .invoke(Na tiveMethod AccessorIm pl.java:39 )
at sun.reflect.DelegatingMeth odAccessor Impl.invok e(Delegati ngMethodAc cessorImpl .java:25)
at be.belga.belgadirect.invoi cing.persi stence.tes t.ClientDA OTest.main (ClientDAO Test.java: 21)
4) testFindById(be.belga.belg adirect.in voicing.pe rsistence. test.Clien tDAOTest)j ava.lang.N oClassDefF oundError
at be.belga.belgadirect.invoi cing.persi stence.tes t.ClientDA OTest.test FindById(C lientDAOTe st.java:92 )
at sun.reflect.NativeMethodAc cessorImpl .invoke0(N ative Method)
at sun.reflect.NativeMethodAc cessorImpl .invoke(Na tiveMethod AccessorIm pl.java:39 )
at sun.reflect.DelegatingMeth odAccessor Impl.invok e(Delegati ngMethodAc cessorImpl .java:25)
at be.belga.belgadirect.invoi cing.persi stence.tes t.ClientDA OTest.main (ClientDAO Test.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.
I've configured the commons-logging.properties
org.apache.commons.logging
And the log4j.properties like this:
## Init logger for Dating DAL
log4j.rootLogger=DEBUG, stdout, file
##########################
## console output
log4j.appender.stdout=org.
log4j.appender.stdout.layo
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layo
##########################
## file output
log4j.appender.file=org.ap
log4j.appender.file.File=.
log4j.appender.file.MaxFil
log4j.appender.file.MaxBac
log4j.appender.file.layout
log4j.appender.file.layout
##########################
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.belgad
at be.belga.belgadirect.invoi
at sun.reflect.NativeMethodAc
at sun.reflect.NativeMethodAc
at sun.reflect.DelegatingMeth
at be.belga.belgadirect.invoi
Caused by: org.apache.commons.logging
at org.apache.commons.logging
at org.apache.commons.logging
at org.apache.commons.logging
at org.apache.commons.logging
at be.belga.belgadirect.invoi
... 18 more
Caused by: org.apache.commons.logging
at org.apache.commons.logging
at org.apache.commons.logging
... 22 more
Caused by: org.apache.commons.logging
at org.apache.commons.logging
... 23 more
2) testUpdate(be.belga.belgad
at be.belga.belgadirect.invoi
at sun.reflect.NativeMethodAc
at sun.reflect.NativeMethodAc
at sun.reflect.DelegatingMeth
at be.belga.belgadirect.invoi
3) testDelete(be.belga.belgad
at be.belga.belgadirect.invoi
at sun.reflect.NativeMethodAc
at sun.reflect.NativeMethodAc
at sun.reflect.DelegatingMeth
at be.belga.belgadirect.invoi
4) testFindById(be.belga.belg
at be.belga.belgadirect.invoi
at sun.reflect.NativeMethodAc
at sun.reflect.NativeMethodAc
at sun.reflect.DelegatingMeth
at be.belga.belgadirect.invoi
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.
why you put:
log4j.rootLogger=DEBUG, stdout, file ?
log4j.rootLogger=DEBUG, stdout, file ?
>log4j.appender.file.File= ./workspac e/BBFM/bbf m.log
be sure you file could be written on that directory (permission,etc.)
be sure you file could be written on that directory (permission,etc.)
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?
>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
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.log 4j.Console Appender
log4j.appender.stdout.layo ut=org.apa che.log4j. PatternLay out
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layo ut.Convers ionPattern =%5p [%t] (%F:%L) - %m%n
########################## ########## ########## ########## ########## #########
## file output
log4j.appender.R=org.apach e.log4j.Ro llingFileA ppender
log4j.appender.R.File=./wo rkspace/BB FM/bbfm.lo g
log4j.appender.R.MaxFileSi ze=10000KB
log4j.appender.R.MaxBackup Index=10
log4j.appender.R.layout=or g.apache.l og4j.Patte rnLayout
log4j.appender.R.layout.Co nversionPa ttern=%p %t %c - %m%n
########################## ########## ########## ########## ########## #########
like documentation suggest.
log4j.rootLogger=DEBUG, stdout, R
##########################
## console output
log4j.appender.stdout=org.
log4j.appender.stdout.layo
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layo
##########################
## file output
log4j.appender.R=org.apach
log4j.appender.R.File=./wo
log4j.appender.R.MaxFileSi
log4j.appender.R.MaxBackup
log4j.appender.R.layout=or
log4j.appender.R.layout.Co
##########################
like documentation suggest.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I've solve this problem like this: put only this line in the commons-logging.properties file:
org.apache.commons.logging .LogFactor y = org.apache.commons.logging .impl.LogF actoryImpl
Everything works perfectly.
org.apache.commons.logging
Everything works perfectly.
ok. Happy for you.
Bye, Giant.
Bye, Giant.
ASKER
Thank for your answer Giant2.
ASKER