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