devxlogo

Should Log Instances Be Static or Not?

Should Log Instances Be Static or Not?

Private static final Log log = LogFactory.getLog(MyClass.class);

It is a good idea to store the log instance in a static final variable, because:

  • No problems with serializable classes
  • Initialization cost only once: getLog() may not be as cheap as you might think
  • Automatically thread-safe, only if the final keyword is included
  • Usable from static and non-static code
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist