1 package org.apache.turbine.services.logging;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57
58 /***
59 * This is an abbstract class that implements Logger interface.
60 * User implementation has to redefine only five methods that can
61 * handle different type of destinations.
62 *
63 * @author <a href="mailto:Tomasz.Zielinski@e-point.pl">Tomasz Zielinski</a>
64 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
65 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
66 * @version $Id: BaseLogger.java,v 1.2 2002/07/11 16:53:26 mpoeschl Exp $
67 */
68 public abstract class BaseLogger
69 implements Logger
70 {
71 /*** Current log level for logger */
72 protected int logLevel;
73
74 /*** Name of the logger */
75 protected String name;
76
77 /*** Store status of the logger */
78 protected boolean initialize;
79
80 /*** Extracts data from RunData */
81 protected RunDataFilter runDataFilter = null;
82
83 /*** The LoggingConfig object for this logger */
84 protected LoggingConfig loggingConfig = null;
85
86 /*** flag is set when console writing is allowed */
87 protected boolean console = false;
88
89 /***
90 * Default Constructor
91 */
92 public BaseLogger()
93 {
94 logLevel = DEBUG;
95 name = null;
96 initialize = false;
97 runDataFilter = new BaseRunDataFilter();
98 }
99
100 /***
101 * This method should be reimplemented by user if class use need
102 * some objects intialization.
103 *
104 * @param loggingConfig Configuration describing the logger.
105 */
106 public void init(LoggingConfig loggingConfig)
107 {
108 this.loggingConfig = loggingConfig;
109 doBaseInit(loggingConfig);
110
111 // Initialization of objects goes here
112 doDispatch(loggingConfig);
113 }
114
115 /***
116 * Starts configuration of the logger, sets name, format, loging level
117 * (defaults to <code>DEBUG</code>).
118 *
119 * @param loggingConfig Configuration describing the logger.
120 */
121 private void doBaseInit(LoggingConfig loggingConfig)
122 {
123 setName(loggingConfig.getName());
124 setLogLevel(loggingConfig.getLevel());
125 setFormat(loggingConfig.getFormat());
126 }
127
128 /***
129 * Dispatches tasks for different types of destinations.
130 * Checks if the logger were configure properly
131 *
132 * @param loggingConfig Configuration describing the logger.
133 */
134 protected void doDispatch(LoggingConfig loggingConfig)
135 {
136 configureFiles(loggingConfig);
137 configureConsole(loggingConfig);
138 configureRemote(loggingConfig);
139 configureSyslog(loggingConfig);
140 configureEmail(loggingConfig);
141 configureDatabase(loggingConfig);
142
143 //chcecking configuration
144 initialize=checkLogger();
145 }
146
147 /*** Returns logger's name */
148 public String getName()
149 {
150 return name;
151 }
152
153 /*** Sets the name of the logger */
154 public void setName(String logName)
155 {
156 name = logName;
157 }
158
159 /***
160 * Sets format output of the <code>Rundata</code>.
161 * Format style is defined in the BaseRunDataFilter class.
162 *
163 * @see org.apache.turbine.services.logging.BaseRunDataFilter
164 *
165 * @param format Text describing which data should be extracted from
166 * RunData
167 */
168 public void setFormat(String format)
169 {
170 runDataFilter.setFormat(format);
171 }
172
173 /***
174 * Sets the logging level based on the text passed in. Uses reasonable
175 * defaults if passed bogus data. Delegates to
176 * <code>setLogLevel(int)</code>.
177 *
178 * @param level The logging level represented as text.
179 */
180 protected void setLogLevel(String level)
181 {
182 if (level != null && !level.trim().equals(""))
183 {
184 int newLevel = DEBUG;
185 level = level.toUpperCase();
186 if (level.equals(LEVELINFO))
187 {
188 newLevel = INFO;
189 }
190 else if (level.equals(LEVELWARN))
191 {
192 newLevel = WARN;
193 }
194 else if (level.equals(LEVELERROR))
195 {
196 newLevel = ERROR;
197 }
198
199 setLogLevel(newLevel);
200 }
201 }
202
203 /***
204 * Sets the logging level based on the numeric level passed in. Uses
205 * reasonable defaults if passed bogus data.
206 *
207 * @param level The logging level.
208 */
209 public void setLogLevel(int level)
210 {
211 if (level < DEBUG)
212 {
213 level = DEBUG;
214 }
215 else if (level > ERROR)
216 {
217 level = ERROR;
218 }
219
220 logLevel = level;
221 }
222
223 /***
224 * Checks if DEBUG statements are enabled.
225 */
226 public boolean isDebugEnabled()
227 {
228 return (logLevel == DEBUG);
229 }
230
231 /***
232 * Checks if INFO statements are enabled.
233 */
234 public boolean isInfoEnabled()
235 {
236 return (logLevel <= INFO);
237 }
238
239 /***
240 * Checks if WARN statements are enabled.
241 */
242 public boolean isWarnEnabled()
243 {
244 return (logLevel <= WARN);
245 }
246
247 /***
248 * Checks if ERROR statements are enabled.
249 */
250 public boolean isErrorEnabled()
251 {
252 return (logLevel <= ERROR);
253 }
254
255 /***
256 * This method should be implemented by user if the logger can handle files.
257 * It adds local file as destinations for logger.
258 *
259 * @param LoggingConfig configuration
260 */
261 protected void configureFiles(LoggingConfig loggingConfig)
262 {
263 }
264
265 /***
266 * This method should be implemented by user if the logger can handle
267 * remote server. It adds remote servers as destinations for logger.
268 *
269 * @param loggingConfig Configuration describing the logger.
270 */
271 protected void configureConsole(LoggingConfig loggingConfig)
272 {
273 }
274
275 /***
276 * This method should be implemented by user if the logger can handle
277 * console. It adds console as a destination for logger.
278 *
279 * @param loggingConfig Configuration describing the logger.
280 */
281 protected void configureRemote(LoggingConfig loggingConfig)
282 {
283 }
284
285 /***
286 * This method should be implemented by user if the logger can handle
287 * syslog demon. It adds syslog demon as destinations for logger.
288 *
289 * @param loggingConfig Configuration describing the logger.
290 */
291 protected void configureSyslog(LoggingConfig loggingConfig)
292 {
293 }
294
295 /***
296 * This method should be implemented by user if the logger can handle
297 * emailing logs. It adds email as a destination for logger.
298 *
299 * @param loggingConfig Configuration describing the logger.
300 */
301 protected void configureEmail(LoggingConfig loggingConfig)
302 {
303 }
304
305 /***
306 * This method should be implemented by user if the logger can handle
307 * database logs. It adds a db as a destination for logger.
308 *
309 * @param loggingConfig Configuration describing the logger.
310 */
311 protected void configureDatabase(LoggingConfig loggingConfig)
312 {
313 }
314
315 /***
316 * This method should be implemented by user.
317 * It performs action that are need for deterimne whether
318 * logger was well configured or has any output
319 */
320 public abstract boolean checkLogger();
321 }
This page was automatically generated by Maven