KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > PlainLoggingManager


1 /*
2  * eAdmin/OWX
3  * Copyright (C) 1996-2003 OWX-Project Team <owx-team@gmx.net>
4  */

5
6 /******************************** Package */
7 package com.raptus.owxv3;
8
9
10 /******************************** Imports */
11 import java.util.*;
12 import java.io.*;
13
14 /******************************** Raptus-Header */
15 /**
16  * This tiny loggingmanager is used mainly for test-purposes.
17  * It can redirect the login either to stdout (System.out.printlnn())
18  * or to a specified file.
19  *
20  * IMPORTANT: The FileWriter is not properly closed... Will fix this when having
21  * time.
22  *
23  * @see com.raptus.owxv3.TomcatLoggingManager TomcatLoggingManager
24  *
25  * <hr>
26  * <table width="100%" border="0">
27  * <tr>
28  * <td width="24%"><b>Filename</b></td><td width="76%">PlainLoggingManager.java</td>
29  * </tr>
30  * <tr>
31  * <td width="24%"><b>Author</b></td><td width="76%">Pascal Mainini (pmainini@raptus.com)</td>
32  * </tr>
33  * <tr>
34  * <td width="24%"><b>Date</b></td><td width="76%">03rd of April 2001</td>
35  * </tr>
36  * </table>
37  * <hr>
38  * <table width="100%" border="0">
39  * <tr>
40  * <td width="24%"><b>Date / Author</b></td><td width="76%"><b>Changes</b></td>
41  * </tr>
42  * <tr>
43  * <td width="24%">2001-04-03/pm</td><td width="76%">created</td>
44  * </tr>
45  * </table>
46  * <hr>
47  */

48 public class PlainLoggingManager extends Object JavaDoc implements LoggingManagerIFace
49 {
50
51 /******************************** Constants */
52     /**
53      * This constant specifies the prefix which identifies a key in the
54      * servlet-params-hash belonging to this class.
55      *
56      * <b>Change this only if you know exactly why!</b>
57      */

58     public static final String JavaDoc PARAM_PREFIX = "PlainLoggingManager.";
59
60     /**
61      * This is used for retrieving the key which contains the Tomcat-Logger to
62      * which we log.
63      *
64      * IF THE VALUE OF THE KEY IS "stdout" THE LOGS ARE NOT GENERATED IN A FILE
65      * BUT ON stdout VIA System.out.println()
66      */

67     public static final String JavaDoc LOGFILE_KEY = "LogFile";
68
69
70 /******************************** Members */
71     /**
72      * If true, we log to stdout, if false we do not...
73      */

74     protected boolean toStdOut = false;
75
76     /**
77      * This is the name of the logfile we log to...
78      */

79     protected String JavaDoc logFile = null;
80
81     /**
82      * This is the FileWriter we use to write a simple logfile.
83      */

84     protected FileWriter myWriter;
85
86
87 /******************************** Constructors */
88     /**
89      * The default constructor just does nothing....
90      */

91     public PlainLoggingManager()
92     {
93     }
94
95
96 /******************************** Methods */
97     /**
98      * @see com.raptus.owxv3.LoggingManagerIFace#initialize(Hashtable hash) Definition
99      */

100     public boolean initialize(Hashtable hash)
101     {
102         logFile = (String JavaDoc) hash.get(LoggingManager.PARAM_PREFIX + PARAM_PREFIX + LOGFILE_KEY);
103
104         if(logFile == null || logFile.length() == 0)
105             return false;
106
107         if(logFile.compareToIgnoreCase("stdout") == 0)
108         {
109             toStdOut = true;
110             return true;
111         }
112
113         try
114         {
115             myWriter = new FileWriter(logFile);
116         }
117         catch(Exception JavaDoc e)
118         {
119             System.out.println("Exception. " + e);
120             return false;
121         }
122
123         return true;
124     }
125
126     /**
127      * @see com.raptus.owxv3.LoggingManagerIFace#log(String msg) Definition
128      */

129     public void log(String JavaDoc msg)
130     {
131         String JavaDoc message = "UnknownComponent:" +
132                           LoggingManager.getBelongingLevelName(LoggingManager.IMPORTANCE_NORMAL) + ":" +
133                           msg;
134
135         if(LoggingManager.isToLog(LoggingManager.IMPORTANCE_NORMAL))
136             if(toStdOut)
137                 System.out.println(message);
138             else
139                 writeToFile(message);
140     }
141
142     /**
143      * @see com.raptus.owxv3.LoggingManagerIFace#log(String msg, int level) Definition
144      */

145     public void log(String JavaDoc msg, int level)
146     {
147         String JavaDoc message = "UnknownComponent:" + LoggingManager.getBelongingLevelName(level) +
148                          ":" + msg;
149
150         if(LoggingManager.isToLog(level))
151             if(toStdOut)
152                 System.out.println(message);
153             else
154                 writeToFile(message);
155     }
156
157     /**
158      * @see com.raptus.owxv3.LoggingManagerIFace#log(String msg, Object caller) Definition
159      */

160     public void log(String JavaDoc msg, Object JavaDoc caller)
161     {
162         String JavaDoc message = caller.getClass().getName() + ":" +
163                          LoggingManager.getBelongingLevelName(LoggingManager.IMPORTANCE_NORMAL) + ":" +
164                          msg;
165
166         if(LoggingManager.isToLog(LoggingManager.IMPORTANCE_NORMAL))
167             if(toStdOut)
168                 System.out.println(message);
169             else
170                 writeToFile(message);
171     }
172
173     /**
174      * @see com.raptus.owxv3.LoggingManagerIFace#log(String msg, Object caller, int level) Definition
175      */

176     public void log(String JavaDoc msg, Object JavaDoc caller, int level)
177     {
178         String JavaDoc message = caller.getClass().getName() + ":" + LoggingManager.getBelongingLevelName(level) +
179                          ":" + msg;
180
181         if(LoggingManager.isToLog(level))
182             if(toStdOut)
183                 System.out.println(message);
184             else
185                 writeToFile(message);
186     }
187
188     /**
189      * This method writes a Message to a file
190      *
191      * @param msg The message to write.
192      */

193     protected void writeToFile(String JavaDoc msg)
194     {
195         try
196         {
197             myWriter.write(msg + "\n");
198             myWriter.flush();
199         }
200         catch(Exception JavaDoc e)
201         {
202             System.out.println("Exception. " + e);
203         }
204     }
205 }
206
Popular Tags