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