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.LogEvent;
22  import org.apache.logging.log4j.core.config.Configuration;
23  import org.apache.logging.log4j.core.config.plugins.Plugin;
24  import org.apache.logging.log4j.core.config.plugins.PluginAttr;
25  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
26  import org.apache.logging.log4j.core.config.plugins.PluginElement;
27  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
28  import org.apache.logging.log4j.core.layout.PatternLayout;
29  import org.apache.logging.log4j.core.net.Advertiser;
30  
31  import java.io.Serializable;
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  /**
36   * File Appender.
37   */
38  @Plugin(name = "FastFile", category = "Core", elementType = "appender", printObject = true)
39  public final class FastFileAppender<T extends Serializable> extends AbstractOutputStreamAppender<T> {
40  
41      private final String fileName;
42      private Object advertisement;
43      private final Advertiser advertiser;
44  
45      private FastFileAppender(String name, Layout<T> layout, Filter filter,
46              FastFileManager manager, String filename, boolean handleException,
47              boolean immediateFlush, Advertiser advertiser) {
48          super(name, layout, filter, handleException, immediateFlush, manager);
49          if (advertiser != null) {
50              Map<String, String> configuration = new HashMap<String, String>(
51                      layout.getContentFormat());
52              configuration.putAll(manager.getContentFormat());
53              configuration.put("contentType", layout.getContentType());
54              configuration.put("name", name);
55              advertisement = advertiser.advertise(configuration);
56          }
57          this.fileName = filename;
58          this.advertiser = advertiser;
59      }
60  
61      @Override
62      public void stop() {
63          super.stop();
64          if (advertiser != null) {
65              advertiser.unadvertise(advertisement);
66          }
67      }
68  
69      /**
70       * Write the log entry rolling over the file when required.
71       *
72       * @param event The LogEvent.
73       */
74      @Override
75      public void append(LogEvent event) {
76  
77          // Leverage the nice batching behaviour of async Loggers/Appenders:
78          // we can signal the file manager that it needs to flush the buffer
79          // to disk at the end of a batch.
80          // From a user's point of view, this means that all log events are
81          // _always_ available in the log file, without incurring the overhead
82          // of immediateFlush=true.
83          ((FastFileManager) getManager()).setEndOfBatch(event.isEndOfBatch());
84          super.append(event);
85      }
86  
87      /**
88       * Returns the file name this appender is associated with.
89       *
90       * @return The File name.
91       */
92      public String getFileName() {
93          return this.fileName;
94      }
95  
96      // difference from standard File Appender:
97      // locking is not supported and buffering cannot be switched off
98      /**
99       * Create a File Appender.
100      *
101      * @param fileName The name and path of the file.
102      * @param append "True" if the file should be appended to, "false" if it
103      *            should be overwritten. The default is "true".
104      * @param name The name of the Appender.
105      * @param immediateFlush "true" if the contents should be flushed on every
106      *            write, "false" otherwise. The default is "true".
107      * @param suppress "true" if exceptions should be hidden from the
108      *            application, "false" otherwise. The default is "true".
109      * @param layout The layout to use to format the event. If no layout is
110      *            provided the default PatternLayout will be used.
111      * @param filter The filter, if any, to use.
112      * @param advertise "true" if the appender configuration should be
113      *            advertised, "false" otherwise.
114      * @param advertiseURI The advertised URI which can be used to retrieve the
115      *            file contents.
116      * @param config The Configuration.
117      * @return The FileAppender.
118      */
119     @PluginFactory
120     public static <S extends Serializable> FastFileAppender<S> createAppender(
121             @PluginAttr("fileName") String fileName,
122             @PluginAttr("append") String append,
123             @PluginAttr("name") String name,
124             @PluginAttr("immediateFlush") String immediateFlush,
125             @PluginAttr("suppressExceptions") String suppress,
126             @PluginElement("layout") Layout<S> layout,
127             @PluginElement("filters") final Filter filter,
128             @PluginAttr("advertise") final String advertise,
129             @PluginAttr("advertiseURI") final String advertiseURI,
130             @PluginConfiguration final Configuration config) {
131 
132         boolean isAppend = append == null ? true : Boolean.valueOf(append);
133         boolean isFlush = immediateFlush == null ? true : Boolean
134                 .valueOf(immediateFlush);
135         boolean handleExceptions = suppress == null ? true : Boolean
136                 .valueOf(suppress);
137         boolean isAdvertise = advertise == null ? false : Boolean
138                 .valueOf(advertise);
139 
140         if (name == null) {
141             LOGGER.error("No name provided for FileAppender");
142             return null;
143         }
144 
145         if (fileName == null) {
146             LOGGER.error("No filename provided for FileAppender with name "
147                     + name);
148             return null;
149         }
150 
151         FastFileManager manager = FastFileManager.getFileManager(fileName,
152                 isAppend, isFlush, advertiseURI);
153         if (manager == null) {
154             return null;
155         }
156         if (layout == null) {
157             @SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
158             Layout<S> l = (Layout<S>)PatternLayout.createLayout(null, null, null, null);
159             layout = l;
160         }
161         return new FastFileAppender<S>(name, layout, filter, manager, fileName,
162                 handleExceptions, isFlush, isAdvertise ? config.getAdvertiser()
163                         : null);
164     }
165 }