KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > util > Logger


1 package org.ashkelon.util;
2 /**
3  * Copyright UptoData Inc. 2001
4  * March 2001
5  */

6
7 import java.io.PrintWriter JavaDoc;
8
9 /**
10  * Basic facility for generating manageable log information for a program.
11  * Provides 5 different logging (or tracing) levels and the ability to
12  * filter log messages above a specified trace level. This class is a
13  * singleton.
14  *
15  * @author Eitan Suez
16  */

17 public class Logger
18 {
19    public static final int ERROR = 10;
20    public static final int BRIEF = 20;
21    public static final int NORMAL = 30;
22    public static final int VERBOSE = 40;
23    public static final int FULL = 50;
24    public static final int DEBUG = 60;
25    
26    private static Logger _instance = null;
27    private int traceLevel;
28    private PrintWriter JavaDoc writer, writer2;
29    private String JavaDoc prefix;
30    
31    private Logger()
32    {
33       traceLevel = NORMAL;
34       setWriter(new PrintWriter JavaDoc(System.out));
35       setWriter2(null);
36       setPrefix("");
37    }
38    
39    public static Logger getInstance()
40    {
41       if (_instance == null)
42       {
43          _instance = new Logger();
44       }
45       return _instance;
46    }
47    
48    /**
49     * set the trace level
50     */

51    public void setTraceLevel(int traceLevel) { this.traceLevel = traceLevel; }
52    public int getTraceLevel() { return traceLevel; }
53    
54    public void setPrefix(String JavaDoc prefix) { this.prefix = StringUtils.avoidNull(prefix); }
55    public String JavaDoc getPrefix() { return prefix; }
56    
57    /**
58     * set the print writer to send log output to
59     */

60    public void setWriter(PrintWriter JavaDoc writer)
61    {
62       if (this.writer != null)
63       {
64          //try
65
//{
66
this.writer.close();
67          //} catch (IOException ex)
68
//{}
69
}
70       this.writer = writer;
71    }
72    public PrintWriter JavaDoc getWriter() { return writer; }
73    
74    /**
75     * optionally set a second print writer to send log output to
76     * (e.g. log to both a file and to stdout)
77     */

78    public void setWriter2(PrintWriter JavaDoc writer2)
79    {
80       if (this.writer2 != null)
81       {
82          //try
83
//{
84
this.writer2.close();
85          //} catch (IOException ex)
86
//{}
87
}
88       this.writer2 = writer2;
89    }
90    public PrintWriter JavaDoc getWriter2() { return writer2; }
91    
92    /**
93     * trace text (no carriage return)
94     */

95    public void trace(String JavaDoc text)
96    {
97       trace(text, traceLevel);
98    }
99    /**
100     * trace text with carriage return
101     */

102    public void traceln(String JavaDoc text)
103    {
104       trace(text + "\n");
105    }
106    
107    /**
108     * trace text with given traceLevel
109     */

110    public void trace(String JavaDoc text, int traceLevel)
111    {
112       if (traceLevel <= this.traceLevel)
113       {
114          if (!StringUtils.isBlank(prefix))
115          {
116             text = prefix + ": " + text;
117          }
118          if (writer != null)
119          {
120             writer.print(text);
121             writer.flush();
122          }
123          if (writer2 != null)
124          {
125             writer2.print(text);
126             writer2.flush();
127          }
128          if (traceLevel==ERROR)
129          { // will show duplicate message if writer or writer 2 are system.err
130
System.err.print(text);
131             System.err.flush();
132          }
133          
134       }
135    }
136    
137    /**
138     * trace text (with carriage return) using given trace level
139     */

140    public void traceln(String JavaDoc text, int traceLevel)
141    {
142       trace(text + "\n", traceLevel);
143    }
144    
145    // facility methods
146

147    public void error(String JavaDoc text)
148    {
149       traceln(text, ERROR);
150    }
151    public void brief(String JavaDoc text)
152    {
153       traceln(text, BRIEF);
154    }
155    public void verbose(String JavaDoc text)
156    {
157       traceln(text, VERBOSE);
158    }
159    public void full(String JavaDoc text)
160    {
161       traceln(text, FULL);
162    }
163    public void debug(String JavaDoc text)
164    {
165       traceln(text, DEBUG);
166    }
167    
168    
169 }
170
Popular Tags