I want to share a "string-message" to a logger and to business code, so I created the message with a String.format
and put this into the logger and my business code:
var msg = Messages.dateNotValid(user, getClass());
log.error(msg);
user.addMsg(msg);
The Messages
stores all my messages into one class:
class Messages {
public static dateNotValid(User user, Class<?> clazz){
return String.format("[%s] the date %s is not valid", clazz.getSimpleName(), user.getDate());
}
}
Normally, I would add the message to the logger in this way:
// will not be parsed if log.error is disabled
log.error("the date {0} is not valid", user.date);
Is there a nicer approach for what I want to do? I am a little unsure about my code. Maybe there is some best practice I am not aware of (or frameworks/apis/extension points which helps in what I want to achieve).