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