View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.rolling;
15  
16  import static ch.qos.logback.core.CoreConstants.CODES_URL;
17  import static ch.qos.logback.core.CoreConstants.MORE_INFO_PREFIX;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.util.Map;
22  import java.util.Map.Entry;
23  
24  import ch.qos.logback.core.CoreConstants;
25  import ch.qos.logback.core.FileAppender;
26  import ch.qos.logback.core.rolling.helper.CompressionMode;
27  import ch.qos.logback.core.rolling.helper.FileNamePattern;
28  import ch.qos.logback.core.util.ContextUtil;
29  
30  /**
31   * <code>RollingFileAppender</code> extends {@link FileAppender} to backup the
32   * log files depending on {@link RollingPolicy} and {@link TriggeringPolicy}.
33   * 
34   * <p>
35   * For more information about this appender, please refer to the online manual
36   * at http://logback.qos.ch/manual/appenders.html#RollingFileAppender
37   *
38   * @author Heinz Richter
39   * @author Ceki G&uuml;lc&uuml;
40   */
41  public class RollingFileAppender<E> extends FileAppender<E> {
42      File currentlyActiveFile;
43      TriggeringPolicy<E> triggeringPolicy;
44      RollingPolicy rollingPolicy;
45  
46      static private String RFA_NO_TP_URL = CODES_URL + "#rfa_no_tp";
47      static private String RFA_NO_RP_URL = CODES_URL + "#rfa_no_rp";
48      static private String COLLISION_URL = CODES_URL + "#rfa_collision";
49      static private String RFA_LATE_FILE_URL = CODES_URL + "#rfa_file_after";
50  
51      public void start() {
52          if (triggeringPolicy == null) {
53              addWarn("No TriggeringPolicy was set for the RollingFileAppender named " + getName());
54              addWarn(MORE_INFO_PREFIX + RFA_NO_TP_URL);
55              return;
56          }
57          if (!triggeringPolicy.isStarted()) {
58              addWarn("TriggeringPolicy has not started. RollingFileAppender will not start");
59              return;
60          }
61  
62          if (checkForCollisionsInPreviousRollingFileAppenders()) {
63              addError("Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting.");
64              addError(MORE_INFO_PREFIX + COLLISION_WITH_EARLIER_APPENDER_URL);
65              return;
66          }
67  
68          // we don't want to void existing log files
69          if (!append) {
70              addWarn("Append mode is mandatory for RollingFileAppender. Defaulting to append=true.");
71              append = true;
72          }
73  
74          if (rollingPolicy == null) {
75              addError("No RollingPolicy was set for the RollingFileAppender named " + getName());
76              addError(MORE_INFO_PREFIX + RFA_NO_RP_URL);
77              return;
78          }
79  
80          // sanity check for http://jira.qos.ch/browse/LOGBACK-796
81          if (checkForFileAndPatternCollisions()) {
82              addError("File property collides with fileNamePattern. Aborting.");
83              addError(MORE_INFO_PREFIX + COLLISION_URL);
84              return;
85          }
86  
87          if (isPrudent()) {
88              if (rawFileProperty() != null) {
89                  addWarn("Setting \"File\" property to null on account of prudent mode");
90                  setFile(null);
91              }
92              if (rollingPolicy.getCompressionMode() != CompressionMode.NONE) {
93                  addError("Compression is not supported in prudent mode. Aborting");
94                  return;
95              }
96          }
97  
98          currentlyActiveFile = new File(getFile());
99          addInfo("Active log file name: " + getFile());
100         super.start();
101     }
102 
103     private boolean checkForFileAndPatternCollisions() {
104         if (triggeringPolicy instanceof RollingPolicyBase) {
105             final RollingPolicyBase base = (RollingPolicyBase) triggeringPolicy;
106             final FileNamePattern fileNamePattern = base.fileNamePattern;
107             // no use checking if either fileName or fileNamePattern are null
108             if (fileNamePattern != null && fileName != null) {
109                 String regex = fileNamePattern.toRegex();
110                 return fileName.matches(regex);
111             }
112         }
113         return false;
114     }
115 
116     private boolean checkForCollisionsInPreviousRollingFileAppenders() {
117         boolean collisionResult = false;
118         if (triggeringPolicy instanceof RollingPolicyBase) {
119             final RollingPolicyBase base = (RollingPolicyBase) triggeringPolicy;
120             final FileNamePattern fileNamePattern = base.fileNamePattern;
121             boolean collisionsDetected = innerCheckForFileNamePatternCollisionInPreviousRFA(fileNamePattern);
122             if (collisionsDetected)
123                 collisionResult = true;
124         }
125         return collisionResult;
126     }
127 
128     private boolean innerCheckForFileNamePatternCollisionInPreviousRFA(FileNamePattern fileNamePattern) {
129         boolean collisionsDetected = false;
130         @SuppressWarnings("unchecked")
131         Map<String, FileNamePattern> map = (Map<String, FileNamePattern>) context
132                 .getObject(CoreConstants.RFA_FILENAME_PATTERN_COLLISION_MAP);
133         if (map == null) {
134             return collisionsDetected;
135         }
136         for (Entry<String, FileNamePattern> entry : map.entrySet()) {
137             if (fileNamePattern.equals(entry.getValue())) {
138                 addErrorForCollision("FileNamePattern", entry.getValue().toString(), entry.getKey());
139                 collisionsDetected = true;
140             }
141         }
142         if (name != null) {
143             map.put(getName(), fileNamePattern);
144         }
145         return collisionsDetected;
146     }
147 
148     @Override
149     public void stop() {
150         super.stop();
151 
152         if (rollingPolicy != null)
153             rollingPolicy.stop();
154         if (triggeringPolicy != null)
155             triggeringPolicy.stop();
156 
157         Map<String, FileNamePattern> map = ContextUtil.getFilenamePatternCollisionMap(context);
158         if (map != null && getName() != null)
159             map.remove(getName());
160 
161     }
162 
163     @Override
164     public void setFile(String file) {
165         // http://jira.qos.ch/browse/LBCORE-94
166         // allow setting the file name to null if mandated by prudent mode
167         if (file != null && ((triggeringPolicy != null) || (rollingPolicy != null))) {
168             addError("File property must be set before any triggeringPolicy or rollingPolicy properties");
169             addError(MORE_INFO_PREFIX + RFA_LATE_FILE_URL);
170         }
171         super.setFile(file);
172     }
173 
174     @Override
175     public String getFile() {
176         return rollingPolicy.getActiveFileName();
177     }
178 
179     /**
180      * Implemented by delegating most of the rollover work to a rolling policy.
181      */
182     public void rollover() {
183         lock.lock();
184         try {
185             // Note: This method needs to be synchronized because it needs exclusive
186             // access while it closes and then re-opens the target file.
187             //
188             // make sure to close the hereto active log file! Renaming under windows
189             // does not work for open files.
190             this.closeOutputStream();
191             attemptRollover();
192             attemptOpenFile();
193         } finally {
194             lock.unlock();
195         }
196     }
197 
198     private void attemptOpenFile() {
199         try {
200             // update the currentlyActiveFile LOGBACK-64
201             currentlyActiveFile = new File(rollingPolicy.getActiveFileName());
202 
203             // This will also close the file. This is OK since multiple close operations are
204             // safe.
205             this.openFile(rollingPolicy.getActiveFileName());
206         } catch (IOException e) {
207             addError("setFile(" + fileName + ", false) call failed.", e);
208         }
209     }
210 
211     private void attemptRollover() {
212         try {
213             rollingPolicy.rollover();
214         } catch (RolloverFailure rf) {
215             addWarn("RolloverFailure occurred. Deferring roll-over.");
216             // we failed to roll-over, let us not truncate and risk data loss
217             this.append = true;
218         }
219     }
220 
221     /**
222      * This method differentiates RollingFileAppender from its super class.
223      */
224     @Override
225     protected void subAppend(E event) {
226         // The roll-over check must precede actual writing. This is the
227         // only correct behavior for time driven triggers.
228 
229         // We need to synchronize on triggeringPolicy so that only one rollover
230         // occurs at a time
231         synchronized (triggeringPolicy) {
232             if (triggeringPolicy.isTriggeringEvent(currentlyActiveFile, event)) {
233                 rollover();
234             }
235         }
236 
237         super.subAppend(event);
238     }
239 
240     public RollingPolicy getRollingPolicy() {
241         return rollingPolicy;
242     }
243 
244     public TriggeringPolicy<E> getTriggeringPolicy() {
245         return triggeringPolicy;
246     }
247 
248     /**
249      * Sets the rolling policy. In case the 'policy' argument also implements
250      * {@link TriggeringPolicy}, then the triggering policy for this appender is
251      * automatically set to be the policy argument.
252      *
253      * @param policy
254      */
255     @SuppressWarnings("unchecked")
256     public void setRollingPolicy(RollingPolicy policy) {
257         rollingPolicy = policy;
258         if (rollingPolicy instanceof TriggeringPolicy) {
259             triggeringPolicy = (TriggeringPolicy<E>) policy;
260         }
261 
262     }
263 
264     public void setTriggeringPolicy(TriggeringPolicy<E> policy) {
265         triggeringPolicy = policy;
266         if (policy instanceof RollingPolicy) {
267             rollingPolicy = (RollingPolicy) policy;
268         }
269     }
270 }