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:
- Retrieves the stack trace element related to the caller method
- Retrieves the Class<?> instance which describes the caller class
- Reads the @Loggable annotation from the caller class
- 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”:
- If no @Loggable annotation has been set for the class, returns the root logger
- If the annotation requires the class to be excluded by logging, returns a void logger
- If the annotation defines a logger name, returns that logger (= Logger.getLogger(annotation.loggerName()) )
- 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) )
- 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!
Pills information leaflet. Effects of Drug Abuse. In Canada
lexapro2021.top generic lexapro
Best trends of medicine. Read here.
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.
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.
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!
Very interesting topic , appreciate it for posting . “Education a debt due from present to future generations.” by George Peabody.
Good day! I simply would like to give you a big thumbs up for the great information you have
here on this post. I will be coming back to your site for more soon.
Right away I am going away to do my breakfast, afterward having my breakfast coming over again to read additional news.
Hello, I enjoy reading through your post. I like to write a little comment
to support you.
Nice answers in return of this difficulty with firm arguments and explaining all about that.
I am happy that I noticed this weblog , exactly the right info that I was searching for! .
OFUSFJ Thanks for sharing this great write-up. Very interesting ideas! (as always, btw)
Pills information leaflet. What side effects? https://zoloft2020.top Everything what you want to know about drugs. Read now.
Major thankies for the blog post. Much obliged.
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.
Wonderful article! We will be linking to this particularly
great content on our site. Keep up the great writing.
You need to take part in a contest for one of the greatest blogs on the web. I most certainly will highly recommend this web site!
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.
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.
Pills prescribing information. What side effects?
flagyl cheap https://flagyl4u.top in US
Everything about meds. Read information here.
Drugs prescribing information. Cautions. In US
doxycycline2021.top cheap doxycycline
Some trends of drugs. Read here.
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
Great blog right here! Additionally your site a lot up very fast! What web host are you using? Can I get your associate link in your host? I want my site loaded up as quickly as yours lol
Undeniably believe that that you simply stated.
Your preferred justification appeared to be about the
web the easiest thing to be familiar with. I have
faith that to you personally, I definitely get irked while people take into
consideration worries that they just do not know about. You been able to
hit the nail upon the top and defined out the whole thing without needing
side-effects , people can go on a signal. Will likely be
back to obtain more. Thanks
Stop by my homepage … 6 ring planner with zipper
You need to take part in a competition for starters in the finest websites online.
I undoubtedly will recommend this page!
my webpage zipper wallet insert
Enjoyed reading through this, very good stuff, regards . “Management is nothing more than motivating other people.” by Lee Iacocca.
I have noticed that online diploma is getting well-liked because attaining your college degree online has become a popular choice for many people. A lot of people have definitely not had an opportunity to attend a normal college or university however seek the increased earning potential and career advancement that a Bachelors Degree gives you. Still others might have a diploma in one training but wish to pursue something they already have an interest in.
You can certainly see your expertise in the work you write. The world hopes for even more passionate writers such as you who aren’t afraid to mention how they believe. All the time go after your heart.
You can definitely see your expertise in the work you write. The world hopes for more passionate writers like you who aren’t afraid to say how they believe. Always follow your heart.
Hi there just wanted to give you a quick heads up. The text in your content seem to be running off the screen in Safari. I’m not sure if this is a format issue or something to do with internet browser compatibility but I figured I’d post to let you know. The design and style look great though! Hope you get the problem resolved soon. Cheers
Undeniably think that which you stated. Your best reason appeared to be around the web the easiest
thing to be aware of. I believe that for you, I certainly get annoyed while people consider
worries that they just tend not to understand about.
You been able to hit the nail upon the top plus defined out everything without the need of side-effects , people could take a signal.
Will likely come back to obtain additional. Thanks
My web blog; bullet journal dotted grid notebook
Meds information leaflet. What side effects? https://doxycyclineph.life All news about medication. Get information here.
смс девушке для поднятия настроения
I precisely needed to thank you so much once more. I am not sure what I would’ve handled without the entire points shown by you about that field. This was a terrifying problem in my opinion, nevertheless taking note of the expert technique you managed the issue took me to jump with delight. I’m happy for this service and even expect you find out what a great job you’re accomplishing teaching the others by way of your blog post. I’m certain you’ve never got to know all of us.
http://kasko-vse.ru/skrytyy_defekt_avtomobilya Скрытый дефект автомобиля
lexapro pay off online 3n81
buy lexapro no prescription
Medicines information. Cautions. where can i buy lisinopril All news about medication. Get now.
I would like to show my appreciation for your kindness in support of those people who should have assistance with your area of interest. Your real commitment to passing the solution across turned out to be really helpful and have surely allowed associates like me to reach their targets. Your new warm and friendly tips and hints means a great deal to me and much more to my fellow workers. Regards; from everyone of us.
I have to get across my respect for your generosity in support of men and women who actually need help on that field. Your special commitment to getting the message all over had been certainly valuable and have consistently encouraged girls just like me to arrive at their ambitions. This valuable useful information indicates much a person like me and extremely more to my mates. Regards; from each one of us.
I intended to create you this very small observation to say thank you once again for all the magnificent knowledge you’ve provided on this page. It is quite unbelievably open-handed of you to grant unreservedly what a lot of folks could possibly have advertised as an ebook in making some dough for their own end, particularly given that you might have tried it in case you desired. Those guidelines likewise worked to become good way to realize that other individuals have a similar dreams just like mine to know the truth great deal more in respect of this condition. I’m sure there are numerous more pleasant opportunities ahead for those who find out your site.
I needed to compose you that little bit of note to be able to give many thanks over again just for the incredible ideas you have featured here. This has been extremely open-handed with people like you to offer openly precisely what a few people would have sold for an ebook in making some profit on their own, primarily considering that you could possibly have done it if you decided. These inspiring ideas likewise served to become good way to be sure that most people have similar dreams like my very own to learn good deal more with regard to this issue. I’m sure there are several more fun opportunities up front for those who scan through your blog.
Meds information leaflet. What side effects?
motrin buy
Actual what you want to know about medication. Read information now.
I went over this site and I believe you have a lot of great information, saved to favorites (:.
I have been browsing online more than 3 hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. Personally, if all website owners and bloggers made good content as you did, the web will be a lot more useful than ever before.
Medicament information. What side effects? In the USA
cost doxycycline
Some information about pills. Get information now.
At this time I am going to do my breakfast, after having my breakfast
coming yet again to read other news.
No matter if some one searches for his essential thing, therefore he/she desires to be available that in detail,
therefore that thing is maintained over here.
Simply a smiling visitor here to share the love (:, btw great design .
i so appreciate
Way cool! Some very valid points! I appreciate you penning this article plus the rest of the site is extremely good.
This blog was… how do I say it? Relevant!! Finally I’ve found something that helped me.
Thanks!