Spring – Setting up Logging using Logback and Maven

Setting up Logging in any Maven based Spring project is pretty much straight-forward. Below are the generic code snippets to integrate logging using Simple Logging Facade (SL4J) and Logback.

What is SL4J ?

Simple Logging Facade for Java or SL4J serves as a simple facade or abstraction for various logging frameworks, such as java.util.logging, Logback and log4j. SLF4J allows the end-user to plug in the desired logging framework at deployment time. Note that SLF4J-enabling your library/application implies the addition of only a single mandatory dependency, namely slf4j-api-x.x.x.jar.


In short , Logback is intended as a successor to the popular log4j project, picking up where log4j leaves off.

Logback’s architecture is sufficiently generic so as to apply under different circumstances. At present time, Logback is divided into three modules, logback-core, logback-classic and logback-access.

The logback-core module lays the groundwork for the other two modules. The logback-classic module can be assimilated to a significantly improved version of log4j. Moreover, logback-classic natively implements the SLF4J API so that you can readily switch back and forth between logback and other logging frameworks such as log4j or java.util.logging (JUL).

The logback-access module integrates with Servlet containers, such as Tomcat and Jetty, to provide HTTP-access log functionality. Note that you could easily build your own module on top of logback-core.



To use SLF4J with Spring you need to replace the commons-logging dependency with the SLF4J-JCL bridge. Once you have done that, then logging calls from within Spring will be translated into logging calls to the SLF4J API, so if other libraries in your application use that API, then you have a single place to configure and manage logging.

To switch off commons-logging, the commons-logging dependency mustn’t be present in the classpath at runtime. If you are using maven, you can simply exclude it from the spring-context artifact :


Required Dependencies :

Because I am using LogBack, which implements SLF4J directly, you will need to depend on two libraries (jcl-over-slf4j and logback):


Configuring Logback

To configure Logback all you have to do is place the file logback.xml in the classpath.

Logback uses appenders, which are components Logback delegates the task of writing logging events to.


JavaScript custom empty() function similar to empty() in PHP

The most common error that arises in the programming world is the usage of “empty” variables in the code logic.

In PHP , an empty variable is considered if any of the following conditions are met :

  • it’s null
  • it’s an empty string ( “” or ” ” or ”      ” )
  • it’s a Boolean false value
  • it’s an array or an object with no keys or values

PHP provides a very simple way to check for an empty value by using empty() function.  However , JavaScript does not provides any such implementation .

Following is the custom empty() function written in JavaScript that helps checking of “empty” variables and sanitation of code.

The above function works very similar to that of PHP empty() function and is extremely handy for checking variable existence and validation before using them straightway in the JavaScript code.

Happy Hacking ! 🙂

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

© 2016 Arpit Gaur

Theme by Anders NorénUp ↑