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.RollingFileManager; 024 import org.apache.logging.log4j.core.appender.rolling.RolloverStrategy; 025 import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy; 026 import org.apache.logging.log4j.core.config.Configuration; 027 import org.apache.logging.log4j.core.config.plugins.Plugin; 028 import org.apache.logging.log4j.core.config.plugins.PluginAttr; 029 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration; 030 import org.apache.logging.log4j.core.config.plugins.PluginElement; 031 import org.apache.logging.log4j.core.config.plugins.PluginFactory; 032 import org.apache.logging.log4j.core.layout.PatternLayout; 033 034 /** 035 * An appender that writes to files andd can roll over at intervals. 036 */ 037 @Plugin(name = "RollingFile", type = "Core", elementType = "appender", printObject = true) 038 public final class RollingFileAppender extends AbstractOutputStreamAppender { 039 040 private final String fileName; 041 private final String filePattern; 042 043 044 private RollingFileAppender(final String name, final Layout layout, final Filter filter, 045 final RollingFileManager manager, final String fileName, 046 final String filePattern, final boolean handleException, final boolean immediateFlush) { 047 super(name, layout, filter, handleException, immediateFlush, manager); 048 this.fileName = fileName; 049 this.filePattern = filePattern; 050 } 051 052 /** 053 * Write the log entry rolling over the file when required. 054 055 * @param event The LogEvent. 056 */ 057 @Override 058 public void append(final LogEvent event) { 059 ((RollingFileManager) getManager()).checkRollover(event); 060 super.append(event); 061 } 062 063 /** 064 * Returns the File name for the Appender. 065 * @return The file name. 066 */ 067 public String getFileName() { 068 return fileName; 069 } 070 071 /** 072 * Returns the file pattern used when rolling over. 073 * @return The file pattern. 074 */ 075 public String getFilePattern() { 076 return filePattern; 077 } 078 079 /** 080 * Create a RollingFileAppender. 081 * @param fileName The name of the file that is actively written to. (required). 082 * @param filePattern The pattern of the file name to use on rollover. (required). 083 * @param append If true, events are appended to the file. If false, the file 084 * is overwritten when opened. Defaults to "true" 085 * @param name The name of the Appender (required). 086 * @param bufferedIO When true, I/O will be buffered. Defaults to "true". 087 * @param immediateFlush When true, events are immediately flushed. Defaults to "true". 088 * @param policy The triggering policy. (required). 089 * @param strategy The rollover strategy. Defaults to DefaultRolloverStrategy. 090 * @param layout The layout to use (defaults to the default PatternLayout). 091 * @param filter The Filter or null. 092 * @param suppress "true" if exceptions should be hidden from the application, "false" otherwise. 093 * The default is "true". 094 * @param config The Configuration. 095 * @return A RollingFileAppender. 096 */ 097 @PluginFactory 098 public static RollingFileAppender createAppender(@PluginAttr("fileName") final String fileName, 099 @PluginAttr("filePattern") final String filePattern, 100 @PluginAttr("append") final String append, 101 @PluginAttr("name") final String name, 102 @PluginAttr("bufferedIO") final String bufferedIO, 103 @PluginAttr("immediateFlush") final String immediateFlush, 104 @PluginElement("policy") final TriggeringPolicy policy, 105 @PluginElement("strategy") RolloverStrategy strategy, 106 @PluginElement("layout") Layout layout, 107 @PluginElement("filter") final Filter filter, 108 @PluginAttr("suppressExceptions") final String suppress, 109 @PluginConfiguration final Configuration config) { 110 111 final boolean isAppend = append == null ? true : Boolean.valueOf(append); 112 final boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress); 113 final boolean isBuffered = bufferedIO == null ? true : Boolean.valueOf(bufferedIO); 114 final boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush); 115 116 if (name == null) { 117 LOGGER.error("No name provided for FileAppender"); 118 return null; 119 } 120 121 if (fileName == null) { 122 LOGGER.error("No filename was provided for FileAppender with name " + name); 123 return null; 124 } 125 126 if (filePattern == null) { 127 LOGGER.error("No filename pattern provided for FileAppender with name " + name); 128 return null; 129 } 130 131 if (policy == null) { 132 LOGGER.error("A TriggeringPolicy must be provided"); 133 return null; 134 } 135 136 if (strategy == null) { 137 strategy = DefaultRolloverStrategy.createStrategy(null, null, "true", config); 138 } 139 140 final RollingFileManager manager = RollingFileManager.getFileManager(fileName, filePattern, isAppend, 141 isBuffered, policy, strategy); 142 if (manager == null) { 143 return null; 144 } 145 146 if (layout == null) { 147 layout = PatternLayout.createLayout(null, null, null, null); 148 } 149 150 return new RollingFileAppender(name, layout, filter, manager, fileName, filePattern, 151 handleExceptions, isFlush); 152 } 153 }