Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

?

like image 766
mr nooby noob Avatar asked Sep 23 '16 00:09

mr nooby noob


2 Answers

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>
like image 186
asch Avatar answered Dec 01 '22 06:12

asch


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.

like image 40
LucaMus Avatar answered Dec 01 '22 08:12

LucaMus