Configure Log4j in Camel context

匆匆过客 提交于 2019-12-08 01:59:47

问题


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?


回答1:


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>



回答2:


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" />


来源:https://stackoverflow.com/questions/13066962/configure-log4j-in-camel-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!