简体   繁体   中英

How to handle more than two appenders logging separately

We are moving from Log4j1 to log4j2. I am able to create multiple files and added logging in those files like below:

name=PropertiesConfig
appenders = file1, file2

appender.file1.type = File
appender.file1.name = LOG1FILE1
appender.file1.fileName= ./logs/operation.log
appender.file1.layout.type=PatternLayout
appender.file1.layout.pattern= %-d{yyyy MMM dd HH:mm:ss:SSS} GMT %-d{Z} %-5p[%t] %m%n

appender.file2.type = File
appender.file2.name = LOGFILE
appender.file2.fileName= ./logs/Connection.log
appender.file2.layout.type=PatternLayout
appender.file2.layout.pattern= %-d{yyyy MMM dd HH:mm:ss:SSS} GMT %-d{Z} %-5p[%t] %m%n
rootLogger.level = info
rootLogger.additivity = false
rootLogger.appenderRefs = logfile
rootLogger.appenderRef.logfile.ref = LOGFILE

rootLogger.appenderRefs = LOG1FILE1
rootLogger.appenderRef.LOG1FILE1.ref = LOG1FILE1

I need to understand, how I can do logging in one particular file for a particular type of appender. I was able to do this in log4j1 earlier. Let's consider I have two appenders one is for Connection and another one is for Operation, so when instantiating the connection, write logs in Connection.log file whereas when operation is performing the logging happens in operation.log file. So same thing I wanted to handle in log4j2.

In Log4j 1.x you attached each appender to a different logger. In Log4j 2.x you just need to do the same:

logger.1.name = BusinessFunction
logger.1.level = TRACE
logger.1.appenderRef.1.ref = LOG1FILE1

logger.2.name = Connection
logger.2.level = TRACE
logger.2.appenderRef.1.ref = LOGFILE

You should also consider attaching the root logger to a file.

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