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 */ 017 package org.apache.logging.log4j.core.appender; 018 019 import org.apache.logging.log4j.core.Filter; 020 import org.apache.logging.log4j.core.Layout; 021 import org.apache.logging.log4j.core.LogEvent; 022 import org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy; 023 import org.apache.logging.log4j.core.appender.rolling.FastRollingFileManager; 024 import org.apache.logging.log4j.core.appender.rolling.RollingFileManager; 025 import org.apache.logging.log4j.core.appender.rolling.RolloverStrategy; 026 import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy; 027 import org.apache.logging.log4j.core.config.Configuration; 028 import org.apache.logging.log4j.core.config.plugins.Plugin; 029 import org.apache.logging.log4j.core.config.plugins.PluginAttr; 030 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration; 031 import org.apache.logging.log4j.core.config.plugins.PluginElement; 032 import org.apache.logging.log4j.core.config.plugins.PluginFactory; 033 import org.apache.logging.log4j.core.layout.PatternLayout; 034 import org.apache.logging.log4j.core.net.Advertiser; 035 036 import java.io.Serializable; 037 import java.util.HashMap; 038 import java.util.Map; 039 040 /** 041 * An appender that writes to random access files and can roll over at 042 * intervals. 043 */ 044 @Plugin(name = "FastRollingFile", category = "Core", elementType = "appender", printObject = true) 045 public final class FastRollingFileAppender<T extends Serializable> extends AbstractOutputStreamAppender<T> { 046 047 private final String fileName; 048 private final String filePattern; 049 private Object advertisement; 050 private final Advertiser advertiser; 051 052 private FastRollingFileAppender(String name, Layout<T> layout, 053 Filter filter, RollingFileManager manager, String fileName, 054 String filePattern, boolean handleException, 055 boolean immediateFlush, Advertiser advertiser) { 056 super(name, layout, filter, handleException, immediateFlush, manager); 057 if (advertiser != null) { 058 Map<String, String> configuration = new HashMap<String, String>( 059 layout.getContentFormat()); 060 configuration.put("contentType", layout.getContentType()); 061 configuration.put("name", name); 062 advertisement = advertiser.advertise(configuration); 063 } 064 this.fileName = fileName; 065 this.filePattern = filePattern; 066 this.advertiser = advertiser; 067 } 068 069 @Override 070 public void stop() { 071 super.stop(); 072 if (advertiser != null) { 073 advertiser.unadvertise(advertisement); 074 } 075 } 076 077 /** 078 * Write the log entry rolling over the file when required. 079 * 080 * @param event The LogEvent. 081 */ 082 @Override 083 public void append(final LogEvent event) { 084 FastRollingFileManager manager = (FastRollingFileManager) getManager(); 085 manager.checkRollover(event); 086 087 // Leverage the nice batching behaviour of async Loggers/Appenders: 088 // we can signal the file manager that it needs to flush the buffer 089 // to disk at the end of a batch. 090 // From a user's point of view, this means that all log events are 091 // _always_ available in the log file, without incurring the overhead 092 // of immediateFlush=true. 093 manager.setEndOfBatch(event.isEndOfBatch()); 094 super.append(event); 095 } 096 097 /** 098 * Returns the File name for the Appender. 099 * 100 * @return The file name. 101 */ 102 public String getFileName() { 103 return fileName; 104 } 105 106 /** 107 * Returns the file pattern used when rolling over. 108 * 109 * @return The file pattern. 110 */ 111 public String getFilePattern() { 112 return filePattern; 113 } 114 115 /** 116 * Create a FastRollingFileAppender. 117 * 118 * @param fileName The name of the file that is actively written to. 119 * (required). 120 * @param filePattern The pattern of the file name to use on rollover. 121 * (required). 122 * @param append If true, events are appended to the file. If false, the 123 * file is overwritten when opened. Defaults to "true" 124 * @param name The name of the Appender (required). 125 * @param immediateFlush When true, events are immediately flushed. Defaults 126 * to "true". 127 * @param policy The triggering policy. (required). 128 * @param strategy The rollover strategy. Defaults to 129 * DefaultRolloverStrategy. 130 * @param layout The layout to use (defaults to the default PatternLayout). 131 * @param filter The Filter or null. 132 * @param suppress "true" if exceptions should be hidden from the 133 * application, "false" otherwise. The default is "true". 134 * @param advertise "true" if the appender configuration should be 135 * advertised, "false" otherwise. 136 * @param advertiseURI The advertised URI which can be used to retrieve the 137 * file contents. 138 * @param config The Configuration. 139 * @return A FastRollingFileAppender. 140 */ 141 @PluginFactory 142 public static <S extends Serializable> FastRollingFileAppender<S> createAppender( 143 @PluginAttr("fileName") final String fileName, 144 @PluginAttr("filePattern") final String filePattern, 145 @PluginAttr("append") final String append, 146 @PluginAttr("name") final String name, 147 @PluginAttr("immediateFlush") final String immediateFlush, 148 @PluginElement("policy") final TriggeringPolicy policy, 149 @PluginElement("strategy") RolloverStrategy strategy, 150 @PluginElement("layout") Layout<S> layout, 151 @PluginElement("filter") final Filter filter, 152 @PluginAttr("suppressExceptions") final String suppress, 153 @PluginAttr("advertise") final String advertise, 154 @PluginAttr("advertiseURI") final String advertiseURI, 155 @PluginConfiguration final Configuration config) { 156 157 final boolean isAppend = append == null ? true : Boolean 158 .valueOf(append); 159 final boolean handleExceptions = suppress == null ? true : Boolean 160 .valueOf(suppress); 161 final boolean isFlush = immediateFlush == null ? true : Boolean 162 .valueOf(immediateFlush); 163 boolean isAdvertise = advertise == null ? false : Boolean 164 .valueOf(advertise); 165 166 if (name == null) { 167 LOGGER.error("No name provided for FileAppender"); 168 return null; 169 } 170 171 if (fileName == null) { 172 LOGGER.error("No filename was provided for FileAppender with name " 173 + name); 174 return null; 175 } 176 177 if (filePattern == null) { 178 LOGGER.error("No filename pattern provided for FileAppender with name " 179 + name); 180 return null; 181 } 182 183 if (policy == null) { 184 LOGGER.error("A TriggeringPolicy must be provided"); 185 return null; 186 } 187 188 if (strategy == null) { 189 strategy = DefaultRolloverStrategy.createStrategy(null, null, 190 "true", config); 191 } 192 193 if (layout == null) { 194 @SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"}) 195 Layout<S> l = (Layout<S>)PatternLayout.createLayout(null, null, null, null, null); 196 layout = l; 197 } 198 199 200 final FastRollingFileManager manager = FastRollingFileManager.getFastRollingFileManager(fileName, filePattern, 201 isAppend, isFlush, policy, strategy, advertiseURI, layout); 202 if (manager == null) { 203 return null; 204 } 205 206 return new FastRollingFileAppender<S>(name, layout, filter, manager, 207 fileName, filePattern, handleExceptions, isFlush, 208 isAdvertise ? config.getAdvertiser() : null); 209 } 210 }