使用Camel 日志组件

x33g5p2x  于2022-09-28 转载在 其他  
字(1.2k)|赞(0)|评价(0)|浏览(703)

Camel 包含一个开箱即用的日志组件,它对调试很有用,可以轻松跟踪流经路由的消息的内容。 通常,此组件仅在开发路线时临时使用,预计将在生产中删除。

让我们看一个如何使用它来记录 Timer 操作的示例:

package com.sample;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;

public class Logger {
  public static void main(String[] args) throws Exception {
    Main main = new Main();
    main.enableHangupSupport();
    main.addRouteBuilder(new RouteLogger());
    main.run(args);
  }
}

class RouteLogger extends RouteBuilder {
  @Override
  public void configure() throws Exception {
    from("timer://simpleTimer?period=1000")
        .setBody(simple("Hello from timer at ${header.firedTime}"))
        .to("log:myLog");
  }
}

结果输出将是:

[hread #0 - timer://simpleTimer] myLog       INFO  Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello from timer at Wed Aug 19 15:43:31 CEST 2015] [hread #0 - timer://simpleTimer] myLog       INFO  Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello from timer at Wed Aug 19 15:43:32 CEST 2015] [hread #0 - timer://simpleTimer] myLog       INFO  Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello from timer at Wed Aug 19 15:43:33 CEST 2015] . . . . . . [hread #0 - timer://simpleTimer] myLog       INFO  Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello from timer at Wed Aug 19 15:44:02 CEST 2015]

请注意,默认情况下,日志组件将以 INFO 日志级别记录,

相关文章