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.

136 thoughts on “Loggable

  1. Great – I should certainly pronounce, impressed with your site. I had no trouble navigating through all the tabs as well as related info ended up being truly easy to do to access. I recently found what I hoped for before you know it in the least. Quite unusual. Is likely to appreciate it for those who add forums or anything, web site theme . a tones way for your client to communicate. Excellent task..

  2. What i do not understood is if truth be told how you’re now not actually a lot more neatly-liked than you might be now. You are so intelligent. You recognize thus significantly with regards to this matter, produced me personally consider it from a lot of numerous angles. Its like men and women aren’t fascinated unless it’s something to accomplish with Lady gaga! Your individual stuffs great. Always take care of it up!

  3. Great – I should definitely pronounce, impressed with your website. I had no trouble navigating through all tabs as well as related info ended up being truly simple to do to access. I recently found what I hoped for before you know it in the least. Reasonably unusual. Is likely to appreciate it for those who add forums or something, website theme . a tones way for your customer to communicate. Nice task.

  4. Usually I do not read article on blogs, but I wish to say that this write-up very compelled me to take a look at and do so! Your writing taste has been surprised me. Thank you, quite great article.

  5. Nice post. I was checking constantly this blog and I am impressed! Very useful info particularly the last part 🙂 I care for such info a lot. I was seeking this certain information for a long time. Thank you and best of luck.

  6. Whats up this is somewhat of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding skills so I wanted to get advice from someone with experience. Any help would be enormously appreciated!

  7. I do believe all the ideas you’ve presented in your post. They’re very convincing and can certainly work. Still, the posts are very short for novices. May just you please prolong them a bit from subsequent time? Thanks for the post.

  8. There are certainly plenty of particulars like that to take into consideration. That is a great level to convey up. I supply the thoughts above as basic inspiration however clearly there are questions just like the one you carry up where crucial thing will likely be working in trustworthy good faith. I don?t know if greatest practices have emerged round issues like that, however I am positive that your job is clearly recognized as a fair game. Both girls and boys feel the influence of only a moment’s pleasure, for the rest of their lives.

  9. Wonderful blog! Do you have any hints for aspiring writers? I’m planning to start my own website soon but I’m a little lost on everything. Would you recommend starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m completely confused .. Any recommendations? Thanks!

  10. I’m extremely impressed with your writing skills and also with the layout on your blog. Is this a paid theme or did you modify it yourself? Either way keep up the excellent quality writing, it is rare to see a great blog like this one nowadays..

  11. I like the helpful info you provide in your articles. I’ll bookmark your weblog and test once more right here frequently. I’m relatively sure I’ll learn many new stuff right right here! Best of luck for the following!

  12. Interesting blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple adjustements would really make my blog jump out. Please let me know where you got your design. With thanks

  13. Hey there! I know this is kinda off topic however I’d figured I’d ask. Would you be interested in trading links or maybe guest writing a blog post or vice-versa? My website goes over a lot of the same topics as yours and I think we could greatly benefit from each other. If you happen to be interested feel free to send me an email. I look forward to hearing from you! Excellent blog by the way!

  14. Someone essentially assist to make critically posts I would state. This is the very first time I frequented your web page and to this point? I amazed with the research you made to create this actual publish amazing. Excellent activity!

  15. I think other web site proprietors should take this site as an model, very clean and magnificent user friendly style and design, as well as the content. You’re an expert in this topic!

  16. I think this website contains some really superb information for everyone. “It is easy enough to define what the Commonwealth is not. Indeed this is quite a popular pastime.” by Elizabeth II.

  17. Very good blog you have here but I was curious about if you knew of any forums that cover the same topics discussed here? I’d really love to be a part of group where I can get opinions from other knowledgeable people that share the same interest. If you have any recommendations, please let me know. Thank you!

  18. Thanks a bunch for sharing this with all of us you actually recognise what you’re talking about! Bookmarked. Please additionally talk over with my site =). We could have a hyperlink trade agreement between us!

  19. My brother recommended I would possibly like this website. He was totally right. This put up actually made my day. You can not imagine simply how a lot time I had spent for this info! Thank you!

Lascia un commento

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