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 org.apache.logging.log4j.status.StatusLogger; 020 021/** 022 * Creates {@link FormattedMessage} instances for {@link MessageFactory2} methods (and {@link MessageFactory} by 023 * extension.) 024 * <p> 025 * Creates {@link SimpleMessage} objects that do not retain a reference to the parameter object. 026 * </p> 027 * <p> 028 * Intended for use by the {@link StatusLogger}: this logger retains a queue of recently logged messages in memory, 029 * causing memory leaks in web applications. (LOG4J2-1176) 030 * </p> 031 * <p> 032 * This class is immutable. 033 * </p> 034 * <h4>Note to implementors</h4> 035 * <p> 036 * This class does <em>not</em> implement any {@link MessageFactory2} methods and lets the superclass funnel those calls 037 * through {@link #newMessage(String, Object...)}. 038 * </p> 039 */ 040public final class ParameterizedNoReferenceMessageFactory extends AbstractMessageFactory { 041 private static final long serialVersionUID = 5027639245636870500L; 042 043 /** 044 * Message implementation that only keeps a reference to the error text and the error (if any), not to the 045 * message parameters, in order to avoid memory leaks. This addresses LOG4J2-1368. 046 * @since 2.6 047 */ 048 static class StatusMessage implements Message { 049 private final String formattedMessage; 050 private final Throwable throwable; 051 052 public StatusMessage(final String formattedMessage, final Throwable throwable) { 053 this.formattedMessage = formattedMessage; 054 this.throwable = throwable; 055 } 056 057 @Override 058 public String getFormattedMessage() { 059 return formattedMessage; 060 } 061 062 @Override 063 public String getFormat() { 064 return formattedMessage; 065 } 066 067 @Override 068 public Object[] getParameters() { 069 return null; 070 } 071 072 @Override 073 public Throwable getThrowable() { 074 return throwable; 075 } 076 } 077 078 /** 079 * Constructs a message factory with default flow strings. 080 */ 081 public ParameterizedNoReferenceMessageFactory() { 082 super(); 083 } 084 085 /** 086 * Instance of ParameterizedStatusMessageFactory. 087 */ 088 public static final ParameterizedNoReferenceMessageFactory INSTANCE = new ParameterizedNoReferenceMessageFactory(); 089 090 /** 091 * Creates {@link SimpleMessage} instances containing the formatted parameterized message string. 092 * 093 * @param message The message pattern. 094 * @param params The message parameters. 095 * @return The Message. 096 * 097 * @see MessageFactory#newMessage(String, Object...) 098 */ 099 @Override 100 public Message newMessage(final String message, final Object... params) { 101 if (params == null) { 102 return new SimpleMessage(message); 103 } 104 final ParameterizedMessage msg = new ParameterizedMessage(message, params); 105 return new StatusMessage(msg.getFormattedMessage(), msg.getThrowable()); 106 } 107}