@Documented @Target(value=TYPE) @Retention(value=RUNTIME) public @interface AsynchronouslyFormattable
Generally, logging mutable objects asynchronously always has the risk that the object is modified between the time the logger is called and the time the log message is formatted and written to disk. Strictly speaking it is the responsibility of the application to ensure that mutable objects are not modified after they have been logged, but this is not always possible.
Log4j prevents the above race condition as follows:
ReusableMessage, asynchronous logging components in the Log4j implementation will copy the message content (formatted message, parameters) onto the queue rather than passing the
Messageinstance itself. This ensures that the formatted message will not change when the mutable object is modified.
AsynchronouslyFormattable, it can be passed to another thread as is.
Message.getFormattedMessage()before passing the Message object to another thread. This gives the Message implementation class a chance to create a formatted message String with the current value of the mutable object. The intention is that the Message implementation caches this formatted message and returns it on subsequent calls. (See LOG4J2-763.)
Copyright © 1999-2023 The Apache Software Foundation. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.