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.Date;
28 import java.util.HashMap;
30 import java.util.logging.LogRecord;
44 private static final Handler
consoleHandler =
new java.util.logging.ConsoleHandler();
57 String logFilePath = Paths.get(logDirectory, LOG_WITHOUT_STACK_TRACES).toString();
59 FileHandler fileHandler =
new FileHandler(logFilePath, LOG_SIZE, LOG_FILE_COUNT);
60 fileHandler.setEncoding(LOG_ENCODING);
61 fileHandler.setFormatter(
new Formatter() {
63 public String format(LogRecord record) {
64 return (
new Date(record.getMillis())).toString() +
" "
65 + record.getSourceClassName() +
" "
66 + record.getSourceMethodName() +
"\n"
67 + record.getLevel() +
": "
68 + this.formatMessage(record) +
"\n";
72 }
catch (IOException ex) {
73 throw new RuntimeException(String.format(
"Error initializing file handler for %s", logFilePath), ex);
86 String logFilePath = Paths.get(logDirectory, LOG_WITH_STACK_TRACES).toString();
88 FileHandler fileHandler =
new FileHandler(logFilePath, LOG_SIZE, LOG_FILE_COUNT);
89 fileHandler.setEncoding(LOG_ENCODING);
90 fileHandler.setFormatter(
new Formatter() {
92 public String format(LogRecord record) {
93 Throwable thrown = record.getThrown();
94 String stackTrace =
"";
95 while (thrown != null) {
96 stackTrace += thrown.toString() +
"\n";
97 for (StackTraceElement traceElem : record.getThrown().getStackTrace()) {
98 stackTrace +=
"\t" + traceElem.toString() +
"\n";
100 thrown = thrown.getCause();
102 return (
new Timestamp(record.getMillis())).toString() +
" "
103 + record.getSourceClassName() +
" "
104 + record.getSourceMethodName() +
"\n"
105 + record.getLevel() +
": "
106 + this.formatMessage(record) +
"\n"
111 }
catch (IOException ex) {
112 throw new RuntimeException(String.format(
"Error initializing file handler for %s", logFilePath), ex);
133 for (
Logger logger : namesToLoggers.values()) {
134 logger.addHandler(newUserFriendlyHandler);
135 logger.addHandler(newDeveloperFriendlyHandler);
136 logger.removeHandler(userFriendlyHandler);
137 logger.removeHandler(userFriendlyHandler);
146 userFriendlyHandler.close();
147 userFriendlyHandler = newUserFriendlyHandler;
148 developerFriendlyHandler.close();
149 developerFriendlyHandler = newDeveloperFriendlyHandler;
179 if (!namesToLoggers.containsKey(name)) {
181 logger.addHandler(userFriendlyHandler);
182 logger.addHandler(developerFriendlyHandler);
183 namesToLoggers.put(name, logger);
185 return namesToLoggers.get(name);
198 private Logger(String name, String resourceBundleName) {
199 super(name, resourceBundleName);
200 super.setUseParentHandlers(
false);
202 super.addHandler(consoleHandler);
static Version.Type getBuildType()
static final int LOG_SIZE
static final String LOG_WITHOUT_STACK_TRACES
synchronized static void setLogDirectory(String directoryPath)
Logger(String name, String resourceBundleName)
static final int LOG_FILE_COUNT
static FileHandler userFriendlyHandler
static final String LOG_ENCODING
static FileHandler developerFriendlyHandler
static final Handler consoleHandler
static FileHandler createFileHandlerWithTraces(String logDirectory)
static final Map< String, Logger > namesToLoggers
synchronized static Logger getLogger(String name)
static final String LOG_WITH_STACK_TRACES
synchronized static Logger getLogger(String name, String resourceBundleName)
static FileHandler createFileHandlerWithoutTraces(String logDirectory)