1 /*
2 * Autopsy Forensic Browser
3 *
4 * Copyright 2012-2018 Basis Technology Corp.
5 * Contact: carrier <at> sleuthkit <dot> org
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 package org.sleuthkit.autopsy.coreutils;
20
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;
28 import java.util.Map;
29 import java.util.logging.LogRecord;
31
37
39 private static final int LOG_SIZE = 0;
// In bytes, zero is unlimited
42 private static final Handler
consoleHandler =
new java.util.logging.ConsoleHandler();
44
54 String logFilePath = Paths.get(logDirectory, LOG_FILE_NAME).toString();
55 try {
57 fileHandler.setEncoding(LOG_ENCODING);
58 fileHandler.setFormatter(new Formatter() {
59 @Override
60 public String format(LogRecord record) {
61 Throwable thrown = record.getThrown();
62 String stackTrace = ""; //NON-NLS
63 while (thrown != null) {
64 stackTrace += thrown.toString() + "\n";
65 for (StackTraceElement traceElem : record.getThrown().getStackTrace()) {
66 stackTrace += "\t" + traceElem.toString() + "\n"; //NON-NLS
67 }
68 thrown = thrown.getCause();
69 }
70 return (new Timestamp(record.getMillis())).toString() + " " //NON-NLS
71 + record.getSourceClassName() + " " //NON-NLS
72 + record.getSourceMethodName() + "\n" //NON-NLS
73 + record.getLevel() + ": " //NON-NLS
74 + this.formatMessage(record) + "\n" //NON-NLS
75 + stackTrace;
76 }
77 });
78 return fileHandler;
79 } catch (IOException ex) {
80 throw new RuntimeException(String.format("Error initializing file handler for %s", logFilePath), ex); //NON-NLS
81 }
82 }
83
90 /*
91 * Create a file handler for the new directory and swap it into all of
92 * the existing loggers using thread-safe Logger methods. The new
93 * handlers are added before the old handlers so that no messages will
94 * be lost, but this makes it possible for log messages to be written
95 * via the old handlers if logging calls are interleaved with the
96 * add/remove handler calls (currently, the base class handlers
97 * collection is a CopyOnWriteArrayList).
98 */
100 for (
Logger logger : namesToLoggers.values()) {
101 logger.addHandler(newFileHandler);
102 logger.removeHandler(logFileHandler);
103 }
104
105 /*
106 * Close the old file handler and save reference to the new handler
107 * so they can be added to any new loggers. This swap is why this method
108 * and the two overloads of getLogger() are synchronized, serializing
109 * access to logFileHandler.
110 */
111 logFileHandler.close();
112 logFileHandler = newFileHandler;
113 }
114
126 }
127
142 if (!namesToLoggers.containsKey(name)) {
144 logger.addHandler(logFileHandler);
145 namesToLoggers.put(name, logger);
146 }
147 return namesToLoggers.get(name);
148 }
149
160 private Logger(String name, String resourceBundleName) {
161 super(name, resourceBundleName);
162 super.setUseParentHandlers(false);
164 super.addHandler(consoleHandler);
165 }
166 }
167 }
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)