KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > util > Log


1 /**
2  * com.mckoi.util.Log 09 Jun 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.util;
26
27 import java.io.*;
28 import java.text.*;
29 import java.util.Date JavaDoc;
30
31 /**
32  * A log file/stream that logs some information generated by the system.
33  * This is intended to help with debugging. It safely handles concurrent
34  * output to the log.
35  *
36  * @author Tobias Downer
37  */

38
39 public class Log {
40
41   /**
42    * The output stream where log information is output to.
43    */

44   private final LogWriter log_output;
45
46   /**
47    * Date formatter.
48    */

49   private final DateFormat date_format = DateFormat.getDateTimeInstance();
50
51
52   public Log(String JavaDoc path) throws FileNotFoundException, IOException {
53     this(new File(path));
54   }
55
56   public Log(File file, int size, int max_count) throws IOException {
57     this.log_output = new LogWriter(file, size, max_count);
58   }
59
60   public Log(File file) throws FileNotFoundException, IOException {
61     // Defaults to a maximum of 12 512k log files
62
this(file, 512 * 1024, 12);
63 // this.log_output = new LogWriter(file, 512 * 1024, 12);
64
}
65
66   protected Log() {
67     log_output = null;
68   }
69
70   /**
71    * Writes an entry to the log file. The log file records the time the entry
72    * was put into the log, and the string which is the log.
73    */

74   public synchronized void log(String JavaDoc text) {
75     try {
76       log_output.write("[");
77       log_output.write(date_format.format(new Date JavaDoc()));
78       log_output.write("] ");
79       log_output.write(text);
80       log_output.flush();
81     }
82     catch (IOException e) {}
83   }
84
85   public synchronized void logln(String JavaDoc text) {
86     try {
87       log_output.write(text);
88       log_output.write('\n');
89       log_output.flush();
90     }
91     catch (IOException e) {}
92   }
93
94   /**
95    * Closes the log file.
96    */

97   public synchronized void close() {
98     try {
99       log_output.close();
100     }
101     catch (IOException e) {}
102   }
103
104   // ---------- Static methods ----------
105

106   /**
107    * Returns a Log that won't actually store a log. This is useful for
108    * options where the user doesn't want anything logged.
109    */

110   public static Log nullLog() {
111     return new NullLog();
112   }
113
114   // ---------- Inner classes ----------
115

116 }
117
118 /**
119  * An implementation of Log that doesn't log anything.
120  */

121 class NullLog extends Log {
122
123   public NullLog() {
124     super();
125   }
126
127   public void log(String JavaDoc text) {
128     // Don't do anything,
129
}
130   public void logln(String JavaDoc text) {
131     // Don't do anything,
132
}
133   public void close() {
134     // Don't do anything,
135
}
136
137 }
138
139
Popular Tags