Camel makes it easy to ensure that all uncaught exceptions are logged in a specific way using the logging error handler. You can set this mechanism both globally (at the Camel context level), and at the route level, to log all unhandled exceptions using a log name and level.
This recipe will show you how to use the LoggingErrorHandler
error handler to log errors caught by Camel at runtime.
The Java code for this recipe is located in the org.camelcookbook.error.logging package
. The Spring XML files are located under src/main/resources/META-INF/spring
and prefixed with logging
.
In the XML DSL, define an errorHandler
element within the camelContext
element, and reference the error handler's id
in the errorHandlerRef
attribute of the camelContext
element. The type
attribute of the errorHandler
element must be set to "LoggingErrorHandler"
. You can optionally set the level
(defaults to ERROR
), and logName
(defaults to Logger
) attributes. This will set up the base error handler for all routes defined within this Camel context. Refer to the following code for the same:
<camelContext errorHandlerRef="myErrorHandler" xmlns="http://camel.apache.org/schema/spring"> <errorHandler id="myErrorHandler" type="LoggingErrorHandler" level="ERROR" logName="MyLoggingErrorHandler"/> <route> <from uri="direct:start"/> <!-- ... --> </route> </camelContext>
In the Java DSL, within the RouteBuilder.configure()
method, define an errorHandler
instance that references a LoggingErrorHandler
instance, configured using the DSL's fluent API. You can set the level
attribute (defaults to ERROR
) and logName
(defaults to Logger
). This will configure the base error handler for all routes defined within this RouteBuilder
implementation.
public class LoggingRouteBuilder extends RouteBuilder { @Override public void configure() throws Exception { errorHandler(loggingErrorHandler() .logName("MyLoggingErrorHandler") .level(LoggingLevel.ERROR) ); from("direct:start") //... } }
Camel uses the SLF4J logging library under the covers to configure logging. The SLF4J library allows Camel to work with many different logging implementations. This allows you to configure runtime properties according to the implementation being used, for example, within the log4j.properties
file, if Log4j
is used.
Setting the logName
attribute is equivalent to calling SLF4J's LoggerFactory.getLogger(logName)
. This allows you to quickly find and automatically filter log messages by using this String, which helps you determine what is happening within your integration.
For example, here is a log entry based on the preceding sample code. Notice how our logName
setting is accessible within the log:
2013-05-28 07:21:36,913 [main ] ERROR MyLoggingErrorHandler - Failed delivery for (MessageId: ID-grelber-local-56780-1369740096617-0-3 on ExchangeId: ID-grelber-local-56780-1369740096617-0-4). Exhausted after delivery attempt: 1 caught: org.camelcookbook.error.logging.FlakyException: FlakyProcessor has gone Flaky
It is also possible to set route-specific error handlers.
In the XML DSL, you can set the errorHandlerRef
attribute of the route
element using the following code:
<camelContext xmlns="http://camel.apache.org/schema/spring"> <errorHandler id="myRouteSpecificLogging" type="LoggingErrorHandler" level="ERROR" logName="MyRouteLogging"/> <route errorHandlerRef="myRouteSpecificLogging"> <from uri="direct:start"/> <!-- ... --> </route> </camelContext>
In the Java DSL, you specify the route-specific error handler after the from
part of the route definition:
from("direct:start") .errorHandler(loggingErrorHandler() .logName("MyRouteLogging") .level(LoggingLevel.ERROR) ) .bean(FlakyProcessor.class) .to("mock:result");
3.133.158.36