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.

Example

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

[java][RAW]
package com.marzapower.test;

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");
}
}
[/RAW][/java]

The Loggable meta-library requires a lighter approach:

[java][RAW]
package com.marzapower.test;

import com.marzapower.loggable.*;

@Loggable
public class LoggableCaller {
public static void main(String… args) {
Log.get().debug("A simple log call");
}
}
[/RAW][/java]

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

[java][RAW]
package com.marzapower.test;

import com.marzapower.loggable.*;

public class RootLoggerCaller {
public static void main(String… args) {
Log.get().debug("A simple log call");
}
}
[/RAW][/java]

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:

  1. Design an appropriate log4j configuration, so that the root logger can be used equally by each class
  2. Write down the code of the application classes, without using any additional parameter or annotation or private logger instance
  3. 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:

[java][RAW]
package com.marzapower.test;

import org.apache.log4j.Logger;

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");
}
}
[/RAW][/java]

into this:

[java][RAW]
package com.marzapower.test;

import com.marzapower.loggable.Log;

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");
}
}
[/RAW][/java]

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:

[java][RAW]
package com.marzapower.test;

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");
}
}
[/RAW][/java]

The following will happen:

  1. 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
  2. MyLoggableCaller will log using the log4j logger named “myLoggable“, if present in the log4j configuration files
  3. 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:

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

Changelog

v. 1.0.3

  • FIX: A bug in the LoggerContainer class caused an endless loop when asking for a void logger or the root logger

v. 1.0.2

  • FIX: A bug in the LoggerContainer class caused a NullPointerException under certain (rare) circumstances

v. 1.0.1

  • FIX: A bug in the LoggerContainer class caused a stack overflow when calling Log.get()

v. 1.0

  • First official release

Credits and copyright

The Loggable meta-library is released under the GNU Lesser General Public License v. 3.

875 thoughts on “Loggable

  1. Tһis design is ѕpectacular! You certainly know how to keep a reader
    amused. Ᏼetween your wit ɑnd your videos, I was almoѕt
    movеd to start mү own Ьlog (weⅼl, almost…HaHa!) Excellent job.
    I really enjoyed what you haⅾ to sɑy, and more than that, how ʏou presented it.
    Too cool!

  2. Just want to say your article is as astonishing. The clearness in your put up is just excellent and that i could assume you’re an expert on this subject. Well along with your permission let me to clutch your RSS feed to stay updated with forthcoming post. Thank you 1,000,000 and please continue the enjoyable work.|

  3. Greеtings from Ꮯolorado! I’m bߋred to death at work so I ԁecided to check out
    ʏour site on my iphone during lunch break. I enjoy the
    information you present here and can’t wаit to take a loοk when I get home.
    I’m surprised at how quick your bloɡ loɑded on my cell phone ..
    I’m not eѵen using WIFI, just 3G .. Anyways,
    great blog!

  4. Ꮋello, i believe that і saw you visited my site thus
    i came to rеtսrn thе desire?.I am attеmpting to in fіnding things to improve my web site!I assume іts adequate to make use of some of yօur ideas!!

  5. You’re so cool! I do not suppose I have read through something like this before. So great to find somebody with a few original thoughts on this issue. Seriously.. thanks for starting this up. This site is something that is required on the web, someone with some originality.

  6. The next time I read a blog, I hope that it won’t fail me just as much as this one. After all, I know it was my choice to read through, however I actually thought you would have something interesting to say. All I hear is a bunch of whining about something that you could fix if you were not too busy searching for attention.

  7. Everything is very open with a very clear clarification of the challenges. It was truly informative. Your website is very helpful. Thanks for sharing!

  8. Oh my goodness! Incredible article dude! Thank you so much, However I am having difficulties with your RSS. I don’t know why I am unable to subscribe to it. Is there anyone else having similar RSS issues? Anyone that knows the solution will you kindly respond? Thanks!

  9. Hi there! This blog post could not be written much better! Reading through this article reminds me of my previous roommate! He constantly kept talking about this. I’ll send this information to him. Pretty sure he’ll have a great read. I appreciate you for sharing!

  10. The next time I read a blog, I hope that it does not disappoint me as much as this one. I mean, I know it was my choice to read, nonetheless I truly believed you would have something interesting to talk about. All I hear is a bunch of crying about something you could fix if you were not too busy seeking attention.

  11. Oh my goodness! Amazing article dude! Thank you, However I am experiencing problems with your RSS. I don’t understand the reason why I am unable to join it. Is there anyone else getting identical RSS issues? Anyone that knows the answer can you kindly respond? Thanx!!

  12. Good post. I learn something new and challenging on sites I stumbleupon on a daily basis. It’s always interesting to read through content from other writers and use something from other web sites.

  13. Hi there fantastic website! Does running a blog such aѕ tһis take a great deal of work?
    Ӏ have virtually no knowledge of coding but I had been һoping
    to start my own blog soon. Anyways, if you have any
    recommendations or tips for new blog owners pleaѕe share.

    I know thіs іs off subject however I jսst
    wanted to ask. Kudos!

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *