Logback: Logging with two loggers

Posted by gammay on Stack Overflow See other posts from Stack Overflow or by gammay
Published on 2014-06-12T09:20:35Z Indexed on 2014/06/12 9:24 UTC
Read the original article Hit count: 217

Filed under:
|
|

I would like to use slf4j+logback for two purposes in my application - log and audit.

For logging, I log the normal way:

static final Logger logger = LoggerFactory.getLogger(Main.class);
logger.debug("-> main()");

For Audit, I create a special named logger and log to it:

static final Logger logger = LoggerFactory.getLogger("AUDIT_LOGGER");
Object[] params =
    { new Integer(1) /* TenantID */, new Integer(10) /* UserID */, msg};
logger.info("{}|{}|{}", params);

logback configuration:

<logger name="AUDIT_LOGGER" level="info">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS}|%msg%n
            </pattern>
        </encoder>
    </appender>
</logger>

<root level="all">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>
</root>

Problem: Messages logged through audit logger appear twice - once under the AUDIT_LOGGER and once under the root logger.

14:41:57.975 [main] DEBUG com.gammay.example.Main - -> main()

14:41:57.978|1|10|welcome to main

14:41:57.978 [main] INFO AUDIT_LOGGER - 1|10|welcome to main

How can I make sure audit messages appear only once under the audit logger?

© Stack Overflow or respective owner

Related posts about java

Related posts about slf4j