简体   繁体   中英

Log4j2: Redirect logging statements of a class and its call chain

I'm using an SQS Handling library, which polls an SQS queue for messages and calls my defined handler. I wanted to redirect all its logging statements (and log statements in its call chain) into a separate log file.

I defined a new appender in my log4j2 config file - and added it to the logger configuration -

    <Logger name="com.my.sqs.handler" level="DEBUG" additivity="false">
        <AppenderRef ref="SQSCONSUMER" level="DEBUG"/>
    </Logger>

But I can't figure out how to redirect all the log statements by the objects invoked within the SQSHandler call chain to the SQSCONSUMER log - how can I do this?

Each time your handler is called, you can put some key-value pair in the Log4j2 ThreadContext.

This gives you multiple options: there is a RoutingAppender which can forward log events with some ThreadContext key to one appender and other log events to another appender. You can also use a Filter that accepts or denies log events based on their context data (containing the key you set).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM