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  import java.util.zip.Deflater;
23  
24  import org.apache.logging.log4j.core.Filter;
25  import org.apache.logging.log4j.core.Layout;
26  import org.apache.logging.log4j.core.LogEvent;
27  import org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy;
28  import org.apache.logging.log4j.core.appender.rolling.RollingFileManager;
29  import org.apache.logging.log4j.core.appender.rolling.RolloverStrategy;
30  import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy;
31  import org.apache.logging.log4j.core.config.Configuration;
32  import org.apache.logging.log4j.core.config.plugins.Plugin;
33  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
34  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
35  import org.apache.logging.log4j.core.config.plugins.PluginElement;
36  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
37  import org.apache.logging.log4j.core.helpers.Booleans;
38  import org.apache.logging.log4j.core.layout.PatternLayout;
39  import org.apache.logging.log4j.core.net.Advertiser;
40  
41  /**
42   * An appender that writes to files and can roll over at intervals.
43   */
44  @Plugin(name = "RollingFile", category = "Core", elementType = "appender", printObject = true)
45  public final class RollingFileAppender extends AbstractOutputStreamAppender<RollingFileManager> {
46  
47      private final String fileName;
48      private final String filePattern;
49      private Object advertisement;
50      private final Advertiser advertiser;
51  
52  
53      private RollingFileAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter,
54                                  final RollingFileManager manager, final String fileName,
55                                  final String filePattern, final boolean ignoreExceptions, final boolean immediateFlush,
56                                  final Advertiser advertiser) {
57          super(name, layout, filter, ignoreExceptions, immediateFlush, manager);
58          if (advertiser != null) {
59              final Map<String, String> configuration = new HashMap<String, String>(layout.getContentFormat());
60              configuration.put("contentType", layout.getContentType());
61              configuration.put("name", name);
62              advertisement = advertiser.advertise(configuration);
63          }
64          this.fileName = fileName;
65          this.filePattern = filePattern;
66          this.advertiser = advertiser;
67      }
68  
69      @Override
70      public void stop() {
71          super.stop();
72          if (advertiser != null) {
73              advertiser.unadvertise(advertisement);
74          }
75      }
76  
77      /**
78       * Write the log entry rolling over the file when required.
79  
80       * @param event The LogEvent.
81       */
82      @Override
83      public void append(final LogEvent event) {
84          getManager().checkRollover(event);
85          super.append(event);
86      }
87  
88      /**
89       * Returns the File name for the Appender.
90       * @return The file name.
91       */
92      public String getFileName() {
93          return fileName;
94      }
95  
96      /**
97       * Returns the file pattern used when rolling over.
98       * @return The file pattern.
99       */
100     public String getFilePattern() {
101         return filePattern;
102     }
103 
104     /**
105      * Create a RollingFileAppender.
106      * @param fileName The name of the file that is actively written to. (required).
107      * @param filePattern The pattern of the file name to use on rollover. (required).
108      * @param append If true, events are appended to the file. If false, the file
109      * is overwritten when opened. Defaults to "true"
110      * @param name The name of the Appender (required).
111      * @param bufferedIO When true, I/O will be buffered. Defaults to "true".
112      * @param immediateFlush When true, events are immediately flushed. Defaults to "true".
113      * @param policy The triggering policy. (required).
114      * @param strategy The rollover strategy. Defaults to DefaultRolloverStrategy.
115      * @param layout The layout to use (defaults to the default PatternLayout).
116      * @param filter The Filter or null.
117      * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
118      *               they are propagated to the caller.
119      * @param advertise "true" if the appender configuration should be advertised, "false" otherwise.
120      * @param advertiseURI The advertised URI which can be used to retrieve the file contents.
121      * @param config The Configuration.
122      * @return A RollingFileAppender.
123      */
124     @PluginFactory
125     public static RollingFileAppender createAppender(
126             @PluginAttribute("fileName") final String fileName,
127             @PluginAttribute("filePattern") final String filePattern,
128             @PluginAttribute("append") final String append,
129             @PluginAttribute("name") final String name,
130             @PluginAttribute("bufferedIO") final String bufferedIO,
131             @PluginAttribute("immediateFlush") final String immediateFlush,
132             @PluginElement("Policy") final TriggeringPolicy policy,
133             @PluginElement("Strategy") RolloverStrategy strategy,
134             @PluginElement("Layout") Layout<? extends Serializable> layout,
135             @PluginElement("Filter") final Filter filter,
136             @PluginAttribute("ignoreExceptions") final String ignore,
137             @PluginAttribute("advertise") final String advertise,
138             @PluginAttribute("advertiseURI") final String advertiseURI,
139             @PluginConfiguration final Configuration config) {
140 
141         final boolean isAppend = Booleans.parseBoolean(append, true);
142         final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
143         final boolean isBuffered = Booleans.parseBoolean(bufferedIO, true);
144         final boolean isFlush = Booleans.parseBoolean(immediateFlush, true);
145         final boolean isAdvertise = Boolean.parseBoolean(advertise);
146         if (name == null) {
147             LOGGER.error("No name provided for FileAppender");
148             return null;
149         }
150 
151         if (fileName == null) {
152             LOGGER.error("No filename was provided for FileAppender with name "  + name);
153             return null;
154         }
155 
156         if (filePattern == null) {
157             LOGGER.error("No filename pattern provided for FileAppender with name "  + name);
158             return null;
159         }
160 
161         if (policy == null) {
162             LOGGER.error("A TriggeringPolicy must be provided");
163             return null;
164         }
165 
166         if (strategy == null) {
167             strategy = DefaultRolloverStrategy.createStrategy(null, null, null,
168                     String.valueOf(Deflater.DEFAULT_COMPRESSION), config);
169         }
170 
171         if (layout == null) {
172             layout = PatternLayout.createLayout(null, null, null, null, null, null);
173         }
174 
175         final RollingFileManager manager = RollingFileManager.getFileManager(fileName, filePattern, isAppend,
176             isBuffered, policy, strategy, advertiseURI, layout);
177         if (manager == null) {
178             return null;
179         }
180 
181         return new RollingFileAppender(name, layout, filter, manager, fileName, filePattern,
182                 ignoreExceptions, isFlush, isAdvertise ? config.getAdvertiser() : null);
183     }
184 }