How to append multiple appender references in Log4j2?

拜拜、爱过 提交于 2019-12-10 15:27:18

问题


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

?


回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/39650949/how-to-append-multiple-appender-references-in-log4j2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!