Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6863961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:51:07+00:00 2026-05-27T02:51:07+00:00

When instantiating Logger in a Spring Controller, is there any reason to declare it

  • 0

When instantiating Logger in a Spring Controller, is there any reason to declare it as a static final? The Logger is not used outside MyController.class. I’ve seen both examples in use, but can’t see why I should use one or the other.

private Logger logger = LoggerFactory.getLogger(MyController.class);

vs

private static final Logger logger = LoggerFactory.getLogger(MyController.class);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:51:07+00:00Added an answer on May 27, 2026 at 2:51 am

    Personally, I use

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    

    The main advantage of this is I can cut and paste this into new classes without having to change the name of the class.

    As for whether or not they should be static, please see Should Logger members of a class be declared as static?, from the slf4j website, which says:

    We used to recommend that loggers members be declared as instance variables instead of static. After further analysis, we no longer recommend one approach over the other.

    Taken from that page:

    Advantages for declaring loggers as static

    1. common and well-established idiom
    2. less CPU overhead: loggers are retrieved and assigned only once, at hosting class initialization
    3. less memory overhead: logger declaration will consume one reference per class

    Disadvantages for declaring loggers as static

    1. For libraries shared between applications, not possible to take advantage of repository selectors. It should be noted that if the SLF4J binding and the underlying API ships with each application (not shared between applications), then each application will still have its own logging environment.
    2. not IOC-friendly

    Advantages for declaring loggers as instance variables

    1. Possible to take advantage of repository selectors even for libraries shared between applications. However, repository selectors only work if the underlying logging system is logback-classic. Repository selectors do not work for the SLF4J+log4j combination.
    2. IOC-friendly

    Disadvantages for declaring loggers as instance variables

    1. Less common idiom than declaring loggers as static variables
    2. higher CPU overhead: loggers are retrieved and assigned for each instance of the hosting class
    3. higher memory overhead: logger declaration will consume one reference per instance of the hosting class

    Explanation

    Static logger members cost a single variable reference for all instances of the class whereas an instance logger member will cost a variable reference for every instance of the class. For simple classes instantiated thousands of times there might be a noticeable difference.

    However, more recent logging systems, e.g log4j or logback, support a distinct logger context for each application running in the application server. Thus, even if a single copy of log4j.jar or logback-classic.jar is deployed in the server, the logging system will be able to differentiate between applications and offer a distinct logging environment for each application.

    More specifically, each time a logger is retrieved by invoking LoggerFactory.getLogger() method, the underlying logging system will return an instance appropriate for the current application. Please note that within the same application retrieving a logger by a given name will always return the same logger. For a given name, a different logger will be returned only for different applications.

    If the logger is static, then it will only be retrieved once when the hosting class is loaded into memory. If the hosting class is used in only in one application, there is not much to be concerned about. However, if the hosting class is shared between several applications, then all instances of the shared class will log into the context of the application which happened to first load the shared class into memory – hardly the behavior expected by the user.

    For more information, see that page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When Instantiating a class, Windsor by default treats all public properties of the class
I am instantiating a class A (which I am importing from somebody else, so
We're seeing a crash when instantiating an instance of the System.Xml.Serialization.XmlSerializer class in a
Is it possible to declare a variable in c++ without instantiating it? I want
An idiom commonly used in OO languages like Python and Ruby is instantiating an
I am trying to do something very simple. I have a com.mypackage.Logger logger class
Possible Duplicate: Instantiating a generic class in Java I am studying Generics Java because
I am instantiating a class like this. myObj = (myObj) Class.forName(fully qualified class name
When I am instantiating a particular class I have written, I give it a
I've been instantiating subclasses in javascript using object = new class () but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.