Autopsy  4.19.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
Logger.java
Go to the documentation of this file.
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 
36 public final class Logger extends java.util.logging.Logger {
37 
38  private static final String LOG_ENCODING = PlatformUtil.getLogFileEncoding();
39  private static final int LOG_SIZE = 0; // In bytes, zero is unlimited
40  private static final String LOG_FILE_NAME = "autopsy.log"; //NON-NLS
41  private static final Map<String, Logger> namesToLoggers = new HashMap<>();
42  private static final Handler consoleHandler = new java.util.logging.ConsoleHandler();
44 
53  private static FileHandler createFileHandlerWithTraces(String logDirectory) {
54  String logFilePath = Paths.get(logDirectory, LOG_FILE_NAME).toString();
55  try {
56  FileHandler fileHandler = new FileHandler(logFilePath, LOG_SIZE, UserPreferences.getLogFileCount());
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 
89  synchronized public static void setLogDirectory(String directoryPath) {
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  */
99  FileHandler newFileHandler = createFileHandlerWithTraces(directoryPath);
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 
124  synchronized public static Logger getLogger(String name) {
125  return getLogger(name, null);
126  }
127 
141  synchronized public static Logger getLogger(String name, String resourceBundleName) {
142  if (!namesToLoggers.containsKey(name)) {
143  Logger logger = new Logger(name, resourceBundleName);
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()
Definition: Version.java:87
synchronized static void setLogDirectory(String directoryPath)
Definition: Logger.java:89
static final String LOG_FILE_NAME
Definition: Logger.java:40
Logger(String name, String resourceBundleName)
Definition: Logger.java:160
static final String LOG_ENCODING
Definition: Logger.java:38
static FileHandler logFileHandler
Definition: Logger.java:43
static final Handler consoleHandler
Definition: Logger.java:42
static FileHandler createFileHandlerWithTraces(String logDirectory)
Definition: Logger.java:53
static final Map< String, Logger > namesToLoggers
Definition: Logger.java:41
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
synchronized static Logger getLogger(String name, String resourceBundleName)
Definition: Logger.java:141

Copyright © 2012-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.