1 /*
2 * Autopsy Forensic Browser
3 *
4 * Copyright 2012-2014 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.File;
22 import java.io.IOException;
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.logging.LogRecord;
29
34
36 private static final int LOG_SIZE = 0;
// In bytes, zero is unlimited
40 private static final Handler
console =
new java.util.logging.ConsoleHandler();
44
46 try {
47 FileHandler f = new FileHandler(logDirectory + File.separator + fileName, LOG_SIZE, LOG_FILE_COUNT);
48 f.setEncoding(LOG_ENCODING);
49 switch (fileName) {
50 case LOG_WITHOUT_STACK_TRACES:
51 f.setFormatter(new Formatter() {
52 @Override
53 public String format(LogRecord record) {
55 return (new Date(record.getMillis())).toString() + " "
56 + record.getSourceClassName() + " "
57 + record.getSourceMethodName() + "\n"
58 + record.getLevel() + ": "
59 + this.formatMessage(record) + "\n";
60 }
61 }
62 });
63 break;
64 case LOG_WITH_STACK_TRACES:
65 f.setFormatter(new Formatter() {
66 @Override
67 public String format(LogRecord record) {
69 if (record.getThrown() != null) {
70
71 StackTraceElement ele[] = record.getThrown().getStackTrace();
72 String StackTrace = "";
73 for (StackTraceElement ele1 : ele) {
74 StackTrace += "\t" + ele1.toString() + "\n";
75 }
76
77 return (new Timestamp(record.getMillis())).toString() + " "
78 + record.getSourceClassName() + " "
79 + record.getSourceMethodName() + "\n"
80 + record.getLevel() + ": "
81 + this.formatMessage(record) + "\n"
82 + record.getThrown().toString() + ": "
83 + StackTrace
84 + "\n";
85 } else {
86 return (new Timestamp(record.getMillis())).toString() + " "
87 + record.getSourceClassName() + " "
88 + record.getSourceMethodName() + "\n"
89 + record.getLevel() + ": "
90 + this.formatMessage(record) + "\n";
91 }
92 }
93 }
94 });
95 break;
96 }
97 return f;
98 } catch (IOException e) {
99 throw new RuntimeException("Error initializing " + fileName + " file handler", e); //NON-NLS
100 }
101 }
102
109 if (null != directoryPath && !directoryPath.isEmpty()) {
110 File directory = new File(directoryPath);
111 if (directory.exists() && directory.canWrite()) {
113 userFriendlyLogFile.close();
114 userFriendlyLogFile =
createFileHandler(directoryPath, LOG_WITHOUT_STACK_TRACES);
115 developersLogFile.close();
117 }
118 }
119 }
120 }
121
132 return new Logger(name, null);
133 }
134
148 return new Logger(name, resourceBundleName);
149 }
150
151 private Logger(String name, String resourceBundleName) {
152 super(name, resourceBundleName);
154 super.addHandler(console);
155 }
157 super.setUseParentHandlers(false);
158 super.addHandler(userFriendlyLogFile);
159 super.addHandler(developersLogFile);
160 }
161 }
162 }
static Version.Type getBuildType()
static final int LOG_SIZE
static final String LOG_WITHOUT_STACK_TRACES
static FileHandler createFileHandler(String logDirectory, String fileName)
static void setLogDirectory(String directoryPath)
Logger(String name, String resourceBundleName)
static final int LOG_FILE_COUNT
static final String LOG_ENCODING
static FileHandler userFriendlyLogFile
static final Object fileHandlerLock
static final String LOG_WITH_STACK_TRACES
static Logger getLogger(String name, String resourceBundleName)
static final Handler console
static Logger getLogger(String name)
static FileHandler developersLogFile