OpenGrokLogger.java revision 384
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;
30N/Aimport java.util.logging.FileHandler;
30N/Aimport java.util.logging.Handler;
58N/Aimport java.util.logging.Level;
58N/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 *
320N/A * @author Jan S Berg
320N/A */
490N/Apublic class OpenGrokLogger {
290N/A
112N/A private static int LOGFILESIZELIMIT = 1000000;
570N/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 logpath = "%t";
320N/A } else {
30N/A File jlp = new File(logpath);
30N/A if (!jlp.exists() && !jlp.mkdirs()) {
77N/A throw new RuntimeException("could not make logpath: " +
77N/A jlp.getAbsolutePath());
77N/A }
77N/A }
30N/A
30N/A clearForeignHandlers();
30N/A String logfile = logpath + File.separatorChar + "opengrok%g.%u" + ".log";
30N/A try {
30N/A FileHandler fh = new FileHandler(logfile,
77N/A LOGFILESIZELIMIT, // size (unlimited)
77N/A LOGFILESCOUNT); // # rotations
30N/A
30N/A fh.setLevel(filelevel);
58N/A fh.setFormatter(new FileLogFormatter());
145N/A
145N/A log.addHandler(fh);
145N/A
145N/A ConsoleHandler ch = new ConsoleHandler();
145N/A ch.setLevel(consolelevel);
58N/A ch.setFormatter(new ConsoleFormatter());
77N/A log.addHandler(ch);
490N/A
490N/A } catch (Exception ex1) {
490N/A System.err.println("Exception logging " + ex1);
490N/A }
490N/A log.setLevel(filelevel);
490N/A return logpath;
490N/A }
490N/A
490N/A private static void clearForeignHandlers() {
490N/A for (Enumeration e = LogManager.getLogManager().getLoggerNames();
490N/A e.hasMoreElements();) {
490N/A String loggerName = (String) e.nextElement();
490N/A Logger l = Logger.getLogger(loggerName);
77N/A Handler[] h = l.getHandlers();
77N/A if (!loggerName.startsWith("org.opensolaris.opengrok")) {
77N/A for (int i = 0; i < h.length; ++i) {
77N/A l.removeHandler(h[i]);
58N/A }
145N/A }
58N/A h = l.getHandlers();
58N/A }
77N/A }
77N/A
77N/A private OpenGrokLogger() {
77N/A }
30N/A}
58N/A