001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.processor;
018
019 import org.apache.camel.Exchange;
020 import org.apache.camel.Processor;
021 import org.apache.camel.util.ServiceHelper;
022 import org.apache.commons.logging.Log;
023 import org.apache.commons.logging.LogFactory;
024
025 /**
026 * An {@link ErrorHandler} which uses commons-logging to dump the error
027 *
028 * @version $Revision: 46137 $
029 */
030 public class LoggingErrorHandler extends ErrorHandlerSupport {
031 private Processor output;
032 private Log log;
033 private LoggingLevel level;
034
035 public LoggingErrorHandler(Processor output) {
036 this(output, LogFactory.getLog(LoggingErrorHandler.class), LoggingLevel.INFO);
037 }
038
039 public LoggingErrorHandler(Processor output, Log log, LoggingLevel level) {
040 this.output = output;
041 this.log = log;
042 this.level = level;
043 }
044
045 @Override
046 public String toString() {
047 return "LoggingErrorHandler[" + output + "]";
048 }
049
050 public void process(Exchange exchange) throws Exception {
051 Throwable error = null;
052 try {
053 output.process(exchange);
054
055 // could also fail and set exception on the exchange itself
056 if (exchange.getException() != null) {
057 error = exchange.getException();
058 }
059 } catch (Throwable e) {
060 error = e;
061 }
062
063 if (error != null) {
064 if (!customProcessorForException(exchange, error)) {
065 logError(exchange, error);
066 }
067 }
068 }
069
070 // Properties
071 // -------------------------------------------------------------------------
072
073 /**
074 * Returns the output processor
075 */
076 public Processor getOutput() {
077 return output;
078 }
079
080 public LoggingLevel getLevel() {
081 return level;
082 }
083
084 public void setLevel(LoggingLevel level) {
085 this.level = level;
086 }
087
088 public Log getLog() {
089 return log;
090 }
091
092 public void setLog(Log log) {
093 this.log = log;
094 }
095
096 // Implementation methods
097 // -------------------------------------------------------------------------
098 protected void logError(Exchange exchange, Throwable e) {
099 switch (level) {
100 case DEBUG:
101 if (log.isDebugEnabled()) {
102 log.debug(logMessage(exchange, e), e);
103 }
104 break;
105 case ERROR:
106 if (log.isErrorEnabled()) {
107 log.error(logMessage(exchange, e), e);
108 }
109 break;
110 case FATAL:
111 if (log.isFatalEnabled()) {
112 log.fatal(logMessage(exchange, e), e);
113 }
114 break;
115 case INFO:
116 if (log.isInfoEnabled()) {
117 log.info(logMessage(exchange, e), e);
118 }
119 break;
120 case TRACE:
121 if (log.isTraceEnabled()) {
122 log.trace(logMessage(exchange, e), e);
123 }
124 break;
125 case WARN:
126 if (log.isWarnEnabled()) {
127 log.warn(logMessage(exchange, e), e);
128 }
129 break;
130 default:
131 log.error("Unknown level: " + level + " when trying to log exchange: " + logMessage(exchange, e),
132 e);
133 }
134 }
135
136 protected Object logMessage(Exchange exchange, Throwable e) {
137 return e + " while processing exchange: " + exchange;
138 }
139
140 protected void doStart() throws Exception {
141 ServiceHelper.startServices(output);
142 }
143
144 protected void doStop() throws Exception {
145 ServiceHelper.stopServices(output);
146 }
147 }