Loggable: a simple log4j meta-library – Part 2

In a recent post I described the goal of my Loggable meta-library. If you haven’t read it yet, I suggest you to do it now.

What we are going to do now is analysing the second main component of the meta-library, the centralized controller Log.

How does it work

The main responsibility of the Log class is handling the logic behind the creation and returning of the Logger instance that the caller class should use. This logic, as we’ve already seen, is driven by the @Loggable annotation.

The Log class will have a single point of access: the public static method get(). Let’s look at if, briefly:

[java highlight=”9″][RAW]
public static Logger get() {
// Retrieve the StacKTraceElement of the caller method
StackTraceElement caller = getCaller();
// Retrieve the caller class
Class<?> clazz = getClass(caller);
// and its Loggable annotation instance
Loggable annotation = getAnnotation(clazz);

logger.debug("Returning a Logger for " + clazz.getSimpleName());

// Retrieve the suitable logger for the caller class and returns it
Logger suitableLogger = getLogger(annotation, clazz);
return suitableLogger;
}
[/RAW][/java]

This method does a precise series of things:

  1. Retrieves the stack trace element related to the caller method
  2. Retrieves the Class<?> instance which describes the caller class
  3. Reads the @Loggable annotation from the caller class
  4. Returns the suitable Logger instance, depending on the class and its @Loggable annotation definition

The whole code for this class will be available within the meta-library package, so I’ll focus just on the main private method: getLogger().

Bonus hint: as you can see, in my development environment the Log class makes a direct access to a Logger instance. This is strictly necessary because of recursions problems: if the Log class would call itself for logging purposes, this will generate a never-ending recursion stack. Try it, and try to explain why: this will be your homework for today 🙂 .

The private getLogger() method

This method is the key for everything, I’d say. It controls the logic and the interpretation of the annotation parameters. It handles the Logger instance creation and retrieval and will be in our scope in the next paragraphs. The source code is the following:

[java][RAW]
private static Logger getLogger(Loggable annotation, Class<?> clazz) {
if (annotation == null)
return LoggerContainer.getRootLogger();

if (annotation.exclude())
return LoggerContainer.getVoidLogger();

// If a "loggerName" parameter has been passed to the annotation,
// returns that specific logger instance
if (!"".equals(annotation.loggerName()))
return LoggerContainer.getInstance(annotation.loggerName(), annotation);

// If no "class" parameter has been passed to the annotation, returns
// the default logger for the caller class …
if (annotation.clazz() == Object.class)
return LoggerContainer.getInstance(clazz, annotation);

// … otherwise use the "class" parameter to build the logger
return LoggerContainer.getInstance(annotation.clazz(), annotation);
}
[/RAW][/java]

What does it do? Just what we said in the previous “episode”:

  1. If no @Loggable annotation has been set for the class, returns the root logger
  2. If the annotation requires the class to be excluded by logging, returns a void logger
  3. If the annotation defines a logger name, returns that logger (= Logger.getLogger(annotation.loggerName()) )
  4. If the clazz parameter of the annotation is still the default (Object.class), returns the default logger for the caller class (= Logger.getLogger(CallerClass.class) )
  5. Otherwise, returns the default logger with the class specified in the annotation (= Logger.getLogger(annotation.clazz()) )

How does this work? This method uses a third class, LoggerContainer. This is a really simple container: when a logger is required, if it has already been built and customized, the same object will be returned; otherwise, the new instance is built, customized, added to the local container and finally returned. This will save time and memory when we call the Log.get() method. (It is a somewhat similar implementation of the static final definition for the usual logger instance).

Conclusion

Given these three simple components, two classes and a single annotation, I tried to build a handy log4j meta-library. I am currently using it in a bunch of personal projects as it has drastically simplified the logging process throughout the whole applications. I found it very helpful and customizable.

I still need some feedback on this project. I will try to complete the setup of the package, and hope to release version 1.0 of the meta-library as soon as possible.

See you soon!

Update: the first official release is now available! Give it a try!

1.372 thoughts on “Loggable: a simple log4j meta-library – Part 2

  1. Thank you so much for giving everyone remarkably terrific chance to read critical reviews from this blog. It’s usually very cool and also packed with a great time for me and my office co-workers to visit the blog more than 3 times per week to read the fresh issues you will have. And definitely, we are at all times satisfied with your tremendous tactics served by you. Selected two areas in this posting are rather the best I’ve had.

  2. I am only commenting to let you be aware of what a superb discovery my cousin’s girl went through visiting your site. She discovered plenty of pieces, which include what it is like to have a marvelous helping nature to get other individuals quite simply know just exactly a number of multifaceted subject matter. You truly surpassed our expected results. Many thanks for imparting such valuable, healthy, educational and also easy thoughts on your topic to Tanya.

  3. Howdy! This is my 1st comment here so I just wanted to give a quick shout out
    and tell you I genuinely enjoy reading your posts. Can you suggest any other blogs/websites/forums that deal with the same subjects?
    Thanks!

  4. I loved as much as you will receive carried out right here.
    The sketch is attractive, your authored subject matter stylish.

    nonetheless, you command get got an impatience over that you wish be delivering the following.
    unwell unquestionably come more formerly again as exactly the same
    nearly a lot often inside case you shield this hike.

  5. Thanks for your write-up. I also believe laptop computers are becoming more and more popular today, and now are usually the only sort of computer employed in a household. Simply because at the same time actually becoming more and more reasonably priced, their computing power is growing to the point where they are as highly effective as desktop computers coming from just a few in years past.

  6. Thanks for expressing your ideas. The one thing is that individuals have a selection between government student loan along with a private student loan where it is easier to decide on student loan debt consolidation than through the federal student loan.

  7. Excellent blog! Have you got any hints for aspiring writers?
    I’m hoping to get started on my site soon but I’m a little bit
    lost on everything. Can you advise beginning from a totally
    free platform like WordPress or take a paid option? There are plenty of choices around that I’m completely confused ..
    Any ideas? Many thanks!

    Feel free to surf to my page … NitaUDelva

Lascia un commento

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