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;
this.id = id;
this.format = new MessageFormat( format );
}
@Override
public String getId() {
return id;
}
@Override
public MessageFormat getFormat() {
return format;
}
@Override
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() );
}
Thanks!