OpenGrokLogger.java revision 465
30N/A/*
30N/A * CDDL HEADER START
30N/A *
30N/A * The contents of this file are subject to the terms of the
30N/A * Common Development and Distribution License (the "License").
30N/A * You may not use this file except in compliance with the License.
30N/A *
30N/A * See LICENSE.txt included in this distribution for the specific
30N/A * language governing permissions and limitations under the License.
30N/A *
30N/A * When distributing Covered Code, include this CDDL HEADER in each
30N/A * file and include the License file at LICENSE.txt.
30N/A * If applicable, add the following below this CDDL HEADER, with the
30N/A * fields enclosed by brackets "[]" replaced with your own identifying
30N/A * information: Portions Copyright [yyyy] [name of copyright owner]
30N/A *
30N/A * CDDL HEADER END
30N/A */
30N/A
30N/A/*
30N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
30N/A * Use is subject to license terms.
30N/A */
30N/Apackage org.opensolaris.opengrok;
30N/A
58N/Aimport java.io.File;
58N/Aimport java.util.Enumeration;
58N/Aimport java.util.logging.ConsoleHandler;
99N/Aimport java.util.logging.FileHandler;
30N/Aimport java.util.logging.Handler;
30N/Aimport java.util.logging.Level;
99N/Aimport java.util.logging.LogManager;
58N/Aimport java.util.logging.Logger;
58N/A
58N/A/**
58N/A * Util class to set up Logging using the Console and FileLog formatter classes
58N/A *
58N/A * @author Jan S Berg
58N/A */
58N/A@SuppressWarnings({"PMD.MoreThanOneLogger", "PMD.SystemPrintln", "PMD.AvoidThrowingRawExceptionTypes"})
320N/Apublic final class OpenGrokLogger {
320N/A
290N/A private static int LOGFILESIZELIMIT = 1000000;
112N/A private static int LOGFILESCOUNT = 30;
30N/A private final static Logger log = Logger.getLogger("org.opensolaris.opengrok");
30N/A
30N/A public static Logger getLogger() {
58N/A return log;
30N/A }
418N/A
58N/A public static String setupLogger(String logpath, Level filelevel, Level consolelevel) {
456N/A System.out.println("Logging to " + logpath);
30N/A if (logpath != null) {
320N/A File jlp = new File(logpath);
320N/A if (!jlp.exists() && !jlp.mkdirs()) {
30N/A throw new RuntimeException("could not make logpath: " +
30N/A jlp.getAbsolutePath());
77N/A }
77N/A }
77N/A
77N/A clearForeignHandlers();
30N/A StringBuffer logfile;
30N/A if (logpath == null) {
30N/A logfile = new StringBuffer("%t");
30N/A } else {
30N/A logfile = new StringBuffer(logpath);
77N/A }
77N/A logfile.append(File.separatorChar).append("opengrok%g.%u.log");
30N/A try {
30N/A FileHandler fh = new FileHandler(logfile.toString(),
58N/A LOGFILESIZELIMIT, // size (unlimited)
145N/A LOGFILESCOUNT); // # rotations
145N/A
145N/A fh.setLevel(filelevel);
145N/A fh.setFormatter(new FileLogFormatter());
145N/A
58N/A log.addHandler(fh);
77N/A
77N/A ConsoleHandler ch = new ConsoleHandler();
77N/A ch.setLevel(consolelevel);
77N/A ch.setFormatter(new ConsoleFormatter());
77N/A log.addHandler(ch);
58N/A
145N/A } catch (Exception ex1) {
58N/A System.err.println("Exception logging " + ex1);
58N/A }
77N/A log.setLevel(filelevel);
77N/A return logpath;
77N/A }
77N/A
30N/A private static void clearForeignHandlers() {
58N/A for (Enumeration e = LogManager.getLogManager().getLoggerNames();
58N/A e.hasMoreElements();) {
58N/A String loggerName = (String) e.nextElement();
58N/A Logger l = Logger.getLogger(loggerName);
58N/A Handler[] h = l.getHandlers();
58N/A if (!loggerName.startsWith("org.opensolaris.opengrok")) {
58N/A for (int i = 0; i < h.length; ++i) {
30N/A l.removeHandler(h[i]);
58N/A }
77N/A }
77N/A h = l.getHandlers();
77N/A }
77N/A }
77N/A
77N/A private OpenGrokLogger() {
145N/A }
77N/A}
77N/A