Bharath
Bharath

Reputation: 11

How to log info logs to one file & error logs to another file

I want to print Info & Debug logs to success.log file & Error logs to error.log file in log4j by Java programmatic level(In config class). I tried many but couldn't get. Can anyone please help.

Upvotes: 0

Views: 2816

Answers (1)

Abhijeet
Abhijeet

Reputation: 4309

To print error logs and info/debug logs in different files. You have to add tow different configuration your log4j/log4j2/logback file. Create different appender/logger for logging different levels of logs.

e.g. for Log4j :

##############For errors######################
# Define the root logger with appender file
log4j.rootLogger = ERROR, FILE, ALERT
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=D:\\application.log
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, overwrite
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

##############For Alerts######################
log4j.appender.ALERT=org.apache.log4j.FileAppender
log4j.appender.ALERT.File=D:\\alert.log
log4j.appender.ALERT.Threshold=fatal
log4j.appender.ALERT.layout=org.apache.log4j.PatternLayout
log4j.appender.ALERT.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Above configuration is from my practice project. Update above configs according to your need. You can refer this link as well : Spring boot multiple log files

Upvotes: 1

Related Questions