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.
I love it when people get together and share ideas. Great blog, continue the good work!
This site was… how do you say it? Relevant!! Finally I have found something that helped me. Thank you!
Great goods from you, man. I have be mindful yoᥙr stuff previous tоo andd you’re simply too excellent.
I actually lіke what you haᴠe recceived here, realⅼy like wһat you’re stɑting and the waү in which
in which you say it. You make it entertaining and you still care for to keep it
sensible. I can not wait to ⅼearn much more from you.
This is really a great web site. http://Www.Campaniainvetrina.com/profile/AdriannaOn
Very good post! We will be linking to this particularly great article on our website. Keep up the good writing.
Great info. Lucky me I came across your website by chance (stumbleupon). I have saved as a favorite for later!
Howdy! This blog post could not be written any better! Reading through this post reminds me of my previous roommate! He constantly kept talking about this. I’ll send this post to him. Pretty sure he will have a good read. Thank you for sharing!
Next time I read a blog, Hopefully it does not disappoint me as much as this one. After all, I know it was my choice to read through, nonetheless I really believed you’d have something useful to talk about. All I hear is a bunch of moaning about something that you could fix if you weren’t too busy looking for attention.
Can you tell us more about this? I’d care to find out more
details.
Having read this I thought it was very informative. I appreciate you taking the time and effort to put this article together. I once again find myself spending way to much time both reading and commenting. But so what, it was still worth it!
I like reading through a post that can make people think. Also, thanks for allowing for me to comment!
A motivating discussion is worth comment. I think that you ought to write more about this subject, it may not be a taboo matter but usually people don’t discuss such topics. To the next! Cheers!!
You should be a part of a contest for one of the most useful sites on the internet. I will highly recommend this website!
Hello! I could have sworn I’ve visited this blog before but after looking at many of the posts I realized it’s new to me. Nonetheless, I’m definitely delighted I discovered it and I’ll be book-marking it and checking back frequently!
Pretty! This was an incredibly wonderful post. Thanks for providing this info.
Hmm is anyone else encountering problems with the pictures on this blog
loading? I’m trying to determine if its a problem on my end
or if it’s the blog. Any suggestions would be greatly appreciated.
Also visit my site travelers notebook
I was recօmmended thiis blog by my cousin. I’m not sure whether this post iѕ written by hhim as nobody else kow such detailed
about mmy problеm. You’re wondеrful! Thanks! https://weekly-wiki.win/index.php/Mainan_Situs_Judi_Slot_Online_Terbaik_Setidaknya_Terkenal_Diputar
Wow, awesome blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is excellent, as well as the content!
Excellent post. Keep posting such kind of info on your site. Im really impressed by your blog.
casino online games for real money http://casinomnx.com/ online casino Cozhhm catxsi
Valuable information. Fortunate me I discovered your web site accidentally, and I am stunned why this accident did not came about in advance! I bookmarked it.
After going over a number of the blog articles on your website, I truly appreciate your way of blogging. I book marked it to my bookmark website list and will be checking back in the near future. Take a look at my website as well and tell me how you feel.
Hello there! This blog post could not be written much better! Reading through this article reminds me of my previous roommate! He always kept preaching about this. I am going to send this article to him. Pretty sure he will have a great read. I appreciate you for sharing!
You’ve made some good points there. I looked on the internet to find out more about the issue and found most individuals will go along with your views on this site.
Spot on with this write-up, I really feel this web site needs a lot more attention. I’ll probably be returning to see more, thanks for the information!
When I initially commented I seem to have clicked on the -Notify me when new comments are added- checkbox
and now each time 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?
Appreciate it!
essay helpers http://essay2y.com/ cheap term papers Zcfezj hhbuts
It’s great that you are getting ideas from this article as well as from our dialogue made at this time.|
As the admin of this website is working, no hesitation very rapidly it will be renowned, due to its feature contents.|
Wonderful work! This is the kind of information that are supposed to be shared around the web. Shame on Google for now not positioning this submit higher! Come on over and discuss with my site . Thank you =)|
Hі, I do believe this iis а great web ѕite. I stumbledupon it 😉
I may come back yet again since i have booкmarked іt.
Mօneey and freedom is the greatest way to
change, mɑy y᧐սu bbe rich and continue to guide otheг рeople. http://625War.kr/?document_srl=883136
I believe that avoiding highly processed foods is the first step to help lose weight. They may taste good, but packaged foods currently have very little vitamins and minerals, making you consume more only to have enough electricity to get over the day. Should you be constantly having these foods, changing to whole grain products and other complex carbohydrates will assist you to have more power while consuming less. Great blog post.