KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > log4j > DayFileHandler


1 /**
2  * Copyright (C) 2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * 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  */

18 package org.objectweb.util.monolog.wrapper.log4j;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.text.DateFormat JavaDoc;
23 import java.util.Calendar JavaDoc;
24 import java.util.Date JavaDoc;
25
26 import org.apache.log4j.helpers.LogLog;
27 import org.apache.log4j.spi.LoggingEvent;
28
29 /**
30  * Represents an file handler which the file name changes every days.
31  * The file name is named such as this example:
32  * 2004_december_25_mylogfile.log
33  *
34  * @author S.Chassande-Barrioz
35  */

36 public class DayFileHandler extends FileHandler {
37
38     private int lastDay;
39
40     Calendar JavaDoc calendar = Calendar.getInstance();
41
42     public DayFileHandler() {
43         super();
44     }
45
46     public DayFileHandler(String JavaDoc name) {
47         super(name);
48     }
49
50     protected void subAppend(LoggingEvent event) {
51         super.subAppend(event);
52         int currentDay = calendar.get(Calendar.DATE);
53         if (fileName != null && currentDay != lastDay) {
54             this.closeWriter(); // keep windows happy.
55
//compute the name of the file from the current
56
//date: 2004_december_25_mylogfile.log
57
Date JavaDoc d = calendar.getTime();
58             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
59             sb.append("_");
60             sb.append(DateFormat.getDateInstance(
61                     DateFormat.YEAR_FIELD).format(d));
62             sb.append("_");
63             sb.append(DateFormat.getDateInstance(
64                     DateFormat.MONTH_FIELD).format(d));
65             sb.append("_");
66             sb.append(DateFormat.getDateInstance(
67                     DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD).format(d));
68             sb.append("_");
69             sb.append(fileName);
70             File JavaDoc file = new File JavaDoc(sb.toString());
71             try {
72                 // This will also close the file. This is OK since multiple
73
// close operations are safe.
74
this.setFile(fileName, false, bufferedIO, bufferSize);
75             } catch (IOException JavaDoc e) {
76                 LogLog.error("setFile(" + fileName + ", 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 }
87
Popular Tags