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