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.
Asked
Active
Viewed 2,511 times
0
-
Update your log4j configuration to log info and error logs in different files. – Abhijeet Jun 04 '18 at 09:09
1 Answers
1
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

Abhijeet
- 4,069
- 1
- 22
- 38
-
Hi, my requirement was different. In your scenario application.log contains logs from debug to fatal but i need only info & debug. So, I have used LevelRangeFilter by setting the values of minLevel=debug & maxLevel=info. This solved my problem. Thank you. – Bharath Jun 11 '18 at 09:03
-
@Bharath above confiugartion was just an example for your reference from my demo project. You have to modify above configuration according to your requirement – Abhijeet Jun 12 '18 at 08:32