001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.message;
018
019import java.lang.annotation.Documented;
020import java.lang.annotation.ElementType;
021import java.lang.annotation.Retention;
022import java.lang.annotation.RetentionPolicy;
023import java.lang.annotation.Target;
024
025/**
026 * Annotation that signals to asynchronous logging components that messages of this type can safely be passed to
027 * a background thread without calling {@link Message#getFormattedMessage()} first.
028 * <p>
029 * Generally, logging mutable objects asynchronously always has the risk that the object is modified between the time
030 * the logger is called and the time the log message is formatted and written to disk. Strictly speaking it is the
031 * responsibility of the application to ensure that mutable objects are not modified after they have been logged,
032 * but this is not always possible.
033 * </p><p>
034 * Log4j prevents the above race condition as follows:
035 * </p><ol>
036 * <li>If the Message implements {@link ReusableMessage}, asynchronous logging components in the Log4j implementation
037 * will copy the message content (formatted message, parameters) onto the queue rather than passing the
038 * {@code Message} instance itself. This ensures that the formatted message will not change
039 * when the mutable object is modified.
040 * </li>
041 * <li>If the Message is annotated with {@link AsynchronouslyFormattable}, it can be passed to another thread as is.</li>
042 * <li>Otherwise, asynchronous logging components in the Log4j implementation will call
043 * {@link Message#getFormattedMessage()} before passing the Message object to another thread.
044 * This gives the Message implementation class a chance to create a formatted message String with the current value
045 * of the mutable object. The intention is that the Message implementation caches this formatted message and returns
046 * it on subsequent calls.
047 * (See <a href="https://issues.apache.org/jira/browse/LOG4J2-763">LOG4J2-763</a>.)
048 * </li>
049 * </ol>
050 *
051 * @see Message
052 * @see ReusableMessage
053 * @see <a href="https://issues.apache.org/jira/browse/LOG4J2-763">LOG4J2-763</a>
054 * @since 2.8
055 */
056@Documented // This annotation is part of the public API of annotated elements.
057@Target(ElementType.TYPE) // Only applies to types.
058@Retention(RetentionPolicy.RUNTIME) //Needs to be reflectively discoverable runtime.
059public @interface AsynchronouslyFormattable {
060}