Search code examples
exceptionloggingapache-camelapache-karaf

How can you set the logger name in Camel's errorHandler with type DeadLetterChannel?


I trying to specify logger name for errorHandler with type DeadLetterChannel and have error about I can't use logName for this.

Is it possible to specify logger name?

or maybe it's bad practice use own logger name for dlc?

my config

<errorHandler id="myDeadLetterChannel" type="DeadLetterChannel" deadLetterUri="direct:deadLetterChannel" logName="MYLOGGERNAME">
    <redeliveryPolicy maximumRedeliveries="5" retryAttemptedLogLevel="INFO" backOffMultiplier="2" useExponentialBackOff="true" />
</errorHandler>

Actual log message:

13:31:46,947 | INFO  | evMasterDbEvent] | DeadLetterChannel                176 | 144 - org.apache.camel.camel-core - 2.12.0.redhat-610379 | Failed delivery for (MessageId: ID:tetra-backend-43331-1577539405267-13:6:2:1:1 on ExchangeId: ID-tetra-backend-38269-1577539416633-5-5). On delivery attempt: 0 caught: com.x.exception.BusinessLogicException: Unexpected event type ZZZ

Expected log message:

13:31:46,947 | INFO  | evMasterDbEvent] | MYLOGGERNAME                176 | 144 - org.apache.camel.camel-core - 2.12.0.redhat-610379 | Failed delivery for (MessageId: ID:tetra-backend-43331-1577539405267-13:6:2:1:1 on ExchangeId: ID-tetra-backend-38269-1577539416633-5-5). On delivery attempt: 0 caught: com.x.exception.BusinessLogicException: Unexpected event type ZZZ

I also tried add CamelLogEipName and this way not change logger name in my situation...

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <properties>
    <property key="CamelLogEipName" value="MYLOGGERNAME"/>
</properties>

Solution

  • I make/found some solution but maybe Apache Camel support better solution?

    Config:

    <bean id="myRedeliveryPolicyConfig" class="org.apache.camel.processor.RedeliveryPolicy">
        <property name="maximumRedeliveries" value="3" />
        <property name="backOffMultiplier" value="2" />
        <property name="useExponentialBackOff" value="true" />
        <property name="retryAttemptedLogLevel" value="WARN" />
    </bean>
    
    <bean id="myDeadLetterChannel" class="com.x.MyDeadLetterChannelBuilder">
        <property name="deadLetterUri" value="direct:deadLetterChannel"/>
        <property name="redeliveryPolicy" ref="myRedeliveryPolicyConfig"/>
        <property name="loggerName" value="MYLOGGERNAME"/>
    </bean>
    
        <camelContext xmlns="http://camel.apache.org/schema/spring">
            <route id="myEventTriggerRoute" errorHandlerRef="zDeadLetterChannel">
                <from uri="{{my.jmsVirtualTopic.myEventConsumer}}" />
                <process ref="myDbEventProcessor" />
            </route>
    
            <route>
                <from uri="direct:deadLetterChannel" />
                <log loggingLevel="INFO" message="Message added to dead letter queue" />
                <to uri="{{my.jms.myEventDeadLetterQueueIn}}" />
            </route>
        </camelContext>
    

    Custom DeadLetterChannelBuilder

    import org.apache.camel.LoggingLevel;
    import org.apache.camel.builder.DeadLetterChannelBuilder;
    import org.apache.camel.util.CamelLogger;
    import org.slf4j.LoggerFactory;
    import lombok.Getter;
    import lombok.Setter;
    
    @Getter
    @Setter
    public class MyDeadLetterChannelBuilder extends DeadLetterChannelBuilder {
    
        private String loggerName;
    
        @Override
        protected CamelLogger createLogger() {
            return new CamelLogger(LoggerFactory.getLogger(loggerName), LoggingLevel.ERROR);
        }
    }