问题
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