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