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 java.util.HashMap; 020 import java.util.Map; 021 022 import org.apache.logging.log4j.core.Filter; 023 import org.apache.logging.log4j.core.Layout; 024 import org.apache.logging.log4j.core.LogEvent; 025 import org.apache.logging.log4j.core.config.Configuration; 026 import org.apache.logging.log4j.core.config.plugins.Plugin; 027 import org.apache.logging.log4j.core.config.plugins.PluginAttr; 028 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration; 029 import org.apache.logging.log4j.core.config.plugins.PluginElement; 030 import org.apache.logging.log4j.core.config.plugins.PluginFactory; 031 import org.apache.logging.log4j.core.layout.PatternLayout; 032 import org.apache.logging.log4j.core.net.Advertiser; 033 034 /** 035 * File Appender. 036 */ 037 @Plugin(name = "FastFile", type = "Core", elementType = "appender", printObject = true) 038 public final class FastFileAppender extends AbstractOutputStreamAppender { 039 040 private final String fileName; 041 private Object advertisement; 042 private final Advertiser advertiser; 043 044 private FastFileAppender(String name, Layout<?> layout, Filter filter, 045 FastFileManager manager, String filename, boolean handleException, 046 boolean immediateFlush, Advertiser advertiser) { 047 super(name, layout, filter, handleException, immediateFlush, manager); 048 if (advertiser != null) { 049 Map<String, String> configuration = new HashMap<String, String>( 050 layout.getContentFormat()); 051 configuration.putAll(manager.getContentFormat()); 052 configuration.put("contentType", layout.getContentType()); 053 configuration.put("name", name); 054 advertisement = advertiser.advertise(configuration); 055 } 056 this.fileName = filename; 057 this.advertiser = advertiser; 058 } 059 060 @Override 061 public void stop() { 062 super.stop(); 063 if (advertiser != null) { 064 advertiser.unadvertise(advertisement); 065 } 066 } 067 068 /** 069 * Write the log entry rolling over the file when required. 070 * 071 * @param event The LogEvent. 072 */ 073 @Override 074 public void append(LogEvent event) { 075 076 // Leverage the nice batching behaviour of async Loggers/Appenders: 077 // we can signal the file manager that it needs to flush the buffer 078 // to disk at the end of a batch. 079 // From a user's point of view, this means that all log events are 080 // _always_ available in the log file, without incurring the overhead 081 // of immediateFlush=true. 082 ((FastFileManager) getManager()).setEndOfBatch(event.isEndOfBatch()); 083 super.append(event); 084 } 085 086 /** 087 * Returns the file name this appender is associated with. 088 * 089 * @return The File name. 090 */ 091 public String getFileName() { 092 return this.fileName; 093 } 094 095 // difference from standard File Appender: 096 // locking is not supported and buffering cannot be switched off 097 /** 098 * Create a File Appender. 099 * 100 * @param fileName The name and path of the file. 101 * @param append "True" if the file should be appended to, "false" if it 102 * should be overwritten. The default is "true". 103 * @param name The name of the Appender. 104 * @param immediateFlush "true" if the contents should be flushed on every 105 * write, "false" otherwise. The default is "true". 106 * @param suppress "true" if exceptions should be hidden from the 107 * application, "false" otherwise. The default is "true". 108 * @param layout The layout to use to format the event. If no layout is 109 * provided the default PatternLayout will be used. 110 * @param filter The filter, if any, to use. 111 * @param advertise "true" if the appender configuration should be 112 * advertised, "false" otherwise. 113 * @param advertiseURI The advertised URI which can be used to retrieve the 114 * file contents. 115 * @param config The Configuration. 116 * @return The FileAppender. 117 */ 118 @PluginFactory 119 public static FastFileAppender createAppender( 120 @PluginAttr("fileName") String fileName, 121 @PluginAttr("append") String append, 122 @PluginAttr("name") String name, 123 @PluginAttr("immediateFlush") String immediateFlush, 124 @PluginAttr("suppressExceptions") String suppress, 125 @PluginElement("layout") Layout<?> layout, 126 @PluginElement("filters") final Filter filter, 127 @PluginAttr("advertise") final String advertise, 128 @PluginAttr("advertiseURI") final String advertiseURI, 129 @PluginConfiguration final Configuration config) { 130 131 boolean isAppend = append == null ? true : Boolean.valueOf(append); 132 boolean isFlush = immediateFlush == null ? true : Boolean 133 .valueOf(immediateFlush); 134 boolean handleExceptions = suppress == null ? true : Boolean 135 .valueOf(suppress); 136 boolean isAdvertise = advertise == null ? false : Boolean 137 .valueOf(advertise); 138 139 if (name == null) { 140 LOGGER.error("No name provided for FileAppender"); 141 return null; 142 } 143 144 if (fileName == null) { 145 LOGGER.error("No filename provided for FileAppender with name " 146 + name); 147 return null; 148 } 149 150 FastFileManager manager = FastFileManager.getFileManager(fileName, 151 isAppend, isFlush, advertiseURI); 152 if (manager == null) { 153 return null; 154 } 155 if (layout == null) { 156 layout = PatternLayout.createLayout(null, null, null, null); 157 } 158 return new FastFileAppender(name, layout, filter, manager, fileName, 159 handleExceptions, isFlush, isAdvertise ? config.getAdvertiser() 160 : null); 161 } 162 }