Apache camel bindy - 如何记录或调试 [英] Apache camel bindy - how to log or debug
问题描述
我正在尝试从骆驼绑定中获取一些日志信息.我有一个使用 BindyCsvDataFormat bindyProduct 的工作设置,带有带注释的 product-bean 和一个 csv 文件作为源.
I'm trying to get some log informations from camel bindy. I had a working setup using an BindyCsvDataFormat bindyProduct with an annotated product-bean and an csv file as source.
现在我更改了 CSV 文件和带注释的 bean.处理接缝卡在绑定处理器中,但我没有得到任何信息/日志.我的 debugProcessor 根本没有到达.如果我把它放在解组步骤之前,那么它会记录一些东西,我可以调试它.我想知道为什么新文件不再适合/匹配,为什么没有日志或异常或任何有帮助的东西.
Now I changed the CSV file and the annotated bean. The processing seams to get stuck within the bindy processor, but I do not get any Informations/logs. My debugProcessor is not reached at all. If I put it before the unmarshal step, then it logs some stuff and I can debug into it. I wonder why the new files do not fit / match any more and why there are no logs OR exceptions or whatever would be of an help.
from("file:csv-testdata")
.unmarshal(bindyProduct)
.process(debugProcessor)
提前致谢AJ
推荐答案
在 apache camel 中记录异常
当我问这个问题时,我刚开始骑骆驼.与此同时,我发现了一些很好的方法来实现,我一直在寻找.因此,如果其他人正在寻找这个问题的答案:
Logging of Exceptions in apache camel
When I asked the question, I was at the very beginning of my camel riding experience. In the meantime I found out some great ways to to achieve, what I was looking for. So if someone else is searching for an answer to this question:
1.1 添加日志记录到 pom.xml 的依赖项,例如
1.1 Add dependencies for logging to your pom.xml e.g
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
1.2 将 log4j.properties 文件添加到您的 src/main/resources 文件夹(如果您想使用 log4j).在那里定义日志级别、附加程序等
1.2 Add log4j.properties file to your src/main/resources folder (if you want to use log4j). Define loglevel, appenders etc. there
1.3 将日志添加到您的路由中
1.3 add the logging to your route
from("...")
.unmarshal(bindyProduct)
.to("log:com.your.package.YourChosenName?level=ERROR&multiline=true&showCaughtException=true")
.to(...);
可以在此处找到所有选项的列表http://camel.apache.org/log.html你可以使用
a list of all options can be found here http://camel.apache.org/log.html you can use
&showAll=true
记录所有信息,如标题、属性、正文等
to log all Informations like headers, properties, body etc
请记住,URLlevel="选项的日志级别的严重性必须等于或高于您在 log4j.properties 文件中定义的级别
Keep in mind, that the severity of the loglevel of the URLs "level=" option must be equal or higher than the one, you defined in your log4j.properties file
2.1 编写一个DebugProcessor处理器必须实现public void process(Exchange exchange) 抛出异常 {
2.1 write an DebugProcessor The Processor has to implement public void process(Exchange exchange) throws Exception {
如果您想知道为什么在
exchange.getException()
尝试使用检索它
Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
用这个例外做你喜欢做的事(登录到系统输出,...)
do what ever you like with this exception (log to sysout, ...)
2.2 将调试器连接到 RouteBuilder Java 类(或其他地方,Spring、XML 等)中的路由
2.2 Wire your debugger into your route within your RouteBuilder Java-Class (or elsewhere, Spring, XML, ...)
DebugProcessor debugProcessor1 = new DebugProcessor();
from("...")
.unmarshal(bindyProduct)
.process(debugProcessor1)
.to(...);
3.第三种方法
使用骆驼 .doTry()/.doCatch() 或(也许更好)使用 onException() 路由
(http://camel.apache.org/exception-clause.html/camel.apache.org/try-catch-finally.html)
3. Third approach
Use camels .doTry()/.doCatch() or (maybe even better) use an onException() route
(http://camel.apache.org/exception-clause.html / camel.apache.org/try-catch-finally.html)
3.1 构建一个与要调试的路由分开的 onException() 路由
3.1 Build an onException() route separated from the route you want to debug
以下 onException 路由聚合异常并以一种很好的、人类可读的方式(通过速度模板)记录它们,每 5 秒或 10 次:
The following onException route aggregates exceptions and writes them down a nice, human readable way (via velocity template) , every 5 seconds or 10 exeptions:
onException(Exception.class) // OR a special excepion (io, etc)
.aggregate(header("CamelFileParent"),
new ExceptionAggregationStrategy())
.completionSize(10).completionTimeout(5000)
.to("velocity:velocity/errors.log.vm")
.to("file:camel/xml-out?fileName=errors-${file:name.noext}-${date:now:yyyy-MM-dd_HH-mm-ss-SSS}.log");
这只是一个例子,你可以做任何你想做的事情,而骆驼也能做到,例如将它们作为邮件发送,将它们放入数据库等.
This is just an example, you can do what ever you want and camel is able to, e.g. send them as mail, put them in a DB etc.
您现在不需要任何额外的处理器或在您的路线中记录东西:
You don't need any extra processors or logging stuff in your route now:
from("...")
.unmarshal(bindyProduct)
.to(...);
这篇关于Apache camel bindy - 如何记录或调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!