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.

3.680 thoughts on “Loggable

  1. Thanks for your whole effort on this site. My mom delights in participating in internet research and it’s easy to see why. I know all relating to the powerful way you make very useful solutions through the website and therefore boost response from people on that concern while our own daughter is always discovering so much. Take pleasure in the remaining portion of the year. You’re performing a brilliant job.

  2. My coder is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of
    the expenses. But he’s tryiong none the less. I’ve been using Movable-type on several websites for about a year and am worried
    about switching to another platform. I have heard great things about blogengine.net.
    Is there a way I can import all my wordpress content into
    it? Any help would be really appreciated!

  3. I was wondering if you ever thought of changing the layout of your website?

    Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so
    people could connect with it better. Youve got an awful lot of text for only having 1 or 2 pictures.
    Maybe you could space it out better?

  4. Have you ever thought about writing an ebook or guest authoring on other blogs?
    I have a blog centered on the same subjects you discuss and would really like to have
    you share some stories/information. I know my audience would
    value your work. If you are even remotely interested, feel free to shoot me an e-mail.

  5. After I initially commented I seem to have clicked on the -Notify
    me when new comments are added- checkbox and from now
    on every time a comment is added I recieve 4 emails with the same comment.
    Is there an easy method you can remove me from that service?
    Thank you!

  6. I am also writing to make you understand of the helpful discovery my princess encountered reading through your web site. She realized so many things, including how it is like to possess a marvelous coaching spirit to have many more really easily thoroughly grasp some hard to do subject areas. You really did more than our desires. Thanks for churning out such informative, trustworthy, edifying and as well as fun thoughts on your topic to Mary.

  7. Thank you so much for providing individuals with a very remarkable possiblity to read in detail from this web site. It’s always so nice plus full of fun for me and my office colleagues to search your site minimum 3 times per week to see the new things you have. And definitely, I am also actually fulfilled for the splendid thoughts served by you. Selected two facts in this post are ultimately the most suitable we’ve ever had.

  8. Its like you read my mind! You seem to know a lot about this,
    like you wrote the book in it or something.
    I think that you can do with some pics to drive
    the message home a bit, but instead of that, this is fantastic blog.

    An excellent read. I will certainly be back.

  9. When I originally commented I seem to have clicked on the -Notify me when new comments are added- checkbox and from now on each time a comment is added I get four emails with the same comment. Is there an easy method you can remove me from that service? Cheers.

  10. I’ve been exploring for a little for any high-quality
    articles or blog posts on this sort of house . Exploring in Yahoo I finally
    stumbled upon this site. Reading this info So i’m satisfied to express that
    I have a very excellent uncanny feeling I found out just
    what I needed. I most undoubtedly will make certain to
    do not disregard this website and provides it a glance regularly.

  11. What’s Happening i’m unfamiliar with this, I found this
    I’ve discovered It absolutely useful and possesses aided
    me out loads. I am hoping to give you a contribution & assist different users like its
    aided me. Good job.

    My web page TammiQCheser

Lascia un commento

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