Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
ApplicationLoggers.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2020 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.apputils;
20 
21 import java.io.IOException;
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24 import java.sql.Timestamp;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.logging.FileHandler;
28 import java.util.logging.Formatter;
29 import java.util.logging.LogRecord;
30 import java.util.logging.Logger;
32 
43 final public class ApplicationLoggers {
44 
45  private static final int LOG_SIZE = 50000000; // In bytes, zero is unlimited.
46  private static final int LOG_FILE_COUNT = 10;
47  private static final String NEWLINE = System.lineSeparator();
48  private static final Map<String, Logger> loggers = new HashMap<>();
49 
57  synchronized public static Logger getLogger(String logName) {
58  Logger logger;
59  if (loggers.containsKey(logName)) {
60  logger = loggers.get(logName);
61  } else {
62  logger = Logger.getLogger(logName);
63  Path logFilePath = Paths.get(PlatformUtil.getUserDirectory().getAbsolutePath(), "var", "log", String.format("%s.log", logName));
64  try {
65  FileHandler fileHandler = new FileHandler(logFilePath.toString(), LOG_SIZE, LOG_FILE_COUNT);
66  fileHandler.setEncoding(PlatformUtil.getLogFileEncoding());
67  fileHandler.setFormatter(new Formatter() {
68  @Override
69  public String format(LogRecord record) {
70  Throwable thrown = record.getThrown();
71  String stackTrace = ""; //NON-NLS
72  while (thrown != null) {
73  stackTrace += thrown.toString() + NEWLINE;
74  for (StackTraceElement traceElem : record.getThrown().getStackTrace()) {
75  stackTrace += "\t" + traceElem.toString() + NEWLINE; //NON-NLS
76  }
77  thrown = thrown.getCause();
78  }
79  return (new Timestamp(record.getMillis())).toString() + " " //NON-NLS
80  + record.getSourceClassName() + " " //NON-NLS
81  + record.getSourceMethodName() + NEWLINE
82  + record.getLevel() + ": " //NON-NLS
83  + this.formatMessage(record) + NEWLINE
84  + stackTrace;
85  }
86  });
87  logger.addHandler(fileHandler);
88  logger.setUseParentHandlers(false);
89  } catch (SecurityException | IOException ex) {
90  throw new RuntimeException(String.format("Error initializing file handler for %s", logFilePath), ex); //NON-NLS
91  }
92  loggers.put(logName, logger);
93  }
94  return logger;
95  }
96 
100  private ApplicationLoggers() {
101  }
102 
103 }
synchronized static Logger getLogger(String logName)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2022 Basis Technology. Generated on: Tue Jun 27 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.