Explain Codes LogoExplain Codes Logo

Should a "static final Logger" be declared in UPPER-CASE?

java
naming-convention
best-practices
logging
Alex KataevbyAlex Kataev·Aug 21, 2024
TLDR

For Java static final Logger instances, best practice leans towards using lowercase logger, not UPPER_CASE LOGGER. This distinction makes it clear that Logger is not a true constant, but a mutable tool used across your application.

//... because loggers are cool, and cool stuff needs lowercase. private static final Logger logger = LoggerFactory.getLogger(MyClass.class);

So, let's keep it logger for readability and consistency with broader development practices.

Uppercase or Lowercase? That is the question.

In Java, uppercase labels are used for immutable constants. These are static final fields with a constant, unchanging value known at compile-time.

//As constant and immovable as a mountain... or a mountain goat. public static final String CONSTANT_NAME = "ImmutableValue";

But a logger instance is mutable. It changes state during the application lifecycle, which is a stark contrast to our unchanging, mountain-like constant.

Understanding mutable vs. immutable

Static final fields can be split into two families: the immutable objects and the mutable folks.

  • Immutable family: Members like String and primitives live here. They enjoy UPPER_CASE as they never change.
//Max connections is fixed, like the number of slices in a pizza. You wish there were more, but there aren't. public static final int MAX_CONNECTIONS = 50;
  • Mutable folks: Logger instances belong here. Despite being popular, they maintain a low profile with lowercase.
//... because we like our loggers like we like our coffee - strong, bold, and down-to-earth. private static final Logger logger = LoggerFactory.getLogger(MyClass.class);

Tools and standards adherence

Automated code tools like PMD or Checkstyle endorse naming conventions that improve readability and maintainability. They aren't fond of uppercase for mutable static final fields -- they think it's a big no-no.

Consider your logging framework

Different logging frameworks might go against the grain. SLF4J can be flexible (LOGGER or logger), whereas Log4J or Java's Util Logging really prefer to see logger.

// SLF4J style // LOGGER is like that one friend who sometimes prefers their full name, rather than their nick private static final Logger LOGGER = LoggerFactory.getLogger(MyClass.class); // Log4J/Common logging style // Logger is more laid-back; they're cool with just their first name private static final Logger logger = Logger.getLogger(MyClass.class);

Balancing your framework convention with practices for mutable and immutable static final fields will yield a beautiful, readable code your team will treasure.

Continuity across your project is a lighthouse in a storm. Stick with LOGGER if that's the norm around you, or logger if that floats your boat. Uniform code is a treat for new contributors or late-night bug fixes.

When you swim into a legacy codebase's waters, where LOGGER and logger fight for dominance, ensure you document your choice and guide future coders.

Finding style guide treasure maps

Style guides are a goldmine of wisdom curated by experienced developers. They show the lands of best practices and code clarity.

Visualizing "Static Final Logger Naming Convention":

Think of your codebase as a toolbox:

🧰 Code Toolbox - 🔨 Hammer (Common Tool) - 🪛 Screwdriver (Common Tool) - static_final_logger (Specialist Tool)

Naming Convention Insight:

- 🧰 Common tools (variables): `lowerCamelCase` - 🔧 Specialist tool (static final Logger): `LOGGER` or `logger`

The Logger is a purpose-specific tool. Traditional Java Naming:

🔧 UPPERCASE: When Immutable & Universal 🪓 lowercase: When mutable & frequent-use

Pick readability and standardization over inflexible dogma. 📘👁️