KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > connectionpool > LogWriter


1 /*
2  * LogWriter is part of the Cofax content management system library.
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.1 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  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/connectionpool/LogWriter.java,v 1.2.2.1 2006/12/11 16:33:22 fxrobin Exp $
21  */

22
23 /*
24  * LogWriter.java 12/13/2000
25  * Provides log writing routines.
26  * Originated from code from Wrox Java book.
27  *
28  */

29 package org.cofax.connectionpool;
30
31 import java.io.*;
32 import java.util.*;
33
34 public class LogWriter {
35     public static final int NONE = 0;
36
37     public static final int ERROR = 1;
38
39     public static final int INFO = 2;
40
41     public static final int DEBUG = 3;
42
43     private static final String JavaDoc ERROR_TEXT = "error";
44
45     private static final String JavaDoc INFO_TEXT = "info";
46
47     private static final String JavaDoc DEBUG_TEXT = "debug";
48
49     private PrintWriter pw;
50
51     private String JavaDoc owner;
52
53     private int logLevel;
54
55     public LogWriter(String JavaDoc owner, int logLevel, PrintWriter pw) {
56         this.pw = pw;
57         this.owner = owner;
58         this.logLevel = logLevel;
59     }
60
61     public LogWriter(String JavaDoc owner, int logLevel) {
62         this(owner, logLevel, null);
63     }
64
65     public int getLogLevel() {
66         return logLevel;
67     }
68
69     public PrintWriter getPrintWriter() {
70         return pw;
71     }
72
73     public void setLogLevel(int logLevel) {
74         this.logLevel = logLevel;
75     }
76
77     public void setPrintWriter(PrintWriter pw) {
78         this.pw = pw;
79     }
80
81     public void log(String JavaDoc msg, int severityLevel) {
82         if (pw != null) {
83             if (severityLevel <= logLevel) {
84                 pw.println("[" + new Date() + "] " + getSeverityString(severityLevel) + ": " + owner + ": " + msg);
85             }
86         }
87     }
88
89     public void log(Throwable JavaDoc t, String JavaDoc msg, int severityLevel) {
90         log(msg + " : " + toTrace(t), severityLevel);
91     }
92
93     private String JavaDoc getSeverityString(int severityLevel) {
94         switch (severityLevel) {
95         case ERROR:
96             return ERROR_TEXT;
97         case INFO:
98             return INFO_TEXT;
99         case DEBUG:
100             return DEBUG_TEXT;
101         default:
102             return "Unknown";
103         }
104     }
105
106     private String JavaDoc toTrace(Throwable JavaDoc e) {
107         StringWriter sw = new StringWriter();
108         PrintWriter pw = new PrintWriter(sw);
109         e.printStackTrace(pw);
110         pw.flush();
111         return sw.toString();
112     }
113 }
114
Popular Tags