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