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