Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
JdkLoggingExceptionHandler |
|
| 2.0;2 |
1 | /* | |
2 | * This software is licensed under the Apache License, Version 2.0 | |
3 | * (the "License") agreement; you may not use this file except in compliance with | |
4 | * the License. You may obtain a copy of the License at | |
5 | * | |
6 | * http://www.apache.org/licenses/LICENSE-2.0 | |
7 | * | |
8 | * Unless required by applicable law or agreed to in writing, software | |
9 | * distributed under the License is distributed on an "AS IS" BASIS, | |
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
11 | * See the License for the specific language governing permissions and | |
12 | * limitations under the License. | |
13 | */ | |
14 | package net.admin4j.exception; | |
15 | ||
16 | import java.util.logging.Handler; | |
17 | import java.util.logging.LogRecord; | |
18 | ||
19 | import org.slf4j.Logger; | |
20 | import org.slf4j.LoggerFactory; | |
21 | ||
22 | /** | |
23 | * Will track exceptions recorded via native Jdk logging | |
24 | * @author D. Ashmore | |
25 | * @since 1.0 | |
26 | */ | |
27 | 15 | public class JdkLoggingExceptionHandler extends Handler { |
28 | ||
29 | 12 | private static Logger logger = LoggerFactory.getLogger(ExceptionTracker.class); |
30 | ||
31 | @Override | |
32 | public void close() throws SecurityException { | |
33 | // NoOp | |
34 | ||
35 | 12 | } |
36 | ||
37 | @Override | |
38 | public void flush() { | |
39 | // NoOp | |
40 | ||
41 | 3 | } |
42 | ||
43 | @Override | |
44 | public void publish(LogRecord record) { | |
45 | /* | |
46 | * It's important that this logger not even throw a RuntimeException. | |
47 | * Throwing any exception will mask the underlying error and do users a great | |
48 | * disservice by masking the root issue. D. Ashmore -- Aug, 2010. | |
49 | */ | |
50 | try { | |
51 | 6 | if (record != null && record.getThrown() != null) { |
52 | 3 | ExceptionTracker.track(record.getThrown()); |
53 | } | |
54 | } | |
55 | 0 | catch (Throwable t) { |
56 | 0 | processError(t); |
57 | 6 | } |
58 | 6 | } |
59 | ||
60 | protected void processError(Throwable t) { | |
61 | 6 | if (logger != null) { |
62 | 6 | logger.error("Error tracking logged exception", t); |
63 | } | |
64 | 0 | else t.printStackTrace(); |
65 | 6 | } |
66 | ||
67 | } |