001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.core.logging;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.util.logging.ConsoleHandler;
021: import java.util.logging.FileHandler;
022: import java.util.logging.Handler;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025: import java.util.logging.SimpleFormatter;
026:
027: import org.columba.ristretto.log.RistrettoLogger;
028:
029: /**
030: * Depending on the debug flag (--debug command line option reflected in
031: * MainInterface.DEBUG) the logger will either show all debug messages or just
032: * severe errors. Logging information is passed to a log file and to the
033: * console.
034: * <p>
035: * Note, that Logging must not be called before MainInterface.DEBUG, was set.
036: * Otherwise, the logger won't show the correct debug level.
037: * <p>
038: * If the user has defined their own logging config file, then this will take
039: * precedence over Columba defined logging handlers, ie. Columba will not create
040: * its own default logging handlers. All has already been defined in the system
041: * property <b>java.util.logging.config.file</b>.
042: * <p>
043: *
044: * @see org.columba.core.main.Main
045: * @see java.util.logging.Logger
046: * @author redsolo
047: */
048: public final class Logging {
049:
050: private static final Logger LOG = Logger.getLogger("org.columba");
051:
052: private static ConsoleHandler consoleHandler;
053:
054: /** If true, enables debugging output from org.columba.core.logging */
055: public static boolean DEBUG = false;
056:
057: /**
058: * Don't instanciate this class.
059: */
060: private Logging() {
061: }
062:
063: /**
064: * Returns true if the user has defined a logging config file. The user can
065: * define a config file using the system property
066: * <code>java.util.logging.config.file</code>.
067: *
068: * @return true if the user has defined a logging config file; false
069: * otherwise.
070: */
071: private static boolean userHasDefinedLogging() {
072: return (System.getProperty("java.util.logging.config.file") != null);
073: }
074:
075: /**
076: * Creates the console handler. The console handler outputs only the
077: * severest logging message unless the MainInterface.DEBUG flag is set.
078: */
079: public static void createDefaultHandler() {
080:
081: if (!userHasDefinedLogging()) {
082:
083: // Since Columba is doing its own logging handlers, we should not
084: // use handlers in the parent logger.
085: LOG.setUseParentHandlers(false);
086:
087: // init console handler
088: consoleHandler = new ConsoleHandler();
089:
090: consoleHandler.setFormatter(new OneLineFormatter());
091: consoleHandler.setLevel(Level.SEVERE);
092:
093: LOG.addHandler(consoleHandler);
094: }
095: }
096:
097: public static void setDebugging(boolean debug) {
098: if (debug) {
099: 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 configDirectory) {
126:
127: if (!userHasDefinedLogging()) {
128: String 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 file = new File(configDirectory, "log");
134: if (!file.exists())
135: file.mkdir();
136:
137: File loggingFile = new File(file, "columba.log");
138:
139: // Setup file logging
140: try {
141: Handler handler = new FileHandler(loggingFile
142: .getPath(), false);
143: handler.setFormatter(new SimpleFormatter()); // 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 ioe) {
156: LOG
157: .severe("Could not start the file logging due to: "
158: + ioe);
159: }
160: }
161: }
162: }
163: }
|