Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DIFExceptionLogAspect |
|
| 0.0;0 |
1 | 0 | /** |
2 | * - Digitalis Internal Framework v2.0 - (C) 2007, Digitalis Informatica. Distribuicao e Gestao de Informatica, Lda. | |
3 | * Estrada de Paco de Arcos num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 | |
4 | * http://www.digitalis.pt | |
5 | */ | |
6 | package pt.digitalis.dif.utils.logging; | |
7 | ||
8 | import pt.digitalis.dif.startup.DIFStartupConfiguration; | |
9 | import pt.digitalis.dif.exception.DIFException; | |
10 | import pt.digitalis.dif.exception.InternalFrameworkException; | |
11 | import pt.digitalis.dif.exception.controller.UserHasNoAccessToStage; | |
12 | import pt.digitalis.log.LogLevel; | |
13 | ||
14 | /** | |
15 | * This aspect logs all system-wide exceptions. | |
16 | * | |
17 | * @author Rodrigo Gon�alves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a> | |
18 | * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a> | |
19 | * @created 2007/05/23 | |
20 | */ | |
21 | 0 | public aspect DIFExceptionLogAspect extends DIFLogAspect { |
22 | ||
23 | /** | |
24 | * Captures all exception handlers, exposing the exception to the advice and excluding this aspect (avoid | |
25 | * recursion). | |
26 | * | |
27 | * @param exception | |
28 | * the exception to treat | |
29 | */ | |
30 | 0 | private pointcut exceptionHandlers(Exception exception) : handler(Exception+) && args(exception) && !within(DIFExceptionLogAspect+) && !cflow(adviceexecution()); |
31 | ||
32 | /** | |
33 | * Logs all exceptions thrown outside the aspect. It treats the situation where the exception comes from a | |
34 | * framework-generated code snippet. In this situation the exception is logged with a lower logging level. | |
35 | * | |
36 | * @param exception | |
37 | * the thrown exception. | |
38 | */ | |
39 | before(Exception exception) : exceptionHandlers (exception) | |
40 | { | |
41 | // Prevent exception concurrency for the System.err | |
42 | 0 | synchronized (exception) |
43 | { | |
44 | ||
45 | // Set log level accordingly to exception type | |
46 | 0 | LogLevel level = (exception instanceof InternalFrameworkException) ? LogLevel.WARN : LogLevel.FATAL; |
47 | ||
48 | // Log exception | |
49 | 0 | printExceptionMessage(level, exception); |
50 | ||
51 | 0 | if (DIFStartupConfiguration.isPrintStackTrace() && !(exception instanceof UserHasNoAccessToStage)) |
52 | 0 | exception.printStackTrace(); |
53 | } | |
54 | 0 | } |
55 | ||
56 | /** | |
57 | * Prints a message with information regarding the exception and the throwing point. | |
58 | * | |
59 | * @param level | |
60 | * the level to log the message | |
61 | * @param exception | |
62 | * the thrown exception | |
63 | */ | |
64 | private void printExceptionMessage(LogLevel level, Throwable exception) | |
65 | { | |
66 | ||
67 | // Initialize message to return | |
68 | 0 | String messageToReturn = null; |
69 | ||
70 | 0 | if (exception instanceof UserHasNoAccessToStage) |
71 | 0 | messageToReturn = exception.getMessage(); |
72 | ||
73 | 0 | else if (getLogger().equals(LogLevel.DEBUG) || getLogger().equals(LogLevel.TRACE) |
74 | 0 | || DIFStartupConfiguration.getDeveloperMode() |
75 | 0 | || DIFStartupConfiguration.getTestingMode()) |
76 | { | |
77 | ||
78 | // Add exception class to message | |
79 | 0 | messageToReturn = "Exception " + exception.getClass() + "\n"; |
80 | ||
81 | // Add context if it's a DiFException | |
82 | 0 | if (exception instanceof DIFException) |
83 | 0 | messageToReturn += ((DIFException) exception).getRenderedExceptionContext(); |
84 | else | |
85 | 0 | messageToReturn += exception.toString(); |
86 | ||
87 | } | |
88 | 0 | else if (exception.getStackTrace().length > 0) |
89 | { | |
90 | 0 | StackTraceElement element = exception.getStackTrace()[0]; |
91 | ||
92 | // Add exception class to message | |
93 | 0 | messageToReturn = element.getFileName() + ": " + exception.getClass().getSimpleName() + " (" |
94 | 0 | + element.getMethodName() + ":" + element.getLineNumber() + ")"; |
95 | } | |
96 | ||
97 | // Log message | |
98 | 0 | getLogger().log(level, messageToReturn); |
99 | 0 | } |
100 | } |