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 static final long serialVersionUID = 4199272162767841280L; 050 private final String formattedMessage; 051 private final Throwable throwable; 052 053 public StatusMessage(final String formattedMessage, final Throwable throwable) { 054 this.formattedMessage = formattedMessage; 055 this.throwable = throwable; 056 } 057 058 @Override 059 public String getFormattedMessage() { 060 return formattedMessage; 061 } 062 063 @Override 064 public String getFormat() { 065 return formattedMessage; 066 } 067 068 @Override 069 public Object[] getParameters() { 070 return null; 071 } 072 073 @Override 074 public Throwable getThrowable() { 075 return throwable; 076 } 077 } 078 079 /** 080 * Constructs a message factory with default flow strings. 081 */ 082 public ParameterizedNoReferenceMessageFactory() { 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}