KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > util > logger > jdk14 > LoggerConfiguration


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/jdk14/org/apache/slide/util/logger/jdk14/LoggerConfiguration.java,v 1.2 2004/07/28 09:38:51 ib Exp $
3  * $Revision: 1.2 $
4  * $Date: 2004/07/28 09:38:51 $
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.jdk14;
25
26 import java.util.logging.ConsoleHandler JavaDoc;
27 import java.util.logging.Formatter JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.FileHandler JavaDoc;
31 import java.util.logging.Handler JavaDoc;
32
33 import java.io.*;
34
35 /**
36  * Configures Java 1.4 logger.
37  *
38  */

39 public class LoggerConfiguration {
40
41     private final static Logger JavaDoc logger = Logger.getLogger(LoggerConfiguration.class.getName());
42
43     public final static String JavaDoc DEFAULT_LOG_FILE = "Slide.log";
44     public final static Level JavaDoc DEFAULT_LOG_LEVEL = Level.ALL;
45     public final static String JavaDoc DEFAULT_FORMAT_STRING = SimpleLogfileFormatter.SIMPLE_FORMAT_STRING;
46     // new LogfileFormatter(DEFAULT_FORMAT_STRING));
47
public final static Formatter JavaDoc DEFAULT_FORMATTER = new SimpleLogfileFormatter(DEFAULT_FORMAT_STRING);
48     public final static Formatter JavaDoc DETAIL_FORMATTER = new LogfileFormatter(DEFAULT_FORMAT_STRING);
49
50     private static boolean isConfigured = false;
51
52     public final static void configure() {
53         configure(DEFAULT_LOG_FILE, DEFAULT_LOG_LEVEL, Level.INFO);
54     }
55
56     /**
57      * Configures standard Java1.4 logger
58      *
59      */

60     public final static void configure(String JavaDoc logFilePath, Level JavaDoc logLevel, Level JavaDoc logToConsoleThreshold) {
61         try {
62             if (isConfigured)
63                 return;
64             isConfigured = true;
65             // XXX first set new formatter to (default) console handler(s)
66
Handler JavaDoc[] handlers = Logger.getLogger("").getHandlers();
67             for (int i = 0; i < handlers.length; i++) {
68                 Handler JavaDoc handler = handlers[i];
69                 if (handler instanceof ConsoleHandler JavaDoc) {
70                     handler.setFormatter(new ConsoleLogfileFormatter());
71                     handler.setLevel(logToConsoleThreshold);
72                     // could exit loop here, but keep on going for robustness (might be more than on handler)
73
}
74             }
75             // set default logging
76
Logger.getLogger("").setLevel(logLevel);
77             Handler JavaDoc fileHandler = new FileHandler JavaDoc(logFilePath);
78             fileHandler.setFormatter(DETAIL_FORMATTER);
79             Logger.getLogger("").addHandler(fileHandler);
80             logger.log(Level.INFO, "Logging Service configured");
81         } catch (IOException ioe) {
82             logger.log(Level.WARNING, "Could not set logging configuration", ioe);
83         }
84     }
85 }
86
Popular Tags