如何在Apache Camel版本大于等于3中配置路由跟踪?

huangapple go评论84阅读模式
英文:

How to configure route tracing in Apache Camel >= 3?

问题

我正在尝试从Camel 2.X迁移到3.X,并在记录路由跟踪方面遇到了一个问题。
之前我在我的应用程序上下文xml文件中进行了以下配置:

<bean id="camelTracer" class="org.apache.camel.processor.interceptor.Tracer">
    <property name="traceExceptions" value="false" />
    <property name="traceInterceptors" value="true" />
    <property name="logLevel" value="DEBUG" />
    <property name="logName" value="com.mycompany.routing.trace" />
</bean>

<bean id="traceFormatter" class="org.apache.camel.processor.interceptor.DefaultTraceFormatter">
    <property name="showBody" value="true" />
    <property name="maxChars" value="0" />
</bean>

但显然这不再起作用。
从Camel网站的迁移指南中:

“已实现了新的跟踪器,并且已删除了旧的跟踪器。新的跟踪器会将消息记录在org.apache.camel.Tracing记录器名称中,该名称是硬编码的。输出格式也进行了更新以使其更好。跟踪器可以进行自定义。”

如果我在路由的开头设置了.tracing(),它确实会记录跟踪信息。名称是硬编码的,这很好,但我想将级别从INFO更改为DEBUG,以及其他一些内容。

是否有人知道在哪里可以找到关于如何配置这个“新”跟踪器的信息(最好是在applicationContext.xml文件中)?或者其他任何地方,也许在Java DSL路由中?或者是否有可能做到这一点?

谢谢!

英文:

I'm trying to migrate from Camel 2.X to 3.X and have run in to a question about logging the routing trace.
Previously I have configured it like this in my application context xml-file:

&lt;bean id=&quot;camelTracer&quot; class=&quot;org.apache.camel.processor.interceptor.Tracer&quot;&gt;
    &lt;property name=&quot;traceExceptions&quot; value=&quot;false&quot; /&gt;
	&lt;property name=&quot;traceInterceptors&quot; value=&quot;true&quot; /&gt;
	&lt;property name=&quot;logLevel&quot; value=&quot;DEBUG&quot; /&gt;
	&lt;property name=&quot;logName&quot; value=&quot;com.mycompany.routing.trace&quot; /&gt;
&lt;/bean&gt;

&lt;bean id=&quot;traceFormatter&quot; class=&quot;org.apache.camel.processor.interceptor.DefaultTraceFormatter&quot;&gt;
    &lt;property name=&quot;showBody&quot; value=&quot;true&quot; /&gt;
	&lt;property name=&quot;maxChars&quot; value=&quot;0&quot; /&gt;
&lt;/bean&gt;

But that obviously does not work anymore.
From the migration guide on the Camel website:

"A new tracer has been implemented and the old tracer has been removed. The new tracer logs messages at the org.apache.camel.Tracing logger name which is hardcoded. The format of the output is also updated to make it better. The tracer can be customized."

If I set .tracing() at the start of my routes it does log the trace. The name is hardcoded which is fine, but I would like to change the level from INFO to DEBUG among other things.

Does anyone know where to find information on how to configure this "new" tracer (preferrably in an applicationContext.xml file)? Or anywhere else, maybe in the Java DSL route? Or if it is even possible?

Thanks!

答案1

得分: 3

DefaultTracer的日志级别无法通过配置进行更改。您需要实现定制的Tracer并将此实现绑定到注册表。

Tracer:

public class TracerCustom extends DefaultTracer {
    private static final Logger LOG = LoggerFactory.getLogger("com.stackoverflow.camel.TracerCustom");

    @Override
    protected void dumpTrace(String out) {
        LOG.debug(out);
    }
    // 如果需要,可以自定义其他方法
}

Spring上下文:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <bean class="com.stackoverflow.camel.TracerCustom" />

  <camelContext id="tracerCamelContext" xmlns="http://camel.apache.org/schema/spring">
    <route trace="true">
      <from uri="timer:test"/>
      <to uri="log:test"/>
    </route>
  </camelContext>

</beans>
英文:

Logging level of DefaultTracer cannot be changed by configuration. You need to implement customized Tracer and bind this implementation to registry.

Tracer:

public class TracerCustom extends DefaultTracer {
    private static final Logger LOG = LoggerFactory.getLogger(&quot;com.stackoverflow.camel.TracerCustom&quot;);

    @Override
    protected void dumpTrace(String out) {
        LOG.debug(out);
    }
    // Customize other methods if needed
}

Spring context:

&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
       xsi:schemaLocation=&quot;
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd&quot;&gt;

  &lt;bean class=&quot;com.stackoverflow.camel.TracerCustom&quot; /&gt;

  &lt;camelContext id=&quot;tracerCamelContext&quot; xmlns=&quot;http://camel.apache.org/schema/spring&quot;&gt;
    &lt;route trace=&quot;true&quot;&gt;
      &lt;from uri=&quot;timer:test&quot;/&gt;
      &lt;to uri=&quot;log:test&quot;/&gt;
    &lt;/route&gt;
  &lt;/camelContext&gt;

&lt;/beans&gt;

huangapple
  • 本文由 发表于 2020年4月9日 22:56:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/61124063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定