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