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