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:
- Design an appropriate log4j configuration, so that the root logger can be used equally by each class
- Write down the code of the application classes, without using any additional parameter or annotation or private logger instance
- 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:
- 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
- MyLoggableCaller will log using the log4j logger named “myLoggable“, if present in the log4j configuration files
- 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.
Hello! I could have sworn I’ve been to this site before but after looking at many of the posts I realized it’s new to me. Anyways, I’m definitely happy I stumbled upon it and I’ll be bookmarking it and checking back often!
Greetings, I believe your website may be having internet browser compatibility problems. Whenever I look at your site in Safari, it looks fine however, when opening in I.E., it’s got some overlapping issues. I just wanted to give you a quick heads up! Besides that, excellent site!
Having read this I believed it was rather informative. I appreciate you finding the time and effort to put this article together. I once again find myself spending a significant amount of time both reading and commenting. But so what, it was still worth it!
Greetings! Very helpful advice within this post! It is the little changes that produce the most significant changes. Thanks for sharing!
I knoow this ѕitе gives qսality baseɗ articles and other data, is there аny other web page whicһ gives such
data iin quality?
sildenafil 150 mg http://cialistedp.com/ best male ed pills Rnsghy eewixc
levitra vardenafil http://levitrosx.com/ treatment for ed Rognah eljkek
Hi there! This blog post couldn’t be written much better! Reading through this article reminds me of my previous roommate! He always kept preaching about this. I will forward this post to him. Pretty sure he will have a very good read. Thanks for sharing!
I needed to thank you for this fantastic read!! I definitely loved every little bit of it. I have got you saved as a favorite to look at new stuff you post?
You’re so cool! I do not think I’ve read something like that before. So great to find someone with some original thoughts on this subject. Seriously.. many thanks for starting this up. This web site is something that is needed on the web, someone with some originality!
I love it when people come together and share thoughts. Great blog, keep it up!
Howdy! I could have sworn I’ve been to this web site before but after looking at some of the posts I realized it’s new to me. Anyways, I’m definitely delighted I stumbled upon it and I’ll be bookmarking it and checking back often!
Hello! Do you use Twitter? I’d like to follow you if that would be ok. I’m undoubtedly enjoying your blog and look forward to new posts.|
You are a great writer thanx. I love sucking dick btw hmu
cialis online without prescription https://ciaaliss.com/
vardenafil pill http://supervardp.com/ cure ed Xyrwqk hcvcwk
Howdy! This blog post couldn’t be written much better! Looking at this article reminds me of my previous roommate! He constantly kept preaching about this. I will forward this information to him. Pretty sure he will have a great read. Thanks for sharing!
When 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 exact same comment. There has to be an easy method you are able to remove me from that service? Thank you!
Everything is very open with a clear clarification of the issues. It was definitely informative. Your site is extremely helpful. Thanks for sharing!
I’m excited to uncover this web site. I want to to thank you for your time for this fantastic read!! I definitely loved every little bit of it and I have you bookmarked to look at new things in your site.
Spot on with this write-up, I truly believe this amazing site needs far more attention. I’ll probably be returning to read through more, thanks for the info!
That is a really good tip especially to those new to the blogosphere. Simple but very accurate info?Thanks for sharing this one. A must read article!
Very good article. I certainly appreciate this website. Keep writing!
When I originally left a comment I seem to have clicked the -Notify me when new comments are added- checkbox and from now on whenever a comment is added I recieve 4 emails with the same comment. There has to be a means you can remove me from that service? Kudos!
Good day! Thіs is my 1st comment һeree so I
just wanted to give a quicҝ shout out aand tell yoս I
genuinely enjoy reading your posts. Can youu recommend
any othedr blogs/ᴡebsites/forums hаt deal with
the same topics? Thhanks for your time!
Here is my ᴡeb page – dewa lapak303
sildenafil 20 http://sildepills.com/ buy ed pills online Zqoaxk rgrech
I was extremely pleased to find this site. I need to to thank you for your time for this particularly fantastic read!! I definitely appreciated every part of it and I have you book marked to look at new information on your site.
prolonged use of viagra side effects womens viagra shops london sell viagra