在Camel上下文中配置Log4j[英] Configure Log4j in Camel context

本文是小编为大家收集整理的关于在Camel上下文中配置Log4j的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

是否可以配置骆驼路由以将消息发送到特定的log4j记录器?例如,我有以下记录器:

<logger name="com.me.log.mylogger" additivity="false">
    <level value="debug" />
    <appender-ref ref="file_appender_messages" />
</logger>

file_appender_messages只是RollingFileAppender.

然后,我尝试使用骆驼上下文中的以下内容来记录它:

<to uri="log:com.me.log.mylogger?level=INFO" />

但它在命令行上输出,而不是file_appender_messages中指定的日志文件:

25-Oct-2012 11:46:44 org.apache.camel.processor.CamelLogger log
INFO: [MESSAGE BODY]

我希望能够使用dffferent Loggers来获取来自不同来源的消息.我可以在消息处理器中进行操作,但理想情况下它可以在路由XML中进行配置.可以做吗?

推荐答案

我通过将记录器定义为我的应用程序XML文件中的bean

解决了这一点
<bean id="myLogger" class="org.apache.log4j.Logger" factory-method="getLogger">
    <constructor-arg value="com.me.log.mylogger"/>
</bean>

然后在我的路线中,当我要记录消息时,我只是将其引导到bean上的相关方法

<to uri="bean:myLogger?method=info" />

其他推荐答案

骆驼从一段时间以来就使用SLF4J.因此,您必须首先配置SLF4J以将Log4J用作后端.在Maven中添加以下依赖性:

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.6.1</version>
</dependency>
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.16</version>
</dependency>

本文地址:https://www.itbaoku.cn/post/1574948.html

问题描述

Is it possible to configure a Camel route to send a message to a specific log4j logger? For example, I have the following logger:

<logger name="com.me.log.mylogger" additivity="false">
    <level value="debug" />
    <appender-ref ref="file_appender_messages" />
</logger>

file_appender_messages is just a RollingFileAppender.

I then try to log it using the following in my Camel context:

<to uri="log:com.me.log.mylogger?level=INFO" />

But it outputs on the command line instead of the log file specified in file_appender_messages:

25-Oct-2012 11:46:44 org.apache.camel.processor.CamelLogger log
INFO: [MESSAGE BODY]

I would like to be able to use dffferent loggers for messages from different sources. I could do it in my message processors but ideally it could be configured in the route xml. Can it be done?

推荐答案

I fixed this by defining the Logger as a Bean in my application XML file

<bean id="myLogger" class="org.apache.log4j.Logger" factory-method="getLogger">
    <constructor-arg value="com.me.log.mylogger"/>
</bean>

Then in my route, when I want to log the message I just direct it to the relevant method on the Bean

<to uri="bean:myLogger?method=info" />

其他推荐答案

Camel uses slf4j since some time. So you have to first configure slf4j to use log4j as backend. In maven add the following dependencies:

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.6.1</version>
</dependency>
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.16</version>
</dependency>