OpenGrokLogger.java revision 1327
822N/A/*
822N/A * CDDL HEADER START
822N/A *
822N/A * The contents of this file are subject to the terms of the
822N/A * Common Development and Distribution License (the "License").
822N/A * You may not use this file except in compliance with the License.
822N/A *
822N/A * See LICENSE.txt included in this distribution for the specific
822N/A * language governing permissions and limitations under the License.
822N/A *
822N/A * When distributing Covered Code, include this CDDL HEADER in each
822N/A * file and include the License file at LICENSE.txt.
822N/A * If applicable, add the following below this CDDL HEADER, with the
822N/A * fields enclosed by brackets "[]" replaced with your own identifying
822N/A * information: Portions Copyright [yyyy] [name of copyright owner]
822N/A *
822N/A * CDDL HEADER END
822N/A */
822N/A
822N/A/*
822N/A * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
822N/A */
822N/Apackage org.opensolaris.opengrok;
822N/A
822N/Aimport java.io.File;
822N/Aimport java.io.IOException;
822N/Aimport java.util.Enumeration;
822N/Aimport java.util.logging.ConsoleHandler;
822N/Aimport java.util.logging.FileHandler;
822N/Aimport java.util.logging.Handler;
822N/Aimport java.util.logging.Level;
822N/Aimport java.util.logging.LogManager;
822N/Aimport java.util.logging.Logger;
822N/A
822N/A/**
822N/A * Util class to set up Logging using the Console and FileLog formatter classes
822N/A *
822N/A * @author Jan S Berg
822N/A */
822N/A@SuppressWarnings({"PMD.MoreThanOneLogger", "PMD.SystemPrintln", "PMD.AvoidThrowingRawExceptionTypes"})
822N/Apublic final class OpenGrokLogger {
822N/A
822N/A private static int LOGFILESIZELIMIT = 1000000;
822N/A private static int LOGFILESCOUNT = 30;
822N/A private static final Logger log = Logger.getLogger("org.opensolaris.opengrok");
822N/A private static Level consoleLevel = Level.WARNING;
822N/A private static Level fileLevel = Level.FINE;
822N/A private static String filepath = "";
822N/A
822N/A public static String getFileLogPath() {
822N/A return filepath;
822N/A }
822N/A
822N/A public static Logger getLogger() {
822N/A return log;
822N/A }
822N/A
822N/A public static void setConsoleLogLevel(Level level) {
822N/A Handler[] handlers = log.getHandlers();
822N/A for (int i = 0; i < handlers.length; i++) {
822N/A Handler h = handlers[i];
if (h instanceof ConsoleHandler) {
h.setLevel(level);
consoleLevel = level;
}
}
}
public static Level getConsoleLogLevel() {
return consoleLevel;
}
/**
*
* @param level new level for console
*/
public static void setOGConsoleLogLevel(Level level) {
for (Enumeration<String> e = LogManager.getLogManager().getLoggerNames();
e.hasMoreElements();) {
String loggerName = 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) {
Handler hi = h[i];
if (hi instanceof ConsoleHandler) {
hi.setLevel(level);
}
}
}
h = l.getHandlers();
}
}
public static void setFileLogLevel(Level level) {
Handler[] handlers = log.getHandlers();
for (int i = 0; i < handlers.length; i++) {
Handler h = handlers[i];
if (h instanceof FileHandler) {
h.setLevel(level);
fileLevel = level;
}
}
}
public static Level getFileLogLevel() {
return fileLevel;
}
public static void setFileLogPath(String path) throws IOException {
if (path != null) {
File jlp = new File(path);
if (!jlp.exists() && !jlp.mkdirs()) {
throw new IOException("could not make logpath: " +
jlp.getAbsolutePath());
}
}
StringBuffer logfile;
if (path == null) {
logfile = new StringBuffer("%t");
} else {
logfile = new StringBuffer(path);
}
filepath = logfile.toString();
logfile.append(File.separatorChar).append("opengrok%g.%u.log");
Handler[] handlers = log.getHandlers();
for (int i = 0; i < handlers.length; i++) {
Handler h = handlers[i];
if (h instanceof FileHandler) {
FileHandler fh = (FileHandler) h;
FileHandler nfh = new FileHandler(logfile.toString(),
LOGFILESIZELIMIT, // size (unlimited)
LOGFILESCOUNT); // # rotations
nfh.setLevel(fh.getLevel());
nfh.setFormatter(new FileLogFormatter());
log.addHandler(nfh);
log.removeHandler(fh);
}
}
}
public static String setupLogger(String logpath, Level filelevel, Level consolelevel) throws IOException {
System.out.println("Logging to " + logpath);
if (logpath != null) {
File jlp = new File(logpath);
if (!jlp.exists() && !jlp.mkdirs()) {
throw new RuntimeException("could not make logpath: " +
jlp.getAbsolutePath());
}
if (!jlp.canWrite() && !Level.OFF.equals(filelevel)) {
throw new IOException("logpath not writeable " + jlp.getAbsolutePath());
}
}
clearForeignHandlers();
StringBuffer logfile;
if (logpath == null) {
logfile = new StringBuffer("%t");
} else {
logfile = new StringBuffer(logpath);
}
filepath = logfile.toString();
logfile.append(File.separatorChar).append("opengrok%g.%u.log");
try {
FileHandler fh = new FileHandler(logfile.toString(),
LOGFILESIZELIMIT, // size (unlimited)
LOGFILESCOUNT); // # rotations
fh.setLevel(filelevel);
fileLevel = filelevel;
fh.setFormatter(new FileLogFormatter());
log.addHandler(fh);
ConsoleHandler ch = new ConsoleHandler();
ch.setLevel(consolelevel);
consoleLevel = consolelevel;
ch.setFormatter(new ConsoleFormatter());
log.addHandler(ch);
} catch (Exception ex1) {
throw new IOException("Exception setting up logging " + ex1.getMessage());
}
log.setLevel(filelevel);
return logpath;
}
private static void clearForeignHandlers() {
for (Enumeration<String> e = LogManager.getLogManager().getLoggerNames();
e.hasMoreElements();) {
String loggerName = 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() {
}
}