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  
23  import org.apache.logging.log4j.core.Filter;
24  import org.apache.logging.log4j.core.Layout;
25  import org.apache.logging.log4j.core.config.Configuration;
26  import org.apache.logging.log4j.core.config.plugins.Plugin;
27  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
28  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
29  import org.apache.logging.log4j.core.config.plugins.PluginElement;
30  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
31  import org.apache.logging.log4j.core.helpers.Booleans;
32  import org.apache.logging.log4j.core.helpers.Integers;
33  import org.apache.logging.log4j.core.layout.PatternLayout;
34  import org.apache.logging.log4j.core.net.Advertiser;
35  
36  /**
37   * File Appender.
38   */
39  @Plugin(name = "File", category = "Core", elementType = "appender", printObject = true)
40  public final class FileAppender extends AbstractOutputStreamAppender<FileManager> {
41  
42      private static final int DEFAULT_BUFFER_SIZE = 8192;
43      private final String fileName;
44      private final Advertiser advertiser;
45      private Object advertisement;
46  
47      private FileAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter, final FileManager manager,
48                           final String filename, final boolean ignoreExceptions, final boolean immediateFlush,
49                           final Advertiser advertiser) {
50          super(name, layout, filter, ignoreExceptions, immediateFlush, manager);
51          if (advertiser != null) {
52              final Map<String, String> configuration = new HashMap<String, String>(layout.getContentFormat());
53              configuration.putAll(manager.getContentFormat());
54              configuration.put("contentType", layout.getContentType());
55              configuration.put("name", name);
56              advertisement = advertiser.advertise(configuration);
57          }
58          this.fileName = filename;
59          this.advertiser = advertiser;
60      }
61  
62      @Override
63      public void stop() {
64          super.stop();
65          if (advertiser != null) {
66              advertiser.unadvertise(advertisement);
67          }
68      }
69  
70      /**
71       * Returns the file name this appender is associated with.
72       * @return The File name.
73       */
74      public String getFileName() {
75          return this.fileName;
76      }
77  
78      /**
79       * Create a File Appender.
80       * @param fileName The name and path of the file.
81       * @param append "True" if the file should be appended to, "false" if it should be overwritten.
82       * The default is "true".
83       * @param locking "True" if the file should be locked. The default is "false".
84       * @param name The name of the Appender.
85       * @param immediateFlush "true" if the contents should be flushed on every write, "false" otherwise. The default
86       * is "true".
87       * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
88       *               they are propagated to the caller.
89       * @param bufferedIO "true" if I/O should be buffered, "false" otherwise. The default is "true".
90       * @param bufferSizeStr buffer size for buffered IO (default is 8192).
91       * @param layout The layout to use to format the event. If no layout is provided the default PatternLayout
92       * will be used.
93       * @param filter The filter, if any, to use.
94       * @param advertise "true" if the appender configuration should be advertised, "false" otherwise.
95       * @param advertiseURI The advertised URI which can be used to retrieve the file contents.
96       * @param config The Configuration
97       * @return The FileAppender.
98       */
99      @PluginFactory
100     public static FileAppender createAppender(
101             @PluginAttribute("fileName") final String fileName,
102             @PluginAttribute("append") final String append,
103             @PluginAttribute("locking") final String locking,
104             @PluginAttribute("name") final String name,
105             @PluginAttribute("immediateFlush") final String immediateFlush,
106             @PluginAttribute("ignoreExceptions") final String ignore,
107             @PluginAttribute("bufferedIO") final String bufferedIO,
108             @PluginAttribute("bufferSize") final String bufferSizeStr,
109             @PluginElement("Layout") Layout<? extends Serializable> layout,
110             @PluginElement("Filters") final Filter filter,
111             @PluginAttribute("advertise") final String advertise,
112             @PluginAttribute("advertiseURI") final String advertiseURI,
113             @PluginConfiguration final Configuration config) {
114 
115         final boolean isAppend = Booleans.parseBoolean(append, true);
116         final boolean isLocking = Boolean.parseBoolean(locking);
117         boolean isBuffered = Booleans.parseBoolean(bufferedIO, true);
118         final boolean isAdvertise = Boolean.parseBoolean(advertise);
119         if (isLocking && isBuffered) {
120             if (bufferedIO != null) {
121                 LOGGER.warn("Locking and buffering are mutually exclusive. No buffering will occur for " + fileName);
122             }
123             isBuffered = false;
124         }
125         final int bufferSize = Integers.parseInt(bufferSizeStr, DEFAULT_BUFFER_SIZE);
126         if (!isBuffered && bufferSize > 0) {
127             LOGGER.warn("The bufferSize is set to {} but bufferedIO is not true: {}", bufferSize, bufferedIO);
128         }
129         final boolean isFlush = Booleans.parseBoolean(immediateFlush, true);
130         final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
131 
132         if (name == null) {
133             LOGGER.error("No name provided for FileAppender");
134             return null;
135         }
136 
137         if (fileName == null) {
138             LOGGER.error("No filename provided for FileAppender with name "  + name);
139             return null;
140         }
141         if (layout == null) {
142             layout = PatternLayout.createLayout(null, null, null, null, null, null);
143         }
144 
145         final FileManager manager = FileManager.getFileManager(fileName, isAppend, isLocking, isBuffered, advertiseURI,
146             layout, bufferSize);
147         if (manager == null) {
148             return null;
149         }
150 
151 
152         return new FileAppender(name, layout, filter, manager, fileName, ignoreExceptions, isFlush,
153                 isAdvertise ? config.getAdvertiser() : null);
154     }
155 }