1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.processor; |
19 |
|
|
20 |
|
import org.apache.camel.Exchange; |
21 |
|
import org.apache.camel.Processor; |
22 |
|
import org.apache.camel.impl.ServiceSupport; |
23 |
|
import org.apache.camel.util.ServiceHelper; |
24 |
|
import org.apache.commons.logging.Log; |
25 |
|
import org.apache.commons.logging.LogFactory; |
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
public class LoggingErrorHandler extends ServiceSupport implements ErrorHandler { |
33 |
|
private Processor output; |
34 |
|
private Log log; |
35 |
|
private LoggingLevel level; |
36 |
|
|
37 |
|
public LoggingErrorHandler(Processor output) { |
38 |
0 |
this(output, LogFactory.getLog(LoggingErrorHandler.class), LoggingLevel.INFO); |
39 |
0 |
} |
40 |
|
|
41 |
1 |
public LoggingErrorHandler(Processor output, Log log, LoggingLevel level) { |
42 |
1 |
this.output = output; |
43 |
1 |
this.log = log; |
44 |
1 |
this.level = level; |
45 |
1 |
} |
46 |
|
|
47 |
|
@Override |
48 |
|
public String toString() { |
49 |
2 |
return "LoggingErrorHandler[" + output + "]"; |
50 |
|
} |
51 |
|
|
52 |
|
public void process(Exchange exchange) throws Exception { |
53 |
|
try { |
54 |
0 |
output.process(exchange); |
55 |
|
} |
56 |
0 |
catch (RuntimeException e) { |
57 |
0 |
logError(exchange, e); |
58 |
0 |
} |
59 |
0 |
} |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
public Processor getOutput() { |
68 |
1 |
return output; |
69 |
|
} |
70 |
|
|
71 |
|
public LoggingLevel getLevel() { |
72 |
0 |
return level; |
73 |
|
} |
74 |
|
|
75 |
|
public void setLevel(LoggingLevel level) { |
76 |
0 |
this.level = level; |
77 |
0 |
} |
78 |
|
|
79 |
|
public Log getLog() { |
80 |
0 |
return log; |
81 |
|
} |
82 |
|
|
83 |
|
public void setLog(Log log) { |
84 |
0 |
this.log = log; |
85 |
0 |
} |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
protected void logError(Exchange exchange, RuntimeException e) { |
90 |
0 |
switch (level) { |
91 |
|
case DEBUG: |
92 |
0 |
if (log.isDebugEnabled()) { |
93 |
0 |
log.debug(logMessage(exchange, e), e); |
94 |
0 |
} |
95 |
|
break; |
96 |
|
case ERROR: |
97 |
0 |
if (log.isErrorEnabled()) { |
98 |
0 |
log.error(logMessage(exchange, e), e); |
99 |
0 |
} |
100 |
|
break; |
101 |
|
case FATAL: |
102 |
0 |
if (log.isFatalEnabled()) { |
103 |
0 |
log.fatal(logMessage(exchange, e), e); |
104 |
0 |
} |
105 |
|
break; |
106 |
|
case INFO: |
107 |
0 |
if (log.isInfoEnabled()) { |
108 |
0 |
log.debug(logMessage(exchange, e), e); |
109 |
0 |
} |
110 |
|
break; |
111 |
|
case TRACE: |
112 |
0 |
if (log.isTraceEnabled()) { |
113 |
0 |
log.trace(logMessage(exchange, e), e); |
114 |
0 |
} |
115 |
|
break; |
116 |
|
case WARN: |
117 |
0 |
if (log.isWarnEnabled()) { |
118 |
0 |
log.warn(logMessage(exchange, e), e); |
119 |
0 |
} |
120 |
|
break; |
121 |
|
default: |
122 |
0 |
log.error("Unknown level: " + level + " when trying to log exchange: " + logMessage(exchange, e), e); |
123 |
|
} |
124 |
0 |
} |
125 |
|
|
126 |
|
protected Object logMessage(Exchange exchange, RuntimeException e) { |
127 |
0 |
return e + " while processing exchange: " + exchange; |
128 |
|
} |
129 |
|
|
130 |
|
protected void doStart() throws Exception { |
131 |
0 |
ServiceHelper.startServices(output); |
132 |
0 |
} |
133 |
|
|
134 |
|
protected void doStop() throws Exception { |
135 |
0 |
ServiceHelper.stopServices(output); |
136 |
0 |
} |
137 |
|
} |