View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender;
18  
19  import java.io.Serializable;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.concurrent.TimeUnit;
23  import java.util.zip.Deflater;
24  
25  import org.apache.logging.log4j.core.Appender;
26  import org.apache.logging.log4j.core.Core;
27  import org.apache.logging.log4j.core.Filter;
28  import org.apache.logging.log4j.core.Layout;
29  import org.apache.logging.log4j.core.LogEvent;
30  import org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy;
31  import org.apache.logging.log4j.core.appender.rolling.RollingRandomAccessFileManager;
32  import org.apache.logging.log4j.core.appender.rolling.RolloverStrategy;
33  import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy;
34  import org.apache.logging.log4j.core.config.Configuration;
35  import org.apache.logging.log4j.core.config.plugins.Plugin;
36  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
37  import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
38  import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
39  import org.apache.logging.log4j.core.config.plugins.PluginElement;
40  import org.apache.logging.log4j.core.layout.PatternLayout;
41  import org.apache.logging.log4j.core.net.Advertiser;
42  import org.apache.logging.log4j.core.util.Booleans;
43  import org.apache.logging.log4j.core.util.Integers;
44  
45  /**
46   * An appender that writes to random access files and can roll over at
47   * intervals.
48   */
49  @Plugin(name = "RollingRandomAccessFile", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE, printObject = true)
50  public final class RollingRandomAccessFileAppender extends AbstractOutputStreamAppender<RollingRandomAccessFileManager> {
51  
52      public static class Builder<B extends Builder<B>> extends AbstractOutputStreamAppender.Builder<B>
53              implements org.apache.logging.log4j.core.util.Builder<RollingRandomAccessFileAppender> {
54  
55          public Builder() {
56              super();
57              withBufferSize(RollingRandomAccessFileManager.DEFAULT_BUFFER_SIZE);
58              withIgnoreExceptions(true);
59              withImmediateFlush(true);
60          }
61  
62          @PluginBuilderAttribute("fileName")
63          private String fileName;
64  
65          @PluginBuilderAttribute("filePattern")
66          private String filePattern;
67  
68          @PluginBuilderAttribute("append")
69          private boolean append = true;
70  
71          @PluginElement("Policy")
72          private TriggeringPolicy policy;
73  
74          @PluginElement("Strategy")
75          private RolloverStrategy strategy;
76  
77          @PluginBuilderAttribute("advertise")
78          private boolean advertise;
79  
80          @PluginBuilderAttribute("advertiseURI")
81          private String advertiseURI;
82  
83          @Override
84          public RollingRandomAccessFileAppender build() {
85              final String name = getName();
86              if (name == null) {
87                  LOGGER.error("No name provided for FileAppender");
88                  return null;
89              }
90  
91              if (fileName == null) {
92                  LOGGER.error("No filename was provided for FileAppender with name " + name);
93                  return null;
94              }
95  
96              if (filePattern == null) {
97                  LOGGER.error("No filename pattern provided for FileAppender with name " + name);
98                  return null;
99              }
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 }