214.8. 完整自定义日志输出
可从 Camel 2.11 开始
使用 #Formatting 部分概述的选项,您可以控制日志记录器的大部分输出。但是,日志行始终遵循这个结构:
Exchange[Id:ID-machine-local-50656-1234567901234-1-2, ExchangePattern:InOut,
Properties:{CamelToEndpoint=log://org.apache.camel.component.log.TEST?showAll=true,
CamelCreatedTimestamp=Thu Mar 28 00:00:00 WET 2013},
Headers:{breadcrumbId=ID-machine-local-50656-1234567901234-1-1}, BodyType:String, Body:Hello World, Out: null]
Exchange[Id:ID-machine-local-50656-1234567901234-1-2, ExchangePattern:InOut,
Properties:{CamelToEndpoint=log://org.apache.camel.component.log.TEST?showAll=true,
CamelCreatedTimestamp=Thu Mar 28 00:00:00 WET 2013},
Headers:{breadcrumbId=ID-machine-local-50656-1234567901234-1-1}, BodyType:String, Body:Hello World, Out: null]
这个格式在某些情况下不知道,您可能需要…
- … 将输出的标头和属性过滤为在 insight 和详细程度间的平衡。
- … 将日志消息调整为您认为最可读的任何内容。
- … 通过日志 mining 系统(如 Splunk)为摘要定制日志消息。
- … 打印特定的正文类型不同。
- … etc.
每次需要绝对自定义时,您可以创建一个实施 ExchangeFormatter 接口的类。在 格式为(Exchange) 方法中,您可以选择并提取您需要的精确信息,以自定义方式对其进行格式化并返回。返回值将成为最终日志消息。
您可以通过以下两种方式之一获取您的自定义 ExchangeFormatter :
在 registry 中明确实例化 LogComponent:
<bean name="log" class="org.apache.camel.component.log.LogComponent"> <property name="exchangeFormatter" ref="myCustomFormatter" /> </bean>
<bean name="log" class="org.apache.camel.component.log.LogComponent">
<property name="exchangeFormatter" ref="myCustomFormatter" />
</bean>
214.8.1. 配置间的约定:* 复制链接链接已复制到粘贴板!
简单地使用名称 logFormatter 注册 Bean;Log 组件足以自动获取它。
<bean name="logFormatter" class="com.xyz.MyCustomExchangeFormatter" />
<bean name="logFormatter" class="com.xyz.MyCustomExchangeFormatter" />
ExchangeFormatter 应用到 该 Camel 上下文 内的所有日志端点。如果您需要不同端点的不同 ExchangeFormatter,请根据需要多次实例化 LogComponent,并使用相关的 bean 名称作为端点前缀。
在使用自定义日志格式器时,从 Camel 2.11.2/2.12 开始,您可以在 log uri 中指定参数,该参数在自定义日志格式器上配置。虽然当您这样做时,您应将"logFormatter"定义为具有不同参数(例如:
<bean name="logFormatter" class="com.xyz.MyCustomExchangeFormatter" scope="prototype"/>
<bean name="logFormatter" class="com.xyz.MyCustomExchangeFormatter" scope="prototype"/>
然后,我们可以使用带有不同选项的日志 uri 来使用 Camel 路由:
<to uri="log:foo?param1=foo&param2=100"/> <to uri="log:bar?param1=bar&param2=200"/>
<to uri="log:foo?param1=foo&param2=100"/>
<to uri="log:bar?param1=bar&param2=200"/>