Converting from log4j to logback - tooltwist/documentation GitHub Wiki

Up till v8.1 ToolTwist has used log4j as it's industry standard logging library. In v8.1 it's time to upgrade to the newer logback library (initiated by the same developer).

Step 1 - Gradle configuration

In the build.gradle file, remove reference to log4j and add the following lines to the dependencies:

    // Dependancies
    ...
    compile group: 'ch.qos.logback', name: 'logback-core', version: '0.9.30'
    compile group: 'ch.qos.logback', name: 'logback-classic', version: '0.9.30'
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.6.2'

Step 2 - Import statements

Logback contains similar class names, but in different packages. In this step, replace every instance of

import org.apache.log4j.Logger;

and replace it with

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Step 3 - Factories

Replace each instance of

static Logger logger = Logger.getLogger(Class);

with

static Logger logger = LoggerFactory.getLogger(Class);

Step 4 - API changes

Several methods from log4j do not exist in logback:

logger.debug(Exception)
logger.info(Exception)
logger.error(Exception)
etc

These should be replaced with their more detailed equivalents:

logger.debug(String, Exception)
logger.info(String, Exception)
logger.error(String, Exception)
etc

Step 5 - Converting config files

There's a great utility at http://logback.qos.ch/translator/, where you simply paste in the text of your log4j config file, and it will display the equivalent logback config.

Useful links

http://logback.qos.ch/index.html
http://bachman.pl/devel/logback-maven-config-in-2-minutes/
http://logback.qos.ch/manual/configuration.html
http://nilshb.wordpress.com/2009/10/08/rip-log4j-hello-logback/