0

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.

Hrushi
  • 163
  • 1
  • 10
  • Can you explain how did you do it in Log4j 1.x? Log4j 2.x supports all the methods available in Log4j to classify messages and more. – Piotr P. Karwasz Feb 20 '22 at 17:39
  • log4j.logger.BusinessFunction=trace,OP log4j.logger.Connection=trace,CONN log4j.appender.OP=org.apache.log4j.DailyRollingFileAppender log4j.appender.OP.File =./logs/performance/Publication/PUB.log log4j.appender.OP.layout=org.apache.log4j.PatternLayout log4j.appender.OP.layout.ConversionPattern=%m, %d{yyyy-MM-dd HH:mm:ss,SSS}, %C, %L, %M, %t%n log4j.appender.CONN=org.apache.log4j.DailyRollingFileAppender log4j.appender.CONN.File =./logs/performance/Connection/CONN.log log4j.appender.CONN.layout.ConversionPattern=%m, %d{yyyy-MM-dd HH:mm:ss,SSS}, %C, %L, %M, %t%n – Hrushi Feb 20 '22 at 17:44

1 Answers1

0

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.

Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43
  • See I am getting all logs in both files, but I wanted to write only specific type of logs in corresponding file by reading the appender through code. We can do this in log4j1 like below- logger.getLogger("Connection"), but how we can do this in log4j2.? – Hrushi Feb 21 '22 at 08:46
  • You should remove the `appenderRef`/`appenderRefs` from the `rootLogger`. To get the logger named "Connection" just use `LogManager.getLogger("Connection")`. – Piotr P. Karwasz Feb 21 '22 at 09:02
  • If I removed the appenderRef/appenderRefs, and tried to get the appender logger as you mentioned, I am not getting logs in the file. – Hrushi Feb 21 '22 at 10:05
  • this is usefile and solved my queries https://stackoverflow.com/questions/48379863/how-to-create-multiple-log-files-in-log4j2-using-property-file – Hrushi Feb 21 '22 at 17:00