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 org.apache.logging.log4j.core.Filter;
20  import org.apache.logging.log4j.core.Layout;
21  import org.apache.logging.log4j.core.config.plugins.Plugin;
22  import org.apache.logging.log4j.core.config.plugins.PluginAttr;
23  import org.apache.logging.log4j.core.config.plugins.PluginElement;
24  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
25  import org.apache.logging.log4j.core.layout.PatternLayout;
26  
27  /**
28   * File Appender.
29   */
30  @Plugin(name = "File", type = "Core", elementType = "appender", printObject = true)
31  public final class FileAppender extends AbstractOutputStreamAppender {
32  
33      private final String fileName;
34  
35      private FileAppender(String name, Layout layout, Filter filter, FileManager manager, String filename,
36                          boolean handleException, boolean immediateFlush) {
37          super(name, layout, filter, handleException, immediateFlush, manager);
38          this.fileName = filename;
39      }
40  
41      /**
42       * Returns the file name this appender is associated with.
43       * @return The File name.
44       */
45      public String getFileName() {
46          return this.fileName;
47      }
48  
49      /**
50       * Create a File Appender.
51       * @param fileName The name and path of the file.
52       * @param append "True" if the file should be appended to, "false" if it should be overwritten.
53       * The default is "true".
54       * @param locking "True" if the file should be locked. The default is "false".
55       * @param name The name of the Appender.
56       * @param immediateFlush "true" if the contents should be flushed on every write, "false" otherwise. The default
57       * is "true".
58       * @param suppress "true" if exceptions should be hidden from the application, "false" otherwise.
59       * The default is "true".
60       * @param bufferedIO "true" if I/O should be buffered, "false" otherwise. The default is "true".
61       * @param layout The layout to use to format the event. If no layout is provided the default PatternLayout
62       * will be used.
63       * @param filter The filter, if any, to use.
64       * @return The FileAppender.
65       */
66      @PluginFactory
67      public static FileAppender createAppender(@PluginAttr("fileName") String fileName,
68                                                @PluginAttr("append") String append,
69                                                @PluginAttr("locking") String locking,
70                                                @PluginAttr("name") String name,
71                                                @PluginAttr("immediateFlush") String immediateFlush,
72                                                @PluginAttr("suppressExceptions") String suppress,
73                                                @PluginAttr("bufferedIO") String bufferedIO,
74                                                @PluginElement("layout") Layout layout,
75                                                @PluginElement("filters") Filter filter) {
76  
77          boolean isAppend = append == null ? true : Boolean.valueOf(append);
78          boolean isLocking = locking == null ? false : Boolean.valueOf(locking);
79          boolean isBuffered = bufferedIO == null ? true : Boolean.valueOf(bufferedIO);
80          if (isLocking && isBuffered) {
81              if (bufferedIO != null) {
82                  LOGGER.warn("Locking and buffering are mutually exclusive. No buffereing will occur for " + fileName);
83              }
84              isBuffered = false;
85          }
86          boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush);
87          boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
88  
89          if (name == null) {
90              LOGGER.error("No name provided for FileAppender");
91              return null;
92          }
93  
94          if (fileName == null) {
95              LOGGER.error("No filename provided for FileAppender with name "  + name);
96              return null;
97          }
98  
99          FileManager manager = FileManager.getFileManager(fileName, isAppend, isLocking, isBuffered);
100         if (manager == null) {
101             return null;
102         }
103         if (layout == null) {
104             layout = PatternLayout.createLayout(null, null, null, null);
105         }
106         return new FileAppender(name, layout, filter, manager, fileName, handleExceptions, isFlush);
107     }
108 }