222c232d357e358bf67bd0685b8331b71bb56f97JHKST * CDDL HEADER START
222c232d357e358bf67bd0685b8331b71bb56f97JHKST * The contents of this file are subject to the terms of the
222c232d357e358bf67bd0685b8331b71bb56f97JHKST * Common Development and Distribution License (the "License").
222c232d357e358bf67bd0685b8331b71bb56f97JHKST * You may not use this file except in compliance with the License.
222c232d357e358bf67bd0685b8331b71bb56f97JHKST * See LICENSE.txt included in this distribution for the specific
222c232d357e358bf67bd0685b8331b71bb56f97JHKST * language governing permissions and limitations under the License.
222c232d357e358bf67bd0685b8331b71bb56f97JHKST * When distributing Covered Code, include this CDDL HEADER in each
222c232d357e358bf67bd0685b8331b71bb56f97JHKST * file and include the License file at LICENSE.txt.
222c232d357e358bf67bd0685b8331b71bb56f97JHKST * If applicable, add the following below this CDDL HEADER, with the
222c232d357e358bf67bd0685b8331b71bb56f97JHKST * fields enclosed by brackets "[]" replaced with your own identifying
222c232d357e358bf67bd0685b8331b71bb56f97JHKST * information: Portions Copyright [yyyy] [name of copyright owner]
222c232d357e358bf67bd0685b8331b71bb56f97JHKST * CDDL HEADER END
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
222c232d357e358bf67bd0685b8331b71bb56f97JHKSTimport org.opensolaris.opengrok.logger.formatter.ConsoleFormatter;
222c232d357e358bf67bd0685b8331b71bb56f97JHKSTimport org.opensolaris.opengrok.logger.formatter.FileLogFormatter;
222c232d357e358bf67bd0685b8331b71bb56f97JHKST * Utilities to maintain logging.
222c232d357e358bf67bd0685b8331b71bb56f97JHKST private static final Logger LOGGER = LoggerFactory.getLogger(LoggerUtil.class);
222c232d357e358bf67bd0685b8331b71bb56f97JHKST public static final String BASE_LOGGER = "org.opensolaris.opengrok";
222c232d357e358bf67bd0685b8331b71bb56f97JHKST private static final int DEFAULT_FILEHANDLER_LIMIT = 52428800;
222c232d357e358bf67bd0685b8331b71bb56f97JHKST private static final int DEFAULT_FILEHANDLER_COUNT = 3;
222c232d357e358bf67bd0685b8331b71bb56f97JHKST public static void setBaseConsoleLogLevel(Level level) {
222c232d357e358bf67bd0685b8331b71bb56f97JHKST private static void setBaseLogLevel(Class<? extends Handler> handlerClass, Level level) {
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco for (Handler handler : getBaseLogger().getHandlers()) {
222c232d357e358bf67bd0685b8331b71bb56f97JHKST private static Level getBaseLogLevel(Class<? extends Handler> handlerClass) {
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco for (Handler handler : getBaseLogger().getHandlers()) {
222c232d357e358bf67bd0685b8331b71bb56f97JHKST return LogManager.getLogManager().getProperty("java.util.logging.FileHandler.pattern");
222c232d357e358bf67bd0685b8331b71bb56f97JHKST public static void setFileHandlerLogPath(String path) throws IOException {
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco throw new IOException("could not make logpath: "
222c232d357e358bf67bd0685b8331b71bb56f97JHKST logfile.append(File.separatorChar).append("opengrok%g.%u.log");
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco for (Handler handler : getBaseLogger().getHandlers()) {
222c232d357e358bf67bd0685b8331b71bb56f97JHKST int logFilesSizeLimit = loggerIntProperty("java.util.logging.FileHandler.limit", DEFAULT_FILEHANDLER_LIMIT);
222c232d357e358bf67bd0685b8331b71bb56f97JHKST int logFilesCount = loggerIntProperty("java.util.logging.FileHandler.count", DEFAULT_FILEHANDLER_COUNT);
222c232d357e358bf67bd0685b8331b71bb56f97JHKST newFileHandler = new FileHandler(logfile.toString(), logFilesSizeLimit, logFilesCount);
222c232d357e358bf67bd0685b8331b71bb56f97JHKST } catch (IOException e) {
222c232d357e358bf67bd0685b8331b71bb56f97JHKST LOGGER.log(Level.WARNING, "Cannot create new logger FileHandler: " + logfile.toString(), e);
222c232d357e358bf67bd0685b8331b71bb56f97JHKST String formatter = LogManager.getLogManager().getProperty("java.util.logging.FileHandler.formatter");
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco newFileHandler.setFormatter((Formatter) Class.forName(formatter).newInstance());
222c232d357e358bf67bd0685b8331b71bb56f97JHKST } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
222c232d357e358bf67bd0685b8331b71bb56f97JHKST return loggerFile != null ? loggerFile : LogManager.getLogManager().getProperty("java.util.logging.FileHandler.pattern");
222c232d357e358bf67bd0685b8331b71bb56f97JHKST private static int loggerIntProperty(String name, int def) {
222c232d357e358bf67bd0685b8331b71bb56f97JHKST String val = LogManager.getLogManager().getProperty(name);
222c232d357e358bf67bd0685b8331b71bb56f97JHKST public static String initLogger(String logpath, Level filelevel, Level consolelevel) throws IOException {
bf4379bb564a2928917ad5dac2fc88c788cbcbb5Lubos Kosco throw new RuntimeException("could not make logpath: "
222c232d357e358bf67bd0685b8331b71bb56f97JHKST if (!jlp.canWrite() && !Level.OFF.equals(filelevel)) {
222c232d357e358bf67bd0685b8331b71bb56f97JHKST throw new IOException("logpath not writeable " + jlp.getAbsolutePath());
222c232d357e358bf67bd0685b8331b71bb56f97JHKST logfile.append(File.separatorChar).append("opengrok%g.%u.log");
222c232d357e358bf67bd0685b8331b71bb56f97JHKST loggerIntProperty("java.util.logging.FileHandler.limit", DEFAULT_FILEHANDLER_LIMIT),
222c232d357e358bf67bd0685b8331b71bb56f97JHKST loggerIntProperty("java.util.logging.FileHandler.count", DEFAULT_FILEHANDLER_COUNT));
222c232d357e358bf67bd0685b8331b71bb56f97JHKST String formatter = LogManager.getLogManager().getProperty("java.util.logging.FileHandler.formatter");
222c232d357e358bf67bd0685b8331b71bb56f97JHKST fh.setFormatter((Formatter) Class.forName(formatter).newInstance());
222c232d357e358bf67bd0685b8331b71bb56f97JHKST } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
222c232d357e358bf67bd0685b8331b71bb56f97JHKST throw new IOException("Exception setting up logging " + ex1);