marzapower

Fork me on GitHub

Loggable

A simple log4j meta-library

Loggable is a Java library to be used in conjunction with the Apache log4j standard logging library.

Its main goal is semplifying the logging process throughout the application, thus reducing the usual declarations overhead due to the log4j library.

Download

All the source code and compiled packages for the Loggable meta-library are hosted on GitHub. Please follow this link to the main project page

Example

A standard call to the log4j library is written this way:

import org.apache.log4j.Logger;
public class Log4jCaller {
    private static final Logger logger = Logger.getLogger(Log4jCaller.class);

    public static void main(String... args) {
        logger.debug("A simple log call");
    }
}

The Loggable meta-library requires a lighter approach:

import com.marzapower.loggable.*;
 
@Loggable
public class LoggableCaller {
    public static void main(String... args) {
        Log.get().debug("A simple log call");
    }
}

This definition will have the same effect at runtime. A still valid approach is the following:

public class RootLoggerCaller {
    public static void main(String... args) {
        Log.get().debug("A simple log call");
    }
}

where the @Loggable annotation is missing. This implementation would be equivalent to the first example, if we changed the logger definition to Logger.getRootLogger() instead of Logger.getLogger(Log4jLogger.class).

How does it work

The Loggable meta-library uses two main elements: the @Loggable annotation and the Log class. The first drives the logging logic of our classes, while the latter is a centralized controller that each class has to call in a static way. While, using only the base log4j library, each class had to define a private org.apache.log4j.Logger instance, and then had to invoke methods on this instance, now each class must be annoted with the @Loggable annotation and must call the Log class directly for logging.

We can increase the ease of implementation of the logging architecture doing the following:

  • Design an appropriate log4j configuration, so that the root logger can be used equally by each class

  • Write down the code of the application classes, without using any additional parameter or annotation or private logger instance

  • Within each class, call Log.get() to retrieve the Logger instance to be used directly.

  • What will happen is that each class, using this approach, will log using the log4j root logger.

We can easily convert this code:

public class Log4jCaller1 {
    private static final Logger logger = Logger.getRootLogger();
    public void log() {
        logger.debug("A simple log call");
    }
}
 
final class Log4jCaller2 {
    private static final Logger logger = Logger.getRootLogger();
    public void log() {
        logger.debug("A simple log call");
    }
}
 
final class Log4jCaller3 {
    private static final Logger logger = Logger.getRootLogger();
    public void log() {
        logger.debug("A simple log call");
    }
}

into this:

public class LoggableCaller1 {
    public void log() {
        Log.get().debug("A simple log call");
    }
}
 
final class LoggableCaller2 {
    public void log() {
        Log.get().debug("A simple log call");
    }
}
 
final class LoggableCaller3 {
    public void log() {
        Log.get().debug("A simple log call");
    }
}

Speed up development time

This meta-library will surely speed up development time. This is true because we no longer have to worry about useless definitions. We can insert that Log.get() call everywhere with no additional effort, and every logging call will succeed. By default in fact, every class can at least log through the root logger.

So, we do not have to worry about the definition of the local logger instance, neither have to worry about deleting this instance when we erase every logging call from our class. If we want our class to log into its dedicated logger, we just have to add the short @Loggable annotation before the class definition. If we want to modify the logger for that class, we will just change some parameters passed to the annotation.

A more complex example

This is a more complex example:

import com.marzapower.loggable.*;
import com.marzapower.loggable.Loggable.LogLevel;
 
@Loggable(clazz = MyOtherClass.class, logLevel = LogLevel.ERROR)
public class LoggableComplexCaller {
    public void log() {
        Log.get().debug("This will never be logged");
        Log.get().error("But this surely will!");
    }
}
 
@Loggable(loggerName = "myLoggable")
final class MyLoggableCaller {
    public void log() {
        Log.get().debug(
                "A simple log call, " +
                "using the myLoggable " +
                "logger instance");
    }
}
 
@Loggable(exclude = true)
final class LoggableSilentCaller {
    public void log() {
        Log.get().debug("This will never be logged");
    }
}

The following will happen:

LoggableComplexCaller will log using the logger instance dedicated to the MyLogger class (the equivalent of Logger.getLogger(MyOtherClass.class)), and only messages whose level is ERROR or higher will be logged MyLoggableCaller will log using the log4j logger named “myLoggable”, if present in the log4j configuration files LoggableSilentCaller will never log, since Log.get() will return a silent logger for this class.

Additional info

You can find a detailed introduction and explanation of the Loggable meta-library in these two posts: