mr nooby noob
mr nooby noob

Reputation: 2253

How to append multiple appender references in Log4j2?

I know in log4j you could use multiple appenders by doing something like:

log4j.logger.com.x=DEBUG, append1, append2

but what would the equivalence be in log4j2?

Would it be this:

logger.com.x.level = DEBUG
logger.com.x.appenderRefs = append1, append2

?

Upvotes: 9

Views: 17428

Answers (2)

LucaMus
LucaMus

Reputation: 731

I know this is a quite old question, but I found a solution by using the following syntax (using your example):

logger.com.x.appenderRef.app1.ref = append1
logger.com.x.appenderRef.app2.ref = append2

In this way everything worked fine.

Upvotes: 8

asch
asch

Reputation: 1963

You did not provide in your example, what are the types of appenders append1 and append2, but this is important, when you are using the properties file. Let's say the appenders are defined as following:

appender.console.type = Console
appender.console.name = append1
...
appender.rolling.type = RollingFile
appender.rolling.name = append2
...

Loggers should be something like this:

logger.console.name = com.x
logger.console.level = debug
logger.console.additivity = false
logger.console.appenderRef.console.ref = append1

logger.rolling.name = com.x
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = append2

I am suggesting this configuration according to the properties file configuration example in the manual. I am not doing this way and prefer the xml format. I recommend you to consider moving to xml format. Most of examples in the log4j2 documentation are for xml configuration format. In case of xml, loggers configuration is much compact and is just like this:

<Loggers>
    <Logger name="com.x" level="debug" additivity="false">
        <appenderRef ref="append1" />
        <appenderRef ref="append2" />
    </Logger>
    ...
</Loggers>

Upvotes: 11

Related Questions