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