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