01: /**
02: * Copyright (C) 2004 France Telecom R&D
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */package org.objectweb.util.monolog.wrapper.log4j;
18:
19: import java.io.File;
20: import java.io.IOException;
21: import java.text.DateFormat;
22: import java.util.Calendar;
23: import java.util.Date;
24:
25: import org.apache.log4j.helpers.LogLog;
26: import org.apache.log4j.spi.LoggingEvent;
27:
28: /**
29: * Represents an file handler which the file name changes every days.
30: * The file name is named such as this example:
31: * 2004_december_25_mylogfile.log
32: *
33: * @author S.Chassande-Barrioz
34: */
35: public class DayFileHandler extends FileHandler {
36:
37: private int lastDay;
38:
39: Calendar calendar = Calendar.getInstance();
40:
41: public DayFileHandler() {
42: super ();
43: }
44:
45: public DayFileHandler(String name) {
46: super (name);
47: }
48:
49: protected void subAppend(LoggingEvent event) {
50: super .subAppend(event);
51: int currentDay = calendar.get(Calendar.DATE);
52: if (fileName != null && currentDay != lastDay) {
53: this .closeWriter(); // keep windows happy.
54: //compute the name of the file from the current
55: //date: 2004_december_25_mylogfile.log
56: Date d = calendar.getTime();
57: StringBuffer sb = new StringBuffer();
58: sb.append("_");
59: sb.append(DateFormat.getDateInstance(DateFormat.YEAR_FIELD)
60: .format(d));
61: sb.append("_");
62: sb.append(DateFormat
63: .getDateInstance(DateFormat.MONTH_FIELD).format(d));
64: sb.append("_");
65: sb.append(DateFormat.getDateInstance(
66: DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD).format(d));
67: sb.append("_");
68: sb.append(fileName);
69: File file = new File(sb.toString());
70: try {
71: // This will also close the file. This is OK since multiple
72: // close operations are safe.
73: this .setFile(fileName, false, bufferedIO, bufferSize);
74: } catch (IOException e) {
75: LogLog.error("setFile(" + fileName
76: + ", false) call failed.", e);
77: }
78: lastDay = currentDay;
79: }
80: }
81:
82: public void activateOptions() {
83: lastDay = calendar.get(Calendar.DATE);
84: super.activateOptions();
85: }
86: }
|