0

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).

nimo23
  • 5,170
  • 10
  • 46
  • 75
  • 1
    your framework, your thread: https://logging.apache.org/log4j/2.x/manual/messages.html – xerx593 Feb 25 '21 at 12:12
  • Where does jboss-logging come into play? What logging framework are you using? – James R. Perkins Feb 26 '21 at 02:20
  • @JamesR.Perkins After trying different options, I came to the conclustion that only jboss logger has a nice addition which is called `@MessageLogger`. That`s exactly what I was looking for. I don`t know if other logging frameworks does this have as well. The only downside is to let the file be preprocessed by the annotation preprocessor. – nimo23 Feb 26 '21 at 08:14
  • @JamesR.Perkins are there better alternatives you know? – nimo23 Feb 26 '21 at 08:16
  • One little problem I had was to include the `jboss-logging-processor` **manually** within the FactoryPath (for annotation processing) even if the lib was already included by maven pom.xml with `compile`. The factory path included all other libs from my classpath automatically (for example, `jboss-logging-annotations` and `jboss-logging`), but the `jboss-logging-processor` was not included. Any reasons for that? – nimo23 Feb 26 '21 at 08:21
  • 1
    There shouldn’t be a need, assuming you’re using a newer version of Maven, then you shouldn’t need to add them to anything but the dependency tree. They can be `provided` – James R. Perkins Feb 26 '21 at 17:13
  • yes, it works without the manual setup. I only needed to adjust my build path and now it works. – nimo23 Feb 26 '21 at 17:20

1 Answers1

-1

The best way to achieve that what I wanted is to use Jboss Logging with its @MessageLogger. That's great.

After trying different options, I came to the conclustion that only jboss logger has a nice addition which is called @MessageLogger. Thats exactly what I was looking for. I dont know if other logging frameworks does this have as well. The only downside is to let the file be preprocessed by the annotation preprocessor.

I guess, another downside of jboss-logging is not have "Garbage-free logging" (in compare to Log4j2).

nimo23
  • 5,170
  • 10
  • 46
  • 75