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