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.appender; 018 019import java.io.Serializable; 020import java.util.HashMap; 021import java.util.Map; 022import java.util.zip.Deflater; 023 024import org.apache.logging.log4j.core.Filter; 025import org.apache.logging.log4j.core.Layout; 026import org.apache.logging.log4j.core.LogEvent; 027import org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy; 028import org.apache.logging.log4j.core.appender.rolling.RollingFileManager; 029import org.apache.logging.log4j.core.appender.rolling.RolloverStrategy; 030import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy; 031import org.apache.logging.log4j.core.config.Configuration; 032import org.apache.logging.log4j.core.config.plugins.Plugin; 033import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 034import org.apache.logging.log4j.core.config.plugins.PluginConfiguration; 035import org.apache.logging.log4j.core.config.plugins.PluginElement; 036import org.apache.logging.log4j.core.config.plugins.PluginFactory; 037import org.apache.logging.log4j.core.layout.PatternLayout; 038import org.apache.logging.log4j.core.net.Advertiser; 039import org.apache.logging.log4j.core.util.Booleans; 040import org.apache.logging.log4j.core.util.Integers; 041 042/** 043 * An appender that writes to files and can roll over at intervals. 044 */ 045@Plugin(name = "RollingFile", category = "Core", elementType = "appender", printObject = true) 046public final class RollingFileAppender extends AbstractOutputStreamAppender<RollingFileManager> { 047 048 private static final int DEFAULT_BUFFER_SIZE = 8192; 049 050 private final String fileName; 051 private final String filePattern; 052 private Object advertisement; 053 private final Advertiser advertiser; 054 055 private RollingFileAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter, 056 final RollingFileManager manager, final String fileName, final String filePattern, 057 final boolean ignoreExceptions, final boolean immediateFlush, final Advertiser advertiser) { 058 super(name, layout, filter, ignoreExceptions, immediateFlush, manager); 059 if (advertiser != null) { 060 final Map<String, String> configuration = new HashMap<>(layout.getContentFormat()); 061 configuration.put("contentType", layout.getContentType()); 062 configuration.put("name", name); 063 advertisement = advertiser.advertise(configuration); 064 } 065 this.fileName = fileName; 066 this.filePattern = filePattern; 067 this.advertiser = advertiser; 068 } 069 070 @Override 071 public void stop() { 072 super.stop(); 073 if (advertiser != null) { 074 advertiser.unadvertise(advertisement); 075 } 076 } 077 078 /** 079 * Write the log entry rolling over the file when required. 080 081 * @param event The LogEvent. 082 */ 083 @Override 084 public void append(final LogEvent event) { 085 getManager().checkRollover(event); 086 super.append(event); 087 } 088 089 /** 090 * Returns the File name for the Appender. 091 * @return The file name. 092 */ 093 public String getFileName() { 094 return fileName; 095 } 096 097 /** 098 * Returns the file pattern used when rolling over. 099 * @return The file pattern. 100 */ 101 public String getFilePattern() { 102 return filePattern; 103 } 104 105 /** 106 * Returns the triggering policy. 107 * @param <T> TriggeringPolicy type 108 * @return The TriggeringPolicy 109 */ 110 public <T extends TriggeringPolicy> T getTriggeringPolicy() { 111 return getManager().getTriggeringPolicy(); 112 } 113 114 /** 115 * Create a RollingFileAppender. 116 * @param fileName The name of the file that is actively written to. (required). 117 * @param filePattern The pattern of the file name to use on rollover. (required). 118 * @param append If true, events are appended to the file. If false, the file 119 * is overwritten when opened. Defaults to "true" 120 * @param name The name of the Appender (required). 121 * @param bufferedIO When true, I/O will be buffered. Defaults to "true". 122 * @param bufferSizeStr buffer size for buffered IO (default is 8192). 123 * @param immediateFlush When true, events are immediately flushed. Defaults to "true". 124 * @param policy The triggering policy. (required). 125 * @param strategy The rollover strategy. Defaults to DefaultRolloverStrategy. 126 * @param layout The layout to use (defaults to the default PatternLayout). 127 * @param filter The Filter or null. 128 * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise 129 * they are propagated to the caller. 130 * @param advertise "true" if the appender configuration should be advertised, "false" otherwise. 131 * @param advertiseURI The advertised URI which can be used to retrieve the file contents. 132 * @param config The Configuration. 133 * @return A RollingFileAppender. 134 */ 135 @PluginFactory 136 public static RollingFileAppender createAppender( 137 @PluginAttribute("fileName") final String fileName, 138 @PluginAttribute("filePattern") final String filePattern, 139 @PluginAttribute("append") final String append, 140 @PluginAttribute("name") final String name, 141 @PluginAttribute("bufferedIO") final String bufferedIO, 142 @PluginAttribute("bufferSize") final String bufferSizeStr, 143 @PluginAttribute("immediateFlush") final String immediateFlush, 144 @PluginElement("Policy") final TriggeringPolicy policy, 145 @PluginElement("Strategy") RolloverStrategy strategy, 146 @PluginElement("Layout") Layout<? extends Serializable> layout, 147 @PluginElement("Filter") final Filter filter, 148 @PluginAttribute("ignoreExceptions") final String ignore, 149 @PluginAttribute("advertise") final String advertise, 150 @PluginAttribute("advertiseURI") final String advertiseURI, 151 @PluginConfiguration final Configuration config) { 152 153 final boolean isAppend = Booleans.parseBoolean(append, true); 154 final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); 155 final boolean isBuffered = Booleans.parseBoolean(bufferedIO, true); 156 final boolean isFlush = Booleans.parseBoolean(immediateFlush, true); 157 final boolean isAdvertise = Boolean.parseBoolean(advertise); 158 final int bufferSize = Integers.parseInt(bufferSizeStr, DEFAULT_BUFFER_SIZE); 159 if (!isBuffered && bufferSize > 0) { 160 LOGGER.warn("The bufferSize is set to {} but bufferedIO is not true: {}", bufferSize, bufferedIO); 161 } 162 if (name == null) { 163 LOGGER.error("No name provided for FileAppender"); 164 return null; 165 } 166 167 if (fileName == null) { 168 LOGGER.error("No filename was provided for FileAppender with name " + name); 169 return null; 170 } 171 172 if (filePattern == null) { 173 LOGGER.error("No filename pattern provided for FileAppender with name " + name); 174 return null; 175 } 176 177 if (policy == null) { 178 LOGGER.error("A TriggeringPolicy must be provided"); 179 return null; 180 } 181 182 if (strategy == null) { 183 strategy = DefaultRolloverStrategy.createStrategy(null, null, null, 184 String.valueOf(Deflater.DEFAULT_COMPRESSION), null, true, config); 185 } 186 187 if (layout == null) { 188 layout = PatternLayout.createDefaultLayout(); 189 } 190 191 final RollingFileManager manager = RollingFileManager.getFileManager(fileName, filePattern, isAppend, 192 isBuffered, policy, strategy, advertiseURI, layout, bufferSize, isFlush); 193 if (manager == null) { 194 return null; 195 } 196 197 return new RollingFileAppender(name, layout, filter, manager, fileName, filePattern, 198 ignoreExceptions, isFlush, isAdvertise ? config.getAdvertiser() : null); 199 } 200}