Hi Ćukasz,
On Sat, 11 Mar 2023 at 10:36, Lukasz Lenart <[email protected]> wrote:
> Is it possible to unpack Optional parameters?
Yes and no: the default message factories [1] and [2] do treat
`Optional` parameter as any other `Object` parameter.
In your case:
LOG.info("Detected container provider [{}] needs to be reloaded.", provider);
creates a `ParameterizeMessage` with a parameter of `provider`.
`provider` will be serialized as `provider.toString()`, not
`provider.get().toString()`.
You can write your own `MessageFactory` that calls `Optional.get()` if
the parameter is of type `Optional`. To replace the default message
factory with your custom version, set the "log4j2.messageFactory" to
the fully-qualified class name of the factory.
However I don't believe that this is the right way to go: IIRC
`Optional.isPresent()` and `Optional.get()` should be replaced with a
single call to `Optional.ifPresent()` whenever possible:
provider.ifPresent(p -> {
LOG.info("Detected container provider [{}] needs to be reloaded.", p);
});
[1]
https://logging.apache.org/log4j/2.x/log4j-api/apidocs/org/apache/logging/log4j/message/ParameterizedMessageFactory.html
[2]
https://logging.apache.org/log4j/2.x/log4j-api/apidocs/org/apache/logging/log4j/message/ReusableMessageFactory.html