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.LoggingLevel; 021 import org.apache.camel.Processor; 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: 765920 $ 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 boolean supportTransacted() { 052 return false; 053 } 054 055 public void process(Exchange exchange) throws Exception { 056 Throwable error = null; 057 try { 058 output.process(exchange); 059 060 // could also fail and set exception on the exchange itself 061 if (exchange.getException() != null) { 062 error = exchange.getException(); 063 } 064 } catch (Exception e) { 065 error = e; 066 } 067 068 if (error != null) { 069 if (!customProcessorForException(exchange, error)) { 070 logError(exchange, error); 071 } 072 } 073 } 074 075 // Properties 076 // ------------------------------------------------------------------------- 077 078 /** 079 * Returns the output processor 080 */ 081 public Processor getOutput() { 082 return output; 083 } 084 085 public LoggingLevel getLevel() { 086 return level; 087 } 088 089 public void setLevel(LoggingLevel level) { 090 this.level = level; 091 } 092 093 public Log getLog() { 094 return log; 095 } 096 097 public void setLog(Log log) { 098 this.log = log; 099 } 100 101 // Implementation methods 102 // ------------------------------------------------------------------------- 103 protected void logError(Exchange exchange, Throwable e) { 104 switch (level) { 105 case DEBUG: 106 if (log.isDebugEnabled()) { 107 log.debug(logMessage(exchange, e), e); 108 } 109 break; 110 case ERROR: 111 if (log.isErrorEnabled()) { 112 log.error(logMessage(exchange, e), e); 113 } 114 break; 115 case FATAL: 116 if (log.isFatalEnabled()) { 117 log.fatal(logMessage(exchange, e), e); 118 } 119 break; 120 case INFO: 121 if (log.isInfoEnabled()) { 122 log.info(logMessage(exchange, e), e); 123 } 124 break; 125 case TRACE: 126 if (log.isTraceEnabled()) { 127 log.trace(logMessage(exchange, e), e); 128 } 129 break; 130 case WARN: 131 if (log.isWarnEnabled()) { 132 log.warn(logMessage(exchange, e), e); 133 } 134 break; 135 default: 136 log.error("Unknown level: " + level + " when trying to log exchange: " + logMessage(exchange, e), e); 137 } 138 } 139 140 protected Object logMessage(Exchange exchange, Throwable e) { 141 return e.getMessage() + " while processing exchange: " + exchange; 142 } 143 144 protected void doStart() throws Exception { 145 ServiceHelper.startServices(output); 146 } 147 148 protected void doStop() throws Exception { 149 ServiceHelper.stopServices(output); 150 } 151 }