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 * @param <T> TriggeringPolicy type 110 * @return The TriggeringPolicy 111 */ 112 public <T extends TriggeringPolicy> T getTriggeringPolicy() { 113 return getManager().getTriggeringPolicy(); 114 } 115 116 /** 117 * Create a RollingFileAppender. 118 * @param fileName The name of the file that is actively written to. (required). 119 * @param filePattern The pattern of the file name to use on rollover. (required). 120 * @param append If true, events are appended to the file. If false, the file 121 * is overwritten when opened. Defaults to "true" 122 * @param name The name of the Appender (required). 123 * @param bufferedIO When true, I/O will be buffered. Defaults to "true". 124 * @param bufferSizeStr buffer size for buffered IO (default is 8192). 125 * @param immediateFlush When true, events are immediately flushed. Defaults to "true". 126 * @param policy The triggering policy. (required). 127 * @param strategy The rollover strategy. Defaults to DefaultRolloverStrategy. 128 * @param layout The layout to use (defaults to the default PatternLayout). 129 * @param filter The Filter or null. 130 * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise 131 * they are propagated to the caller. 132 * @param advertise "true" if the appender configuration should be advertised, "false" otherwise. 133 * @param advertiseURI The advertised URI which can be used to retrieve the file contents. 134 * @param config The Configuration. 135 * @return A RollingFileAppender. 136 */ 137 @PluginFactory 138 public static RollingFileAppender createAppender( 139 @PluginAttribute("fileName") final String fileName, 140 @PluginAttribute("filePattern") final String filePattern, 141 @PluginAttribute("append") final String append, 142 @PluginAttribute("name") final String name, 143 @PluginAttribute("bufferedIO") final String bufferedIO, 144 @PluginAttribute("bufferSize") final String bufferSizeStr, 145 @PluginAttribute("immediateFlush") final String immediateFlush, 146 @PluginElement("Policy") final TriggeringPolicy policy, 147 @PluginElement("Strategy") RolloverStrategy strategy, 148 @PluginElement("Layout") Layout<? extends Serializable> layout, 149 @PluginElement("Filter") final Filter filter, 150 @PluginAttribute("ignoreExceptions") final String ignore, 151 @PluginAttribute("advertise") final String advertise, 152 @PluginAttribute("advertiseURI") final String advertiseURI, 153 @PluginConfiguration final Configuration config) { 154 155 final boolean isAppend = Booleans.parseBoolean(append, true); 156 final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); 157 final boolean isBuffered = Booleans.parseBoolean(bufferedIO, true); 158 final boolean isFlush = Booleans.parseBoolean(immediateFlush, true); 159 final boolean isAdvertise = Boolean.parseBoolean(advertise); 160 final int bufferSize = Integers.parseInt(bufferSizeStr, DEFAULT_BUFFER_SIZE); 161 if (!isBuffered && bufferSize > 0) { 162 LOGGER.warn("The bufferSize is set to {} but bufferedIO is not true: {}", bufferSize, bufferedIO); 163 } 164 if (name == null) { 165 LOGGER.error("No name provided for FileAppender"); 166 return null; 167 } 168 169 if (fileName == null) { 170 LOGGER.error("No filename was provided for FileAppender with name " + name); 171 return null; 172 } 173 174 if (filePattern == null) { 175 LOGGER.error("No filename pattern provided for FileAppender with name " + name); 176 return null; 177 } 178 179 if (policy == null) { 180 LOGGER.error("A TriggeringPolicy must be provided"); 181 return null; 182 } 183 184 if (strategy == null) { 185 strategy = DefaultRolloverStrategy.createStrategy(null, null, null, 186 String.valueOf(Deflater.DEFAULT_COMPRESSION), null, true, config); 187 } 188 189 if (layout == null) { 190 layout = PatternLayout.createDefaultLayout(); 191 } 192 193 final RollingFileManager manager = RollingFileManager.getFileManager(fileName, filePattern, isAppend, 194 isBuffered, policy, strategy, advertiseURI, layout, bufferSize); 195 if (manager == null) { 196 return null; 197 } 198 199 return new RollingFileAppender(name, layout, filter, manager, fileName, filePattern, 200 ignoreExceptions, isFlush, isAdvertise ? config.getAdvertiser() : null); 201 } 202}