KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > util > logger > SimpleLogger


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/util/logger/SimpleLogger.java,v 1.12 2004/07/28 09:34:23 ib Exp $
3  * $Revision: 1.12 $
4  * $Date: 2004/07/28 09:34:23 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.util.logger;
25
26 import java.util.Date JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.text.SimpleDateFormat JavaDoc;
29
30 /**
31  * Simple logger implementation.
32  *
33  */

34 public class SimpleLogger implements Logger {
35     
36     
37     // ----------------------------------------------------- Instance Variables
38

39     
40     /**
41      * Logging level of the logger.
42      */

43     protected int loggerLevel = 0;
44     
45     
46     /**
47      * Date / Time format.
48      */

49     protected SimpleDateFormat JavaDoc dateFormat =
50         new SimpleDateFormat JavaDoc("dd MMM yyyy HH:mm:ss", Locale.US);
51     
52     
53     /**
54      * Text values for logging priorities.
55      */

56     protected String JavaDoc[] loggingLevels = { "EMERGENCY", "CRITICAL", "ERROR",
57                                          "", "WARNING", "", "INFO", "DEBUG" };
58     
59     
60     // ------------------------------------------------------------- Properties
61

62     
63     /**
64      * Logger level setter.
65      *
66      * @param loggerLevel New logger level
67      */

68     public void setLoggerLevel(String JavaDoc channel, int loggerLevel) {
69         this.loggerLevel = loggerLevel;
70     }
71     
72     
73     /**
74      * Logger level setter.
75      *
76      * @param loggerLevel New logger level
77      */

78     public void setLoggerLevel(int loggerLevel) {
79         setLoggerLevel(DEFAULT_CHANNEL, loggerLevel);
80     }
81     
82     
83     /**
84      * Logger level getter.
85      *
86      * @return int logger level
87      */

88     public int getLoggerLevel() {
89         return getLoggerLevel(DEFAULT_CHANNEL);
90     }
91     
92     
93     
94     /**
95      * Logger level getter.
96      *
97      * @return int logger level
98      */

99     public int getLoggerLevel(String JavaDoc channel) {
100         return loggerLevel;
101     }
102     
103     
104     /**
105      * Date format setter.
106      *
107      * @param pattern Format pattern
108      */

109     public void setDateFormat(String JavaDoc pattern) {
110         dateFormat = new SimpleDateFormat JavaDoc(pattern);
111     }
112     
113     
114     // --------------------------------------------------------- Logger Methods
115

116     
117     /**
118      * Log an object and an associated throwable thru the specified channel and with the specified level.
119      *
120      * @param data object to log
121      * @param throwable throwable to be logged
122      * @param channel channel name used for logging
123      * @param level level used for logging
124      */

125     public void log(Object JavaDoc data, Throwable JavaDoc throwable, String JavaDoc channel, int level) {
126         if (isEnabled(channel, level)) {
127             String JavaDoc levelValue = "";
128             if (channel.equals(DEFAULT_CHANNEL))
129                 channel = "";
130             else
131                 channel = channel + " - ";
132             if ((level >= 0) && (level < loggingLevels.length))
133                 levelValue = loggingLevels[level];
134             if (dateFormat == null) {
135                 System.out.println(System.currentTimeMillis() + " - "
136                                    + channel + levelValue + " - "
137                                    + data);
138             } else {
139                 System.out.println(dateFormat.format(new Date JavaDoc()) + " - "
140                                    + channel + levelValue + " - "
141                                    + data);
142             }
143             throwable.printStackTrace();
144         }
145     }
146
147     
148     /**
149      * Log an object thru the specified channel and with the specified level.
150      *
151      * @param data The object to log.
152      * @param channel The channel name used for logging.
153      * @param level The level used for logging.
154      */

155     public void log(Object JavaDoc data, String JavaDoc channel, int level) {
156         if (isEnabled(channel, level)) {
157             String JavaDoc levelValue = "";
158             if (channel.equals(DEFAULT_CHANNEL))
159                 channel = "";
160             else
161                 channel = channel + " - ";
162             if ((level >= 0) && (level < loggingLevels.length))
163                 levelValue = loggingLevels[level];
164             if (dateFormat == null) {
165                 System.out.println(System.currentTimeMillis() + " - "
166                                    + channel + levelValue + " - "
167                                    + data);
168             } else {
169                 System.out.println(dateFormat.format(new Date JavaDoc()) + " - "
170                                    + channel + levelValue + " - "
171                                    + data);
172             }
173             if (data instanceof Throwable JavaDoc)
174                 ((Throwable JavaDoc) data).printStackTrace();
175         }
176     }
177     
178     
179     /**
180      * Log an object with the specified level.
181      *
182      * @param data The object to log.
183      * @param level The level used for logging.
184      */

185     public void log(Object JavaDoc data, int level) {
186         this.log(data, DEFAULT_CHANNEL, level);
187     }
188     
189     
190     /**
191      * Log an object.
192      *
193      * @param data The object to log.
194      */

195     public void log(Object JavaDoc data) {
196         this.log(data, DEFAULT_CHANNEL, Logger.DEBUG);
197     }
198     
199             
200     
201         
202     
203     /**
204      * Check if the channel with the specified level is enabled for logging.
205      * This implementation ignores the channel specification
206      *
207      * @param channel The channel specification
208      * @param level The level specification
209      */

210
211     public boolean isEnabled(String JavaDoc channel, int level) {
212         return getLoggerLevel() >= level;
213     }
214             
215         
216     
217     /**
218      * Check if the default channel with the specified level is enabled for logging.
219      *
220      * @param level The level specification
221      */

222
223     public boolean isEnabled(int level) {
224         return isEnabled(DEFAULT_CHANNEL, level);
225     }
226
227
228 }
229
Popular Tags