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 1111545
In Process

The Archive Base Latest Questions

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

Should logger be declared static or not? Usually I’ve seen two types of declaration

  • 0

Should logger be declared static or not? Usually I’ve seen two types of declaration for a logger :

    protected Log log = new Log4JLogger(aClass.class);

or

    private static Log log = new Log4JLogger(aClass.class);

Which one should be used? what are the pro’s and con’s of both?

  • 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-17T02:33:52+00:00Added an answer on May 17, 2026 at 2:33 am

    The advantage of the non-static form is that you can declare it in an (abstract) base class like follows without worrying that the right classname will be used:

    protected Log log = new Log4JLogger(getClass());
    

    However its disadvantage is obviously that a whole new logger instance will be created for every instance of the class. This may not per se be expensive, but it adds a significant overhead. If you’d like to avoid this, you’d like to use the static form instead. But its disadvantage is in turn that you have to declare it in every individual class and take care in every class that the right classname is been used during logger’s construction because getClass() cannot be used in static context. However, in the average IDE you can create an autocomplete template for this. E.g. logger + ctrl+space.

    On the other hand, if you obtain the logger by a factory which in turn may cache the already-instantiated loggers, then using the non-static form won’t add that much overhead. Log4j for example has a LogManager for this purpose.

    protected Log log = LogManager.getLogger(getClass());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Should I be writing assertTrue(User logged in, user.isLoggedIn()); or assertTrue(User is not logged in,
When you log out of a web app, should ALL your session be logged
Should timestamps always use UTC (as in 2012-06-14T10:32:11+00:00 ) and not local time (as
I do not know why my log file is not created . Here is
Something in Rails (ActiveRecord::Base.logger ?) puts all executed SQL into log/development.log. I have a
I am having following variables in my powershell script $Buildlog=FormalBuild.log $Logger=/flp1:Append;LogFile=$Buildlog;Verbosity=Diag; #Logger will use
Or should I just stick with Python2.5 for a bit longer?
EDIT: My main code no longer works, should this function work? <script type=text/javascript src=jquery-1.7.2.js></script>
So, I've been working on this for far longer than I should be and
Background: working a user q&a site where users logged,etc. What user/visitor data should be

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.