Description
Internal log4j logging like below will just log the message but will not report the specified exception:
try { someProcess(); } catch (Exception exception) { StatusLogger.getLogger().warn("error occurred during {}", "some process", exception); }
This is caused by a bug in ParameterizedNoReferenceMessageFactory (the factory used by StatusLogger):
final String formatted = new ParameterizedMessage(message, params).getFormattedMessage(); return new SimpleMessage(formatted); // exception is dropped
This factory was introduced to resolve memory leaks that appeared in web containers because StatusLogger keeps a ring buffer of previously logged messages and ParameterizedMessage keeps a reference to its parameter objects (LOG4J2-1176).
AbstractLogger depends on the Message to get the Throwable when logging a message with parameters, so SimpleMessage is not good enough. I'm thinking to introduce a new class StatusMessage (perhaps as an inner class of ParameterizedNoReferenceMessageFactory in order to keep the API surface small). StatusMessage would have the formatted string and the Throwable (which may be null). That should solve this issue.
If the Throwable is a custom Log4j class like AppenderLoggingException then keeping a reference to it may re-introduce memory leaks again though, but I guess that is a fight for another day...
Attachments
Issue Links
- is duplicated by
-
LOG4J2-1392 PluginBuilder.build() looses reason for error
- Closed