MasteringLogging

Go to main

Logging With Slf4j and Slf4j-Simple

In this modules we’ll configure Slf4j-Simple with SLF4J for logging in a simple Java project.

Required dependencies

As this is a simple Java project and does not use any build tools like Maven or Gradle etc., so we’ll have to add the required dependencies explicitly. Following are the dependencies required for logging:

Code for testing logs

Following is the simple sample code to test different log levels:

package au.com.lucidtech.slf4jsimple;

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

public class LoggingExample {
    private static final Logger logger = LoggerFactory.getLogger(LoggingExample.class);

    public static void main(String[] args) {
        final String parameter = "logging";
        if (logger.isTraceEnabled()) {
            logger.trace("This is trace " + parameter);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("This is debug " + parameter);
        }

        if (logger.isInfoEnabled()) {
            logger.info("This is info " + parameter);
        }

        logger.warn("This is warn " + parameter);
        logger.error("This is error " + parameter);
    }
}

Now, if we run this code, we should be able to see the logs with the default settings/configurations.

Customised Logging

If we want to customize logging in order to be able to:

then we’ll need to add the simplelogger.properties file somewhere on the classpath.

Sample simplelogger.properties file

The following simplelogger.properties can be placed in the src/main/resources folder:

org.slf4j.simpleLogger.defaultLogLevel=trace
org.slf4j.simpleLogger.showDateTime=true
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd 'at' HH:mm:ss.SSS
org.slf4j.simpleLogger.showThreadName=true
org.slf4j.simpleLogger.levelInBrackets=true

Where should the configuration files such as simplelogger.properties be located on the classpath?

Configuration files such as simplelogger.properties can be located directly under any folder declared in the class path. For example, if the class path reads c:/java/jdk15/lib/rt.jar;c:/mylibs/, then the simplelogger.properties file should be located directly under c:/mylibs/, that is as c:/mylibs/simplelogger.properties. Placing it under a sub-folder of c:/mylibs/, say, c:/mylibs/other/, will not work.

For web-applications, configuration files can be placed directly under WEB-INF/classes/. So you need to put simplelogger.properties in the classpath.

Why simplelogger.properties or config file ignored or not picked up or doesn’t work?

We need to ensure that the config file is on the class path. We might have created the file in the src/main/resources folder and might assume that, that’s it, it should work now. But unfortunately that’s not enough as we need to ensure that it gets copied over to the target folder (or wherever you’re compiling your project files).

For Maven

So, if we are using some build tool like Maven then we can use the maven’s resource plugin to copy them over.

For running the application in the IDE

When running the application in IDEs, ensure that the resources folder is marked as resources in project setting, as that’ll ensure that the simplelogger.properties inside resources folder is copied over to the target folder (and that’s where the config file will be searched for configuration when the application is started). mark-resources.png

Go to main