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.interceptor; 018 019 import org.apache.camel.Exchange; 020 import org.apache.camel.Predicate; 021 import org.apache.camel.Processor; 022 import org.apache.camel.model.ProcessorType; 023 import org.apache.camel.processor.DelegateProcessor; 024 import org.apache.camel.processor.Logger; 025 import org.apache.camel.processor.LoggingLevel; 026 import org.apache.commons.logging.Log; 027 import org.apache.commons.logging.LogFactory; 028 029 /** 030 * An interceptor for debugging and tracing routes 031 * 032 * @version $Revision: 669650 $ 033 */ 034 public class TraceInterceptor extends DelegateProcessor implements ExchangeFormatter { 035 private final ProcessorType node; 036 private Predicate traceFilter; 037 private boolean traceExceptions = true; 038 private Logger logger = new Logger(LogFactory.getLog(TraceInterceptor.class), this); 039 private TraceFormatter formatter; 040 041 public TraceInterceptor(ProcessorType node, Processor target, TraceFormatter formatter) { 042 super(target); 043 this.node = node; 044 this.formatter = formatter; 045 } 046 047 @Override 048 public String toString() { 049 return "TraceInterceptor[" + node + "]"; 050 } 051 052 public void process(Exchange exchange) throws Exception { 053 try { 054 if (shouldLogExchange(exchange)) { 055 logExchange(exchange); 056 } 057 super.proceed(exchange); 058 } catch (Exception e) { 059 logException(exchange, e); 060 throw e; 061 } catch (Error e) { 062 logException(exchange, e); 063 throw e; 064 } 065 } 066 067 public Object format(Exchange exchange) { 068 return formatter.format(this, exchange); 069 } 070 071 // Properties 072 //------------------------------------------------------------------------- 073 public ProcessorType getNode() { 074 return node; 075 } 076 077 public Predicate getTraceFilter() { 078 return traceFilter; 079 } 080 081 public void setTraceFilter(Predicate traceFilter) { 082 this.traceFilter = traceFilter; 083 } 084 085 public boolean isTraceExceptions() { 086 return traceExceptions; 087 } 088 089 public void setTraceExceptions(boolean traceExceptions) { 090 this.traceExceptions = traceExceptions; 091 } 092 093 public Logger getLogger() { 094 return logger; 095 } 096 097 public TraceFormatter getFormatter() { 098 return formatter; 099 } 100 101 public void setFormatter(TraceFormatter formatter) { 102 this.formatter = formatter; 103 } 104 105 public LoggingLevel getLevel() { 106 return getLogger().getLevel(); 107 } 108 109 public Log getLog() { 110 return getLogger().getLog(); 111 } 112 113 public void setLog(Log log) { 114 getLogger().setLog(log); 115 } 116 117 public void setLevel(LoggingLevel level) { 118 getLogger().setLevel(level); 119 } 120 121 // Implementation methods 122 //------------------------------------------------------------------------- 123 protected void logExchange(Exchange exchange) { 124 logger.process(exchange); 125 } 126 127 protected void logException(Exchange exchange, Throwable throwable) { 128 logger.process(exchange, throwable); 129 } 130 131 132 /** 133 * Returns true if the given exchange should be logged in the trace list 134 */ 135 protected boolean shouldLogExchange(Exchange exchange) { 136 return traceFilter == null || traceFilter.matches(exchange); 137 } 138 139 }