Search in sources :

Example 1 with FileLogFormatter

use of org.opensolaris.opengrok.logger.formatter.FileLogFormatter in project OpenGrok by OpenGrok.

the class LoggerUtil method setFileHandlerLogPath.

public static void setFileHandlerLogPath(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());
        }
    }
    StringBuilder logfile = new StringBuilder();
    logfile.append(path == null ? "%t" : path);
    logfile.append(File.separatorChar).append("opengrok%g.%u.log");
    for (Handler handler : getBaseLogger().getHandlers()) {
        if (handler instanceof FileHandler) {
            FileHandler fileHandler = (FileHandler) handler;
            FileHandler newFileHandler;
            try {
                int logFilesSizeLimit = loggerIntProperty("java.util.logging.FileHandler.limit", DEFAULT_FILEHANDLER_LIMIT);
                int logFilesCount = loggerIntProperty("java.util.logging.FileHandler.count", DEFAULT_FILEHANDLER_COUNT);
                newFileHandler = new FileHandler(logfile.toString(), logFilesSizeLimit, logFilesCount);
            } catch (IOException e) {
                LOGGER.log(Level.WARNING, "Cannot create new logger FileHandler: " + logfile.toString(), e);
                return;
            }
            String formatter = LogManager.getLogManager().getProperty("java.util.logging.FileHandler.formatter");
            newFileHandler.setLevel(fileHandler.getLevel());
            try {
                newFileHandler.setFormatter((Formatter) Class.forName(formatter).newInstance());
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                newFileHandler.setFormatter(new FileLogFormatter());
            }
            getBaseLogger().addHandler(newFileHandler);
            getBaseLogger().removeHandler(fileHandler);
            loggerFile = logfile.toString();
        }
    }
}
Also used : FileLogFormatter(org.opensolaris.opengrok.logger.formatter.FileLogFormatter) FileHandler(java.util.logging.FileHandler) ConsoleHandler(java.util.logging.ConsoleHandler) Handler(java.util.logging.Handler) IOException(java.io.IOException) File(java.io.File) FileHandler(java.util.logging.FileHandler)

Example 2 with FileLogFormatter

use of org.opensolaris.opengrok.logger.formatter.FileLogFormatter in project OpenGrok by OpenGrok.

the class LoggerUtil method initLogger.

public static String initLogger(String logpath, Level filelevel, Level consolelevel) throws IOException {
    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());
        }
    }
    Logger.getGlobal().setLevel(Level.OFF);
    getBaseLogger().setLevel(Level.ALL);
    StringBuilder logfile = new StringBuilder();
    logfile.append(logpath == null ? "%t" : logpath);
    logfile.append(File.separatorChar).append("opengrok%g.%u.log");
    try {
        FileHandler fh = new FileHandler(logfile.toString(), loggerIntProperty("java.util.logging.FileHandler.limit", DEFAULT_FILEHANDLER_LIMIT), loggerIntProperty("java.util.logging.FileHandler.count", DEFAULT_FILEHANDLER_COUNT));
        fh.setLevel(filelevel);
        String formatter = LogManager.getLogManager().getProperty("java.util.logging.FileHandler.formatter");
        try {
            fh.setFormatter((Formatter) Class.forName(formatter).newInstance());
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            fh.setFormatter(new FileLogFormatter());
        }
        getBaseLogger().addHandler(fh);
        loggerFile = logfile.toString();
        ConsoleHandler ch = new ConsoleHandler();
        ch.setLevel(consolelevel);
        ch.setFormatter(new ConsoleFormatter());
        getBaseLogger().addHandler(ch);
    } catch (IOException | SecurityException ex1) {
        LOGGER.log(Level.SEVERE, "Exception logging", ex1);
        throw new IOException("Exception setting up logging " + ex1);
    }
    return logpath;
}
Also used : FileLogFormatter(org.opensolaris.opengrok.logger.formatter.FileLogFormatter) IOException(java.io.IOException) ConsoleHandler(java.util.logging.ConsoleHandler) FileHandler(java.util.logging.FileHandler) ConsoleFormatter(org.opensolaris.opengrok.logger.formatter.ConsoleFormatter) File(java.io.File)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 ConsoleHandler (java.util.logging.ConsoleHandler)2 FileHandler (java.util.logging.FileHandler)2 FileLogFormatter (org.opensolaris.opengrok.logger.formatter.FileLogFormatter)2 Handler (java.util.logging.Handler)1 ConsoleFormatter (org.opensolaris.opengrok.logger.formatter.ConsoleFormatter)1