Logging is important for an application. It gives insider information :-) It helps us understand what is happening inside the engine by recording parameters silently and provides vital information. It helps in providing system related information like health status, debugging and provides critical support during production trouble shooting and unexplained failure.
We need unified logging system within our application. We integrate multiple different components/packages to our application. We always do that in real time scenario, for example we integrate Quartz package for scheduling. Then imagine, each such external system requires its own separate logging configuration and each will do their own logging. That will be horrible man! Its like having four steering in a car.
Spring provides central control in terms of logging. We can have one central logging configuration file and control all logging output via that. There are many external components integrated with Spring.
Jakarta Commons Logging API (JCL) is the only mandatory external dependency for Spring!
spring-core defines a dependency on commons-logging. JCL is a thin logging framework that serves as an abstraction for other logging APIs. JCL interface looks similar to Log4j. So, as a Spring developer what should we do? We need to make sure the dependency on JCL is met. commons-logging is an implementation for JCL and it can be used to solve the dependency.
We should add commons-logging to the Spring application. commons-logging discovers what are the logging framework added in the application and chooses one that suits the best and uses it for logging, if we have not added any logging API, then it uses java.util.Logging framework. We can also configure commons-logging to use a certain logging framework.
We say highly configurable and we say that there is a hard dependency with JCL. Yes it is but we have our choice of implementation. JCL is fine but I don’t like the implementation commons-logging. Ok, how do we force Spring not to use commons-logging? Yes it is possible lets disable commons-logging and use some other alternate implementation of JCL. It can be achieved easily when we use Maven,
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.0.0.RELEASE</version> <scope>runtime</scope> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
add the above to the dependencies in maven pom file. We are excluding commons-logging from spring core.
Spring says,
“SLF4J is a cleaner dependency and more efficient at runtime than commons-logging because it uses compile-time bindings instead of runtime discovery of the other logging frameworks it integrates…”
no I didn’t say that, don’t shout at me! I am afraid ;-))
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.0.0.RELEASE</version> <scope>runtime</scope> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.5.8</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.5.8</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.5.8</version> <scope>runtime</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> <scope>runtime</scope> </dependency> </dependencies>
Now lets leave JCL and look at underlying logging framework. We want to use Log4j as the underlying logging component how do we do it. Assume we are using commons-logging then it is simple and direct, just add the log4j libraries at classpath. We can do this by adding a dependency in maven pom.
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.0.0.RELEASE</version> <scope>runtime</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> <scope>runtime</scope> </dependency> </dependencies>
Sample log4j.properties to use,
log4j.rootCategory=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n log4j.category.org.springframework.beans.factory=DEBUG
Note:
We have a common problem related to Spring logging configuration. When we use use Tomcat kind of servlet containers it is ok. When we use muscular IBM Websphere Application Server (WAS) kind of servers, they come bundled with their own implementation of JCL. It conflicts with Spring configuration. Just disable commons-logging and things will become good.
Comments are closed for "Logging in Spring".
Thanks Joe .. a good read :)
The explanation of is so understandable in this article. My hands are scratching to code and try on. Again a complicated world, which made someone’s (Joe’s) vision helped all of us to understand things in more simpler way!! A really nice explanation, Joe!! Sometime I think, if VMWare made everything of own, like: AOP, Security, Transaction, Batch, Messaging (JMS, JMX), MVC, web flow. then why not logging too? But if VMWare will take each of it, then in future there will be no point of JEE it might turned in to SEE(Spring Enterprise Edition) :-). Wish, how the Java world could be if, in place of Oracle, VMWare would have capabilities to take over Sun Microsystems!! It would have everything like flying colors for we java developers!!
Great article Joe. Nice sharing of knowledge!!
Useful Information, Thanks Joe…
hi Joe,
nice tutorial..
hope you remember me…
we worked together in locus :)
~Prakash
Hi Joe,
It was good tutorial.
well explained..
Truth of Loggging is revealed to developers even who have 10+ years Java experience.
Keep up your good work with KISS (Keep It Simple, Stupid)
Really Nice explaination Joe!
Nice article Mr.Joe, Can you share article on basics of web-services and trends.
nice post …
Very nice blog, keep on the good job and thanks for sharing with us!
Enlightening.
Great information about logging in spring. Am working with spring, but never knew about common-logging and jcl stuff. Thanks a ton.!!
Thanks Joe
for info on commons-logging and jcl
so nice explanation
thank you so much .
Great and simple explanation
Very good explanation Joe. Keep the work up..
A very simple and great explanation Joe..Thanks a ton :-)
Sachin
I like the simplicity whith which you convey the different java features and concepts. Keep talkin we like it.
Could you add a search to locate a specific topic quickl? :)
hi joe what a beautiful
fantanstic
blogs
This is very useful.
Can you please give us Spring AOP (Aspect-oriented programming) framework for us…?
Hi Joe. I have problem running my application (using logback) in IBM websphere. I just wonder how I could “disable commons-logging” wich is bundled with Websphere so that the implementation of LogFactory from jcl-over-slf4j is used.
i felt that the explanation is at very high level…It would be helpul if u dig into details…expecting explanation on Maven…tx a lot for ur blog
Thank you so much for this very informative article. I found it to be an excellent explanation of a complex topic that I was having trouble grappling with.
Welcome Chris.
Joe, I would visit your site before i go Google. You are awesome. keep updating technical skills and staying on top of them.
— Find me
Hi Joe, Nice Tutorial on Logging with spring. you have given only the configuration details, if you give the full tutorial for logging with spring, will be helpful.
Thanks for the information.