Why it is good practice to declare loggers private, static, and final?

Source: Internet
Author: User

// Jakarta commons Logging
Private Static final log = logfactory. getlog (myclass. Class );
The above Code also shows another good practice, which is to pass The Class Object To the getlog () method, instead of a string.
Why the java. util. Logging. Logger class doesn't even provide a method accepting a class object is simply beyond me.
Why did the people who developed the java. util. Logging Package base their API on log4j yet omit some of the most useful parts of it?
Oh well. Now to the point.
Why it is good practice to declare loggers private, static, and final?
A logger is an internal implementation detail, so it shoshould be private.
You only need one logger for all instances of a class, hence static.
And a logger shoshould not be able to be replaced, thus final.
So if this is good, what's not so good (at least in my opinion )?
Simple-any logger that is not private, static, final, and which doesn't pass in a class object to getlog ()!
For example, consider this common bit of code, declared in some base class:

// Not so good logger Declaration
Protected final log = logfactory. getlog (getclass ());
Why is this bad? Well, It isn' t static for one thing.
For another, it uses getclass () to obtain the log.
At first this seems efficient since now all subclasses automatically inherit a ready-made log of the correct runtime type.
So what's the issue here?
The biggest problem with loggers declared in this manner is that you now get all the logging from the superclass mixed in with the logging from the subclass,
And it is impossible in the log output to discern which messages came from which class unless you look at the source.
This is really annoying if the superclass has a lot of logging that you don't want to see, since you cannot filter it out.
Another problem is that your ability to set log levels differently goes away,
For example if a subclass resides in a different package than the superclass.
In that case, if you try to filter out logging from the superclass, you can't because the actual runtime class was used to obtain the logger.
Last, having a protected logger just seems to violate basic object-oriented principles.
Why in the world shocould subclasses know about an internal implementation detail from a superclass that is a cross-cutting concern, no less?
Anyway, though this is a silly little rant it really is annoying when you extend a superclass that declares a protected logger like this.


Class <? Extends valueofnull> JAVA. Lang. Object. getclass ()

Returns the runtime class of this object.
The returned class object is the object that is locked by static synchronized methods of the represented class.
The actual result type is class <? Extends | x |> where | x | is the erasure of the static type of the expression on which getclass is called.
For example, no cast is required in this code fragment:

Number n = 0;
Class <? Extends number> C = n. getclass ();

The class object that represents the runtime class of this object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.