KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > logging > Logging


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.logging;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.util.logging.ConsoleHandler JavaDoc;
21 import java.util.logging.FileHandler JavaDoc;
22 import java.util.logging.Handler JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import java.util.logging.SimpleFormatter JavaDoc;
26
27 import org.columba.ristretto.log.RistrettoLogger;
28
29 /**
30  * Depending on the debug flag (--debug command line option reflected in
31  * MainInterface.DEBUG) the logger will either show all debug messages or just
32  * severe errors. Logging information is passed to a log file and to the
33  * console.
34  * <p>
35  * Note, that Logging must not be called before MainInterface.DEBUG, was set.
36  * Otherwise, the logger won't show the correct debug level.
37  * <p>
38  * If the user has defined their own logging config file, then this will take
39  * precedence over Columba defined logging handlers, ie. Columba will not create
40  * its own default logging handlers. All has already been defined in the system
41  * property <b>java.util.logging.config.file</b>.
42  * <p>
43  *
44  * @see org.columba.core.main.Main
45  * @see java.util.logging.Logger
46  * @author redsolo
47  */

48 public final class Logging {
49
50     private static final Logger JavaDoc LOG = Logger.getLogger("org.columba");
51
52     private static ConsoleHandler JavaDoc consoleHandler;
53
54     /** If true, enables debugging output from org.columba.core.logging */
55     public static boolean DEBUG = false;
56
57     /**
58      * Don't instanciate this class.
59      */

60     private Logging() {
61     }
62
63     /**
64      * Returns true if the user has defined a logging config file. The user can
65      * define a config file using the system property
66      * <code>java.util.logging.config.file</code>.
67      *
68      * @return true if the user has defined a logging config file; false
69      * otherwise.
70      */

71     private static boolean userHasDefinedLogging() {
72         return (System.getProperty("java.util.logging.config.file") != null);
73     }
74
75     /**
76      * Creates the console handler. The console handler outputs only the
77      * severest logging message unless the MainInterface.DEBUG flag is set.
78      */

79     public static void createDefaultHandler() {
80
81         if (!userHasDefinedLogging()) {
82
83             // Since Columba is doing its own logging handlers, we should not
84
// use handlers in the parent logger.
85
LOG.setUseParentHandlers(false);
86
87             // init console handler
88
consoleHandler = new ConsoleHandler JavaDoc();
89
90             consoleHandler.setFormatter(new OneLineFormatter());
91             consoleHandler.setLevel(Level.SEVERE);
92
93             LOG.addHandler(consoleHandler);
94         }
95     }
96
97     public static void setDebugging(boolean debug) {
98         if (debug) {
99             consoleHandler.setFormatter(new DebugFormatter());
100             consoleHandler.setLevel(Level.ALL);
101
102             LOG.setLevel(Level.ALL);
103             // System.setProperty("javax.net.debug",
104
// "ssl,handshake,data,trustmanager"); // init java.net.ssl
105
// debugging
106

107             // TODO Ristretto should handle the logging of streams in another
108
// way.
109
RistrettoLogger.setLogStream(System.out);
110         } else {
111             consoleHandler.setFormatter(new OneLineFormatter());
112             consoleHandler.setLevel(Level.SEVERE);
113
114             LOG.setLevel(Level.SEVERE);
115         }
116     }
117
118     /**
119      * Default logger configuration used by Columba.
120      * <p>
121      * If the user has not defined their own config file for the logging
122      * framework, then this will create a log file named
123      * <code>columba.log</code>, in the default config directory.
124      */

125     public static void createDefaultFileHandler(File JavaDoc configDirectory) {
126
127         if (!userHasDefinedLogging()) {
128             String JavaDoc logConfigFile = System
129                     .getProperty("java.util.logging.config.file");
130             if (logConfigFile == null) {
131
132                 // create logging file in "<users config-folder>/log"
133
File JavaDoc file = new File JavaDoc(configDirectory, "log");
134                 if (!file.exists())
135                     file.mkdir();
136
137                 File JavaDoc loggingFile = new File JavaDoc(file, "columba.log");
138
139                 // Setup file logging
140
try {
141                     Handler JavaDoc handler = new FileHandler JavaDoc(loggingFile.getPath(),
142                             false);
143                     handler.setFormatter(new SimpleFormatter JavaDoc()); // don't use
144
// standard
145
// XML
146
// formatting
147

148                     if (Logging.DEBUG) {
149                         handler.setLevel(Level.ALL);
150                     } else {
151                         handler.setLevel(Level.WARNING);
152                     }
153
154                     LOG.addHandler(handler);
155                 } catch (IOException JavaDoc ioe) {
156                     LOG.severe("Could not start the file logging due to: "
157                             + ioe);
158                 }
159             }
160         }
161     }
162 }
163
Popular Tags