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.

4.508 thoughts on “Loggable

  1. Thank you for each of your effort on this site. My daughter takes pleasure in conducting internet research and it’s really simple to grasp why. Most people notice all regarding the dynamic tactic you present advantageous information via your blog and in addition strongly encourage participation from the others on that concern and my simple princess is now understanding a great deal. Take pleasure in the remaining portion of the year. You’re carrying out a remarkable job.

  2. I’ve been exploring for a bit for any high quality articles or blog posts on this sort of space . Exploring in Yahoo I finally stumbled upon this website. Reading this information So i am satisfied to convey that I have a very just right uncanny feeling I came upon just what I needed. I most unquestionably will make certain to do not put out of your mind this site and provides it a look on a continuing basis.

  3. Wonderful goods from you, man. I have understand your stuff previous to and you are just too magnificent. I actually like what you’ve acquired here, really like what you’re stating and the way in which you say it. You make it enjoyable and you still care for to keep it sensible. I cant wait to read much more from you. This is really a terrific site.

  4. Pingback: Homepage
  5. 432577 94030Fantastic beat ! I wish to apprentice whilst you amend your web website, how can i subscribe for a blog web site? The account aided me a appropriate deal. I had been just a little bit acquainted of this your broadcast provided bright clear concept 266380

  6. I have been surfing online greater than 3 hours lately, yet I by no means discovered any fascinating article like yours. It’s beautiful worth enough for me. In my opinion, if all website owners and bloggers made just right content as you probably did, the internet might be much more helpful than ever before. “Nothing will come of nothing.” by William Shakespeare.

  7. Hmm it looks like your blog ate my first comment (it was extremely
    long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying
    your blog. I as well am an aspiring blog blogger but I’m still new to
    everything. Do you have any tips and hints for rookie blog writers?
    I’d definitely appreciate it.

  8. I actually wanted to jot down a simple note to be able to appreciate you for some of the splendid tips and tricks you are placing on this site. My time consuming internet search has at the end of the day been recognized with good quality information to go over with my relatives. I ‘d state that that we visitors are undoubtedly endowed to dwell in a notable network with so many perfect individuals with valuable tactics. I feel very much grateful to have seen the weblog and look forward to some more entertaining moments reading here. Thanks a lot once more for everything.

  9. I needed to write you the little word so as to say thanks a lot once again for your personal fantastic solutions you’ve discussed in this case. It is quite shockingly open-handed with you to provide easily exactly what most people could have made available for an e book to help make some dough on their own, mostly considering the fact that you might have tried it in the event you decided. These creative ideas in addition acted to be a good way to fully grasp other people online have a similar dreams much like my own to know lots more with regard to this condition. I believe there are some more fun times in the future for many who check out your website.

  10. Wow, superb weblog structure! How long have you been blogging for? you make running a blog glance easy. The total glance of your website is great, let alone the content material!

  11. Thanks for the sensible critique. Me and my neighbor were just preparing to do some research about this. We got a grab a book from our local library but I think I learned more clear from this post. I’m very glad to see such fantastic info being shared freely out there.

  12. I was excited to uncover this website. I wanted to thank
    you for ones time just for this fantastic read!! I definitely appreciated every bit of it and
    i also have you book marked to see new stuff in your website.

  13. Generally I do not learn article on blogs, however I
    would like to say that this write-up very pressured me to take a look at and do so!
    Your writing style has been amazed me. Thanks, very
    great post.

  14. Great – I should definitely pronounce, impressed with your website. I had no trouble navigating through all tabs as well as related information ended up being truly simple 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 something, website theme . a tones way for your client to communicate. Excellent task.

  15. I am really loving the theme/design of your site. Do you ever run into any browser compatibility issues? A handful of my blog visitors have complained about my blog not operating correctly in Explorer but looks great in Safari. Do you have any advice to help fix this problem?

  16. I do accept as true with all of the concepts you have
    offered for your post. They’re very convincing
    and will definitely work. Nonetheless, the posts are very
    brief for novices. May you please prolong them a little
    from subsequent time? Thanks for the post.

    Feell free to visit my blog :: hunter burn reviews

  17. I’m extremely pleased to discover this website. I want to to thank you for your time for this fantastic read!! I definitely loved every bit of it and I have you bookmarked to check out new things on your website.

  18. You’re so interesting! I don’t suppose I have read anything like this before. So good to discover someone with original thoughts on this topic. Seriously.. thanks for starting this up. This website is one thing that is required on the internet, someone with a bit of originality!

  19. When someone writes an article he/she maintains the image of
    a user in his/her mind that how a user can be aware of
    it. Therefore that’s why this article is perfect. Thanks!

    my website … سئو چیست (Maximo)

  20. I blog frequently and I truly thank you for your information. Your article has really peaked my interest. I am going to bookmark your site and keep checking for new details about once a week. I subscribed to your RSS feed too.

  21. I would like to thank you for the efforts you’ve put in penning this site. I really hope to view the same high-grade blog posts by you in the future as well. In fact, your creative writing abilities has inspired me to get my own website now 😉

  22. I blog often and I truly appreciate your information. The article has really peaked my interest. I’m going to book mark your blog and keep checking for new details about once a week. I subscribed to your Feed as well.

  23. Having read this I believed it was rather informative. I appreciate you spending some time and energy to put this information together. I once again find myself personally spending way too much time both reading and posting comments. But so what, it was still worth it!

  24. Right here is the right webpage for anybody who really wants to understand this topic. You understand a whole lot its almost tough to argue with you (not that I personally would want to…HaHa). You definitely put a brand new spin on a topic that has been discussed for ages. Wonderful stuff, just excellent!

  25. You’re so cool! I do not suppose I’ve read a single thing like that before. So great to discover somebody with a few unique thoughts on this issue. Seriously.. many thanks for starting this up. This website is one thing that’s needed on the web, someone with a bit of originality!

Lascia un commento

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