Annotation Type AsynchronouslyFormattable

@Documented @Target(TYPE) @Retention(RUNTIME) public @interface AsynchronouslyFormattable
Annotation that signals to asynchronous logging components that messages of this type can safely be passed to a background thread without calling Message.getFormattedMessage() first.

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:

  1. If the Message implements ReusableMessage, asynchronous logging components in the Log4j implementation will copy the message content (formatted message, parameters) onto the queue rather than passing the Message instance itself. This ensures that the formatted message will not change when the mutable object is modified.
  2. If the Message is annotated with AsynchronouslyFormattable, it can be passed to another thread as is.
  3. Otherwise, asynchronous logging components in the Log4j implementation will call 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.)
See Also: