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.model.LoggingLevel; 022 import org.apache.camel.util.ServiceHelper; 023 import org.apache.commons.logging.Log; 024 import org.apache.commons.logging.LogFactory; 025 026 /** 027 * An {@link ErrorHandler} which uses commons-logging to dump the error 028 * 029 * @version $Revision: 61064 $ 030 */ 031 public class LoggingErrorHandler extends ErrorHandlerSupport { 032 private Processor output; 033 private Log log; 034 private LoggingLevel level; 035 036 public LoggingErrorHandler(Processor output) { 037 this(output, LogFactory.getLog(LoggingErrorHandler.class), LoggingLevel.INFO); 038 } 039 040 public LoggingErrorHandler(Processor output, Log log, LoggingLevel level) { 041 this.output = output; 042 this.log = log; 043 this.level = level; 044 } 045 046 @Override 047 public String toString() { 048 return "LoggingErrorHandler[" + output + "]"; 049 } 050 051 public void process(Exchange exchange) throws Exception { 052 Throwable error = null; 053 try { 054 output.process(exchange); 055 056 // could also fail and set exception on the exchange itself 057 if (exchange.getException() != null) { 058 error = exchange.getException(); 059 } 060 } catch (Throwable e) { 061 error = e; 062 } 063 064 if (error != null) { 065 if (!customProcessorForException(exchange, error)) { 066 logError(exchange, error); 067 } 068 } 069 } 070 071 // Properties 072 // ------------------------------------------------------------------------- 073 074 /** 075 * Returns the output processor 076 */ 077 public Processor getOutput() { 078 return output; 079 } 080 081 public LoggingLevel getLevel() { 082 return level; 083 } 084 085 public void setLevel(LoggingLevel level) { 086 this.level = level; 087 } 088 089 public Log getLog() { 090 return log; 091 } 092 093 public void setLog(Log log) { 094 this.log = log; 095 } 096 097 // Implementation methods 098 // ------------------------------------------------------------------------- 099 protected void logError(Exchange exchange, Throwable e) { 100 switch (level) { 101 case DEBUG: 102 if (log.isDebugEnabled()) { 103 log.debug(logMessage(exchange, e), e); 104 } 105 break; 106 case ERROR: 107 if (log.isErrorEnabled()) { 108 log.error(logMessage(exchange, e), e); 109 } 110 break; 111 case FATAL: 112 if (log.isFatalEnabled()) { 113 log.fatal(logMessage(exchange, e), e); 114 } 115 break; 116 case INFO: 117 if (log.isInfoEnabled()) { 118 log.info(logMessage(exchange, e), e); 119 } 120 break; 121 case TRACE: 122 if (log.isTraceEnabled()) { 123 log.trace(logMessage(exchange, e), e); 124 } 125 break; 126 case WARN: 127 if (log.isWarnEnabled()) { 128 log.warn(logMessage(exchange, e), e); 129 } 130 break; 131 default: 132 log.error("Unknown level: " + level + " when trying to log exchange: " + logMessage(exchange, e), 133 e); 134 } 135 } 136 137 protected Object logMessage(Exchange exchange, Throwable e) { 138 return e + " while processing exchange: " + exchange; 139 } 140 141 protected void doStart() throws Exception { 142 ServiceHelper.startServices(output); 143 } 144 145 protected void doStop() throws Exception { 146 ServiceHelper.stopServices(output); 147 } 148 }