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.core.async; 018 019import org.apache.logging.log4j.core.util.Constants; 020import org.apache.logging.log4j.message.AsynchronouslyFormattable; 021import org.apache.logging.log4j.message.Message; 022 023/** 024 * Helper class providing some async logging-related functionality. 025 * <p> 026 * Consider this class private. 027 * </p> 028 */ 029public class InternalAsyncUtil { 030 /** 031 * Returns the specified message, with its content frozen unless system property 032 * {@code log4j.format.msg.async} is true or the message class is annotated with 033 * {@link AsynchronouslyFormattable}. 034 * 035 * @param msg the message object to inspect, modify and return 036 * @return Returns the specified message, with its content frozen 037 */ 038 public static Message makeMessageImmutable(final Message msg) { 039 // if the Message instance is reused, there is no point in freezing its message here 040 if (msg != null && !canFormatMessageInBackground(msg)) { 041 msg.getFormattedMessage(); // LOG4J2-763: ask message to makeMessageImmutable parameters 042 } 043 return msg; 044 } 045 046 private static boolean canFormatMessageInBackground(final Message message) { 047 return Constants.FORMAT_MESSAGES_IN_BACKGROUND // LOG4J2-898: user wants to format all msgs in background 048 || message.getClass().isAnnotationPresent(AsynchronouslyFormattable.class); // LOG4J2-1718 049 } 050}