19 package org.sleuthkit.autopsy.coreutils;
21 import java.io.IOException;
22 import java.nio.file.Paths;
23 import java.util.logging.FileHandler;
24 import java.util.logging.Formatter;
25 import java.util.logging.Handler;
26 import java.sql.Timestamp;
27 import java.util.HashMap;
29 import java.util.logging.LogRecord;
42 private static final Handler
consoleHandler =
new java.util.logging.ConsoleHandler();
54 String logFilePath = Paths.get(logDirectory, LOG_FILE_NAME).toString();
57 fileHandler.setEncoding(LOG_ENCODING);
58 fileHandler.setFormatter(
new Formatter() {
60 public String format(LogRecord record) {
61 Throwable thrown = record.getThrown();
62 String stackTrace =
"";
63 while (thrown != null) {
64 stackTrace += thrown.toString() +
"\n";
65 for (StackTraceElement traceElem : record.getThrown().getStackTrace()) {
66 stackTrace +=
"\t" + traceElem.toString() +
"\n";
68 thrown = thrown.getCause();
70 return (
new Timestamp(record.getMillis())).toString() +
" "
71 + record.getSourceClassName() +
" "
72 + record.getSourceMethodName() +
"\n"
73 + record.getLevel() +
": "
74 + this.formatMessage(record) +
"\n"
79 }
catch (IOException ex) {
80 throw new RuntimeException(String.format(
"Error initializing file handler for %s", logFilePath), ex);
100 for (
Logger logger : namesToLoggers.values()) {
101 logger.addHandler(newFileHandler);
102 logger.removeHandler(logFileHandler);
111 logFileHandler.close();
112 logFileHandler = newFileHandler;
142 if (!namesToLoggers.containsKey(name)) {
144 logger.addHandler(logFileHandler);
145 namesToLoggers.put(name, logger);
147 return namesToLoggers.get(name);
160 private Logger(String name, String resourceBundleName) {
161 super(name, resourceBundleName);
162 super.setUseParentHandlers(
false);
164 super.addHandler(consoleHandler);
static Version.Type getBuildType()
static final int LOG_SIZE
static int getLogFileCount()
synchronized static void setLogDirectory(String directoryPath)
static final String LOG_FILE_NAME
Logger(String name, String resourceBundleName)
static final String LOG_ENCODING
static FileHandler logFileHandler
static final Handler consoleHandler
static FileHandler createFileHandlerWithTraces(String logDirectory)
static final Map< String, Logger > namesToLoggers
synchronized static Logger getLogger(String name)
synchronized static Logger getLogger(String name, String resourceBundleName)