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 java.util.List; 020 021 import org.apache.camel.CamelContext; 022 import org.apache.camel.Exchange; 023 import org.apache.camel.Predicate; 024 import org.apache.camel.Processor; 025 import org.apache.camel.impl.DefaultCamelContext; 026 import org.apache.camel.model.LoggingLevel; 027 import org.apache.camel.model.ProcessorType; 028 import org.apache.camel.spi.InterceptStrategy; 029 030 /** 031 * An interceptor strategy for tracing routes 032 * 033 * @version $Revision: 62931 $ 034 */ 035 public class Tracer implements InterceptStrategy { 036 037 private TraceFormatter formatter = new TraceFormatter(); 038 private boolean enabled = true; 039 private String logName; 040 private LoggingLevel logLevel; 041 private Predicate<Exchange> traceFilter; 042 private boolean traceInterceptors; 043 private boolean traceExceptions = true; 044 private boolean traceOutExchanges; 045 046 /** 047 * A helper method to return the Tracer instance for a given {@link CamelContext} if one is enabled 048 * 049 * @param context the camel context the tracer is connected to 050 * @return the tracer or null if none can be found 051 */ 052 public static Tracer getTracer(CamelContext context) { 053 if (context instanceof DefaultCamelContext) { 054 DefaultCamelContext defaultCamelContext = (DefaultCamelContext) context; 055 List<InterceptStrategy> list = defaultCamelContext.getInterceptStrategies(); 056 for (InterceptStrategy interceptStrategy : list) { 057 if (interceptStrategy instanceof Tracer) { 058 return (Tracer)interceptStrategy; 059 } 060 } 061 } 062 return null; 063 } 064 065 public Processor wrapProcessorInInterceptors(ProcessorType processorType, Processor target) throws Exception { 066 // Force the creation of an id, otherwise the id is not available when the trace formatter is 067 // outputting trace information 068 String id = processorType.idOrCreate(); 069 return new TraceInterceptor(processorType, target, this); 070 } 071 072 public TraceFormatter getFormatter() { 073 return formatter; 074 } 075 076 public void setFormatter(TraceFormatter formatter) { 077 this.formatter = formatter; 078 } 079 080 public void setEnabled(boolean flag) { 081 enabled = flag; 082 } 083 084 public boolean isEnabled() { 085 return enabled; 086 } 087 088 public boolean isTraceInterceptors() { 089 return traceInterceptors; 090 } 091 092 /** 093 * Sets whether interceptors should be traced or not 094 */ 095 public void setTraceInterceptors(boolean traceInterceptors) { 096 this.traceInterceptors = traceInterceptors; 097 } 098 099 public Predicate getTraceFilter() { 100 return traceFilter; 101 } 102 103 /** 104 * Sets a predicate to be used as filter when tracing 105 */ 106 public void setTraceFilter(Predicate traceFilter) { 107 this.traceFilter = traceFilter; 108 } 109 110 public LoggingLevel getLogLevel() { 111 return logLevel; 112 } 113 114 /** 115 * Sets the logging level to output tracing. Will use <tt>INFO</tt> level by default. 116 */ 117 public void setLogLevel(LoggingLevel logLevel) { 118 this.logLevel = logLevel; 119 } 120 121 public boolean isTraceExceptions() { 122 return traceExceptions; 123 } 124 125 /** 126 * Sets whether thrown exceptions should be traced 127 */ 128 public void setTraceExceptions(boolean traceExceptions) { 129 this.traceExceptions = traceExceptions; 130 } 131 132 public String getLogName() { 133 return logName; 134 } 135 136 /** 137 * Sets the logging name to use. 138 * Will default use <tt>org.apache.camel.processor.interceptor.TraceInterceptor<tt>. 139 */ 140 public void setLogName(String logName) { 141 this.logName = logName; 142 } 143 144 /** 145 * Sets whether exchanges coming out of processors should be traced 146 */ 147 public void setTraceOutExchanges(boolean traceOutExchanges) { 148 this.traceOutExchanges = traceOutExchanges; 149 } 150 151 public boolean isTraceOutExchanges() { 152 return traceOutExchanges; 153 } 154 }