Defining Logger as static and non-static variable

Hi,

Sometimes in project i see a static variable defined for logger

private final static Logger LOGGER = Logger.getLogger(Something.class);

in some cases i saw static is not being used and defined as

private final Logger LOGGER = Logger.getLogger(Something.class);

what is the difference between the two? Does it makes any difference in cluster and in non-cluster environment?

Thanks.
Software ProgrammerAsked:
Who is Participating?
 
CEHJCommented:
though that might depend on the framework being used
In Log4J and Logback calls are synchronized at the appending method, so you don't need to worry about issues of concurrency
0
 
CEHJCommented:
It shouldn't make any notable difference. The static logger is used often simply to allow logging in the context of static methods
0
 
Software ProgrammerAuthor Commented:
If static used will it create issue in multi thread environment...
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
CEHJCommented:
It's conceivable, though that might depend on the framework being used. If you're doing server-side logging, it's academic really, since you wouldn't use a static logger
0
 
Software ProgrammerAuthor Commented:
It is being used in the server side and saw in many projects which works in cluster environment and in a multi-threaded environment as well. So not sure how they are handling it. Do you think using static logger will cause an issue? if yes, how to use it and get rid of the multi-thread issue.
0
 
Andrea BalassoFull Stack Java DeveloperCommented:
The static logger variable generate only one instance in the application context, instead the second logger declaration generate a new logger instance every time you create a new instance class. This means that if you have a class A and generate 1000 instance, in the first case you will have only one logger object, instead in the second case you will have 1000 instance logger object variable, one for every A instance class.
Using the static declaration you can optimize the memory resource use and this is one reason because I ususally use the static declaration for the logger.
0
 
Software ProgrammerAuthor Commented:
Nice answers by both CEHJ and Andrea and fine with them.
0
 
Software ProgrammerAuthor Commented:
CHEJ your explanations are great..go through my remaining questions if you had time..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.