[slf4j-user] Use same message in log and Exception

Jacob Kjome hoju at visi.com
Mon Jul 5 07:40:32 CEST 2010


Maybe you have a unique situation, but the general rule is that you log where you
handle the exception, not where you throw it.  Otherwise you may end up with logs
containing duplicate messages.

Personally, I'd just do...

throw new SomeException(String.format("Asdf asdf asdf qwer=%s asdf zxcv=%s", obj1,
obj2));

Then log the exception where you handle it.

Jake

On 7/3/2010 9:18 PM, Jeff Jensen wrote:
> Is there a better way to make one String to log it and use as exception
> message than this?
> 
>         final String msg =
>                 "Asdf asdf asdf qwer={} asdf zxcv={}";
>         LOG.error(msg, obj1, obj2);
>         final FormattingTuple logMsg =
>                 MessageFormatter.format(msg, obj1, obj2);
>         throw new SomeException(logMsg.getMessage());
> 
> It would be useful if the log messages (e.g. error, debug, etc.) returned
> the String logged message.  The code above creates the message twice.
> 
> 
> _______________________________________________
> slf4j-user mailing list
> slf4j-user at qos.ch
> http://qos.ch/mailman/listinfo/slf4j-user
> 
> 
> 


More information about the slf4j-user mailing list