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;
022import java.util.concurrent.TimeUnit;
023import java.util.zip.Deflater;
024
025import org.apache.logging.log4j.core.Appender;
026import org.apache.logging.log4j.core.Core;
027import org.apache.logging.log4j.core.Filter;
028import org.apache.logging.log4j.core.Layout;
029import org.apache.logging.log4j.core.LogEvent;
030import org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy;
031import org.apache.logging.log4j.core.appender.rolling.RollingRandomAccessFileManager;
032import org.apache.logging.log4j.core.appender.rolling.RolloverStrategy;
033import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy;
034import org.apache.logging.log4j.core.config.Configuration;
035import org.apache.logging.log4j.core.config.plugins.Plugin;
036import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
037import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
038import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
039import org.apache.logging.log4j.core.config.plugins.PluginElement;
040import org.apache.logging.log4j.core.layout.PatternLayout;
041import org.apache.logging.log4j.core.net.Advertiser;
042import org.apache.logging.log4j.core.util.Booleans;
043import org.apache.logging.log4j.core.util.Integers;
044
045/**
046 * An appender that writes to random access files and can roll over at
047 * intervals.
048 */
049@Plugin(name = "RollingRandomAccessFile", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE, printObject = true)
050public final class RollingRandomAccessFileAppender extends AbstractOutputStreamAppender<RollingRandomAccessFileManager> {
051
052    public static class Builder<B extends Builder<B>> extends AbstractOutputStreamAppender.Builder<B>
053            implements org.apache.logging.log4j.core.util.Builder<RollingRandomAccessFileAppender> {
054
055        public Builder() {
056            super();
057            withBufferSize(RollingRandomAccessFileManager.DEFAULT_BUFFER_SIZE);
058            withIgnoreExceptions(true);
059            withImmediateFlush(true);
060        }
061
062        @PluginBuilderAttribute("fileName")
063        private String fileName;
064
065        @PluginBuilderAttribute("filePattern")
066        private String filePattern;
067
068        @PluginBuilderAttribute("append")
069        private boolean append = true;
070
071        @PluginElement("Policy")
072        private TriggeringPolicy policy;
073
074        @PluginElement("Strategy")
075        private RolloverStrategy strategy;
076
077        @PluginBuilderAttribute("advertise")
078        private boolean advertise;
079
080        @PluginBuilderAttribute("advertiseURI")
081        private String advertiseURI;
082
083        @Override
084        public RollingRandomAccessFileAppender build() {
085            final String name = getName();
086            if (name == null) {
087                LOGGER.error("No name provided for FileAppender");
088                return null;
089            }
090
091            if (fileName == null) {
092                LOGGER.error("No filename was provided for FileAppender with name " + name);
093                return null;
094            }
095
096            if (filePattern == null) {
097                LOGGER.error("No filename pattern provided for FileAppender with name " + name);
098                return null;
099            }
100
101            if (policy == null) {
102                LOGGER.error("A TriggeringPolicy must be provided");
103                return null;
104            }
105
106            if (strategy == null) {
107                strategy = DefaultRolloverStrategy.createStrategy(null, null, null,
108                        String.valueOf(Deflater.DEFAULT_COMPRESSION), null, true, getConfiguration());
109            }
110
111            final Layout<? extends Serializable> layout = getOrCreateLayout();
112
113            final boolean immediateFlush = isImmediateFlush();
114            final int bufferSize = getBufferSize();
115            final RollingRandomAccessFileManager manager = RollingRandomAccessFileManager
116                    .getRollingRandomAccessFileManager(fileName, filePattern, append, immediateFlush, bufferSize, policy,
117                            strategy, advertiseURI, layout, getConfiguration());
118            if (manager == null) {
119                return null;
120            }
121
122            manager.initialize();
123
124            return new RollingRandomAccessFileAppender(name, layout,getFilter(), manager, fileName, filePattern,
125                    isIgnoreExceptions(), immediateFlush, bufferSize, advertise ? getConfiguration().getAdvertiser() : null);
126        }
127
128        public B withFileName(final String fileName) {
129            this.fileName = fileName;
130            return asBuilder();
131        }
132
133        public B withFilePattern(final String filePattern) {
134            this.filePattern = filePattern;
135            return asBuilder();
136        }
137
138        public B withAppend(final boolean append) {
139            this.append = append;
140            return asBuilder();
141        }
142
143        public B withPolicy(final TriggeringPolicy policy) {
144            this.policy = policy;
145            return asBuilder();
146        }
147
148        public B withStrategy(final RolloverStrategy strategy) {
149            this.strategy = strategy;
150            return asBuilder();
151        }
152
153        public B withAdvertise(final boolean advertise) {
154            this.advertise = advertise;
155            return asBuilder();
156        }
157
158        public B withAdvertiseURI(final String advertiseURI) {
159            this.advertiseURI = advertiseURI;
160            return asBuilder();
161        }
162
163    }
164    
165    private final String fileName;
166    private final String filePattern;
167    private final Object advertisement;
168    private final Advertiser advertiser;
169
170    private RollingRandomAccessFileAppender(final String name, final Layout<? extends Serializable> layout,
171            final Filter filter, final RollingRandomAccessFileManager manager, final String fileName,
172            final String filePattern, final boolean ignoreExceptions,
173            final boolean immediateFlush, final int bufferSize, final Advertiser advertiser) {
174        super(name, layout, filter, ignoreExceptions, immediateFlush, manager);
175        if (advertiser != null) {
176            final Map<String, String> configuration = new HashMap<>(layout.getContentFormat());
177            configuration.put("contentType", layout.getContentType());
178            configuration.put("name", name);
179            advertisement = advertiser.advertise(configuration);
180        } else {
181            advertisement = null;
182        }
183        this.fileName = fileName;
184        this.filePattern = filePattern;
185        this.advertiser = advertiser;
186    }
187
188    @Override
189    public boolean stop(final long timeout, final TimeUnit timeUnit) {
190        setStopping();
191        super.stop(timeout, timeUnit, false);
192        if (advertiser != null) {
193            advertiser.unadvertise(advertisement);
194        }
195        setStopped();
196        return true;
197    }
198
199    /**
200     * Write the log entry rolling over the file when required.
201     *
202     * @param event The LogEvent.
203     */
204    @Override
205    public void append(final LogEvent event) {
206        final RollingRandomAccessFileManager manager = getManager();
207        manager.checkRollover(event);
208
209        // Leverage the nice batching behaviour of async Loggers/Appenders:
210        // we can signal the file manager that it needs to flush the buffer
211        // to disk at the end of a batch.
212        // From a user's point of view, this means that all log events are
213        // _always_ available in the log file, without incurring the overhead
214        // of immediateFlush=true.
215        manager.setEndOfBatch(event.isEndOfBatch()); // FIXME manager's EndOfBatch threadlocal can be deleted
216
217        // LOG4J2-1292 utilize gc-free Layout.encode() method: taken care of in superclass
218        super.append(event);
219    }
220
221    /**
222     * Returns the File name for the Appender.
223     *
224     * @return The file name.
225     */
226    public String getFileName() {
227        return fileName;
228    }
229
230    /**
231     * Returns the file pattern used when rolling over.
232     *
233     * @return The file pattern.
234     */
235    public String getFilePattern() {
236        return filePattern;
237    }
238
239    /**
240     * Returns the size of the file manager's buffer.
241     * @return the buffer size
242     */
243    public int getBufferSize() {
244        return getManager().getBufferSize();
245    }
246
247    /**
248     * Create a RollingRandomAccessFileAppender.
249     *
250     * @param fileName The name of the file that is actively written to.
251     *            (required).
252     * @param filePattern The pattern of the file name to use on rollover.
253     *            (required).
254     * @param append If true, events are appended to the file. If false, the
255     *            file is overwritten when opened. Defaults to "true"
256     * @param name The name of the Appender (required).
257     * @param immediateFlush When true, events are immediately flushed. Defaults
258     *            to "true".
259     * @param bufferSizeStr The buffer size, defaults to {@value RollingRandomAccessFileManager#DEFAULT_BUFFER_SIZE}.
260     * @param policy The triggering policy. (required).
261     * @param strategy The rollover strategy. Defaults to
262     *            DefaultRolloverStrategy.
263     * @param layout The layout to use (defaults to the default PatternLayout).
264     * @param filter The Filter or null.
265     * @param ignoreExceptions If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
266     *               they are propagated to the caller.
267     * @param advertise "true" if the appender configuration should be
268     *            advertised, "false" otherwise.
269     * @param advertiseURI The advertised URI which can be used to retrieve the
270     *            file contents.
271     * @param configuration The Configuration.
272     * @return A RollingRandomAccessFileAppender.
273     * @deprecated Use {@link #newBuilder()}.
274     */
275    @Deprecated
276    public static <B extends Builder<B>> RollingRandomAccessFileAppender createAppender(
277            final String fileName,
278            final String filePattern,
279            final String append,
280            final String name,
281            final String immediateFlush,
282            final String bufferSizeStr,
283            final TriggeringPolicy policy,
284            final RolloverStrategy strategy,
285            final Layout<? extends Serializable> layout,
286            final Filter filter,
287            final String ignoreExceptions,
288            final String advertise,
289            final String advertiseURI,
290            final Configuration configuration) {
291
292        final boolean isAppend = Booleans.parseBoolean(append, true);
293        final boolean isIgnoreExceptions = Booleans.parseBoolean(ignoreExceptions, true);
294        final boolean isImmediateFlush = Booleans.parseBoolean(immediateFlush, true);
295        final boolean isAdvertise = Boolean.parseBoolean(advertise);
296        final int bufferSize = Integers.parseInt(bufferSizeStr, RollingRandomAccessFileManager.DEFAULT_BUFFER_SIZE);
297
298        return RollingRandomAccessFileAppender.<B>newBuilder()
299           .withAdvertise(isAdvertise)
300           .withAdvertiseURI(advertiseURI)
301           .withAppend(isAppend)
302           .withBufferSize(bufferSize)
303           .setConfiguration(configuration)
304           .withFileName(fileName)
305           .withFilePattern(filePattern)
306           .withFilter(filter)
307           .withIgnoreExceptions(isIgnoreExceptions)
308           .withImmediateFlush(isImmediateFlush)
309           .withLayout(layout)
310           .withName(name)
311           .withPolicy(policy)
312           .withStrategy(strategy)
313           .build();
314    }
315    
316    @PluginBuilderFactory
317    public static <B extends Builder<B>> B newBuilder() {
318        return new Builder<B>().asBuilder();
319    }
320
321}