Yes, we can log messages from Camel route, and can define different log levels for each log message.
In order to add logger, first you need to add required additional dependencies to your application.
<dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
Then add log4j.properties or log4j.xml to the application classpath.
log4j.properties
log4j.rootLogger=INFO, out log4j.appender.out=org.apache.log4j.ConsoleAppender log4j.appender.out.layout=org.apache.log4j.PatternLayout log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
Add below line to your properties file to enable debugging of Camel messages
log4j.logger.org.apache.camel=DEBUG
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" /> </layout> </appender> <root> <level value="DEBUG" /> <appender-ref ref="console" /> </root> </log4j:configuration>
Now in camel routes, use below code snippets to add loggers in camel routes
.log(LoggingLevel.DEBUG, "This is an DEBUG log message") .log(LoggingLevel.INFO, "This is an INFO log message") .log(LoggingLevel.ERROR, "This is an ERROR log message")
That’s it. Your application now will start logging messages from camel routes. You can add more log4j attributes as required to this to customized for your application.