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