1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
16 */
17 package org.apache.logging.log4j.message;
18
19 import java.lang.annotation.Documented;
20 import java.lang.annotation.ElementType;
21 import java.lang.annotation.Retention;
22 import java.lang.annotation.RetentionPolicy;
23 import java.lang.annotation.Target;
24
25 /**
26 * Annotation that signals to asynchronous logging components that messages of this type can safely be passed to
27 * a background thread without calling {@link Message#getFormattedMessage()} first.
28 * <p>
29 * Generally, logging mutable objects asynchronously always has the risk that the object is modified between the time
30 * the logger is called and the time the log message is formatted and written to disk. Strictly speaking it is the
31 * responsibility of the application to ensure that mutable objects are not modified after they have been logged,
32 * but this is not always possible.
33 * </p><p>
34 * Log4j prevents the above race condition as follows:
35 * </p><ol>
36 * <li>If the Message implements {@link ReusableMessage}, asynchronous logging components in the Log4j implementation
37 * will copy the message content (formatted message, parameters) onto the queue rather than passing the
38 * {@code Message} instance itself. This ensures that the formatted message will not change
39 * when the mutable object is modified.
40 * </li>
41 * <li>If the Message is annotated with {@link AsynchronouslyFormattable}, it can be passed to another thread as is.</li>
42 * <li>Otherwise, asynchronous logging components in the Log4j implementation will call
43 * {@link Message#getFormattedMessage()} before passing the Message object to another thread.
44 * This gives the Message implementation class a chance to create a formatted message String with the current value
45 * of the mutable object. The intention is that the Message implementation caches this formatted message and returns
46 * it on subsequent calls.
47 * (See <a href="https://issues.apache.org/jira/browse/LOG4J2-763">LOG4J2-763</a>.)
48 * </li>
49 * </ol>
50 *
51 * @see Message
52 * @see ReusableMessage
53 * @see <a href="https://issues.apache.org/jira/browse/LOG4J2-763">LOG4J2-763</a>
54 * @since 2.8
55 */
56 @Documented // This annotation is part of the public API of annotated elements.
57 @Target(ElementType.TYPE) // Only applies to types.
58 @Retention(RetentionPolicy.RUNTIME) //Needs to be reflectively discoverable runtime.
59 public @interface AsynchronouslyFormattable {
60 }