Java enums for logging

I'm new to Java, I'm testing some code for an existing project. I was wondering why they chose to use enums for their log messages. Any hint? Is it a good approach?

This is the class containing the log messages:

import com.myproject.logging.LogEntryDefinition;

import java.text.MessageFormat;
import java.util.logging.Level;

public enum LogEntries implements LogEntryDefinition {

    MsgProcessingError( Level.SEVERE, "RPS0001", "Exception during message processing: {0}. Message consumed: {1}" ),
    MessageConsumedFromPcuQueue( Level.INFO, "RPS0002", "Message extracted from queue: {0}" ),
    ResourceRetrievedFromParis( Level.INFO, "RPS0003", "Resource retrieved from db: {0}" ),
    IllegalMessageConsumed( Level.SEVERE, "RPS0004", "Illegal update message consumed by Agent. Message: {0}" ),

    private final String id;
    private final MessageFormat format;
    private final Level level;

    private LogEntries( final Level level, final String id, final String format ) {
        this.level = level; = id;
        this.format = new MessageFormat( format );

    public String getId() {
        return id;

    public MessageFormat getFormat() {
        return format;

    public Level getLevel() {
        return level;

This is the interface:

package com.myproject.logging;

import java.text.MessageFormat;
import java.util.logging.Level;

public interface LogEntryDefinition {

    String getId();

    MessageFormat getFormat();

    Level getLevel();

And this an example of how it's used:


}catch(Exception e){
    LOGGER.log( LogEntries.MsgProcessingError, e.getMessage(), t.toString() );



You suggested they should use a Singleton in the comments. Enums are an easier way to implement singletons.

Enum singletons ensure thread-safe initialization without the need for double-checked locking. It's an easy alternative to the Initialize On-Demand idiom, which although ensures thread-safe initialization without synchronization overhead, it's quite verbose and requires you to create a helper class.

Enum singletons also protect against reflection attacks. Only one instance of a singleton should exist (due to the nature of the design). Using a class-based singleton, reflection could be used to change the accessability of the constructor from private to public, allowing multiple instances. An enum's constructor is implicitly private, and it's accessability cannot be modified via reflection.

As for your comment of comparing enums vs comparing ints, that is considered micro-optimization. You should focus more on maintainability than trying to save a few nanoseconds.

Hope this answers your question! :)

I don't think use Enum for message logging is the best way.I think Properties is better than Enum in this case.

Need Your Help

Float Phonegap Activity in Android

android cordova

I need to create a phonegap activity which only uses part of the screen for Android platform.

How do you check whether a CSV file is corrupted in PHP and/or Javascript?

javascript php jquery mysql csv

I'm making an HTML5/jQuery/PHP app which involves uploading CSV files (via a drag and drop), and processing them to a MySQL database. So far, I can upload the files, and I know how to read them int...