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.helpers.Booleans; 038import org.apache.logging.log4j.core.layout.PatternLayout; 039import org.apache.logging.log4j.core.net.Advertiser; 040 041/** 042 * An appender that writes to files and can roll over at intervals. 043 */ 044@Plugin(name = "RollingFile", category = "Core", elementType = "appender", printObject = true) 045public final class RollingFileAppender extends AbstractOutputStreamAppender<RollingFileManager> { 046 047 private final String fileName; 048 private final String filePattern; 049 private Object advertisement; 050 private final Advertiser advertiser; 051 052 053 private RollingFileAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter, 054 final RollingFileManager manager, final String fileName, 055 final String filePattern, final boolean ignoreExceptions, final boolean immediateFlush, 056 final Advertiser advertiser) { 057 super(name, layout, filter, ignoreExceptions, immediateFlush, manager); 058 if (advertiser != null) { 059 final Map<String, String> configuration = new HashMap<String, String>(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 getManager().checkRollover(event); 085 super.append(event); 086 } 087 088 /** 089 * Returns the File name for the Appender. 090 * @return The file name. 091 */ 092 public String getFileName() { 093 return fileName; 094 } 095 096 /** 097 * Returns the file pattern used when rolling over. 098 * @return The file pattern. 099 */ 100 public String getFilePattern() { 101 return filePattern; 102 } 103 104 /** 105 * Create a RollingFileAppender. 106 * @param fileName The name of the file that is actively written to. (required). 107 * @param filePattern The pattern of the file name to use on rollover. (required). 108 * @param append If true, events are appended to the file. If false, the file 109 * is overwritten when opened. Defaults to "true" 110 * @param name The name of the Appender (required). 111 * @param bufferedIO When true, I/O will be buffered. Defaults to "true". 112 * @param immediateFlush When true, events are immediately flushed. Defaults to "true". 113 * @param policy The triggering policy. (required). 114 * @param strategy The rollover strategy. Defaults to DefaultRolloverStrategy. 115 * @param layout The layout to use (defaults to the default PatternLayout). 116 * @param filter The Filter or null. 117 * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise 118 * they are propagated to the caller. 119 * @param advertise "true" if the appender configuration should be advertised, "false" otherwise. 120 * @param advertiseURI The advertised URI which can be used to retrieve the file contents. 121 * @param config The Configuration. 122 * @return A RollingFileAppender. 123 */ 124 @PluginFactory 125 public static RollingFileAppender createAppender( 126 @PluginAttribute("fileName") final String fileName, 127 @PluginAttribute("filePattern") final String filePattern, 128 @PluginAttribute("append") final String append, 129 @PluginAttribute("name") final String name, 130 @PluginAttribute("bufferedIO") final String bufferedIO, 131 @PluginAttribute("immediateFlush") final String immediateFlush, 132 @PluginElement("Policy") final TriggeringPolicy policy, 133 @PluginElement("Strategy") RolloverStrategy strategy, 134 @PluginElement("Layout") Layout<? extends Serializable> layout, 135 @PluginElement("Filter") final Filter filter, 136 @PluginAttribute("ignoreExceptions") final String ignore, 137 @PluginAttribute("advertise") final String advertise, 138 @PluginAttribute("advertiseURI") final String advertiseURI, 139 @PluginConfiguration final Configuration config) { 140 141 final boolean isAppend = Booleans.parseBoolean(append, true); 142 final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); 143 final boolean isBuffered = Booleans.parseBoolean(bufferedIO, true); 144 final boolean isFlush = Booleans.parseBoolean(immediateFlush, true); 145 final boolean isAdvertise = Boolean.parseBoolean(advertise); 146 if (name == null) { 147 LOGGER.error("No name provided for FileAppender"); 148 return null; 149 } 150 151 if (fileName == null) { 152 LOGGER.error("No filename was provided for FileAppender with name " + name); 153 return null; 154 } 155 156 if (filePattern == null) { 157 LOGGER.error("No filename pattern provided for FileAppender with name " + name); 158 return null; 159 } 160 161 if (policy == null) { 162 LOGGER.error("A TriggeringPolicy must be provided"); 163 return null; 164 } 165 166 if (strategy == null) { 167 strategy = DefaultRolloverStrategy.createStrategy(null, null, null, 168 String.valueOf(Deflater.DEFAULT_COMPRESSION), config); 169 } 170 171 if (layout == null) { 172 layout = PatternLayout.createLayout(null, null, null, null, null, null); 173 } 174 175 final RollingFileManager manager = RollingFileManager.getFileManager(fileName, filePattern, isAppend, 176 isBuffered, policy, strategy, advertiseURI, layout); 177 if (manager == null) { 178 return null; 179 } 180 181 return new RollingFileAppender(name, layout, filter, manager, fileName, filePattern, 182 ignoreExceptions, isFlush, isAdvertise ? config.getAdvertiser() : null); 183 } 184}