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