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