Disable log4j console logging and enable file logging - java

My log4j.properties file is:
log4j.rootLogger=WARN, stdout, file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %p [%c] - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${jboss.server.log.dir}/afrodite.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.Append=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%t] %p [%c] - %m%n
log4j.logger.info.release.scheduler=INFO, scheduler
log4j.appender.scheduler=org.apache.log4j.RollingFileAppender
log4j.appender.scheduler.File=${jboss.server.log.dir}/afrodite_scheduler.log
log4j.appender.scheduler.MaxFileSize=10MB
log4j.appender.scheduler.MaxBackupIndex=10
log4j.appender.scheduler.Append=true
log4j.appender.scheduler.layout=org.apache.log4j.PatternLayout
log4j.appender.scheduler.layout.ConversionPattern=%d [%t] %p [%c] - %m%n
If I want to disable console output for scheduler logger what should I do? That means scheduler logger only log into file not in console.
Thanks and Regards.

Add this line to your config:
log4j.appender.scheduler.additivity=false
log4j.additivity.info.release.scheduler=false
(sorry, first hint was incorrect.)

you can add two lines in your properties file.
log4j.rootLogger=ALL, stdout, file,scheduler (#here ALL declare for all level of logs)
log4j.logger.info.release.scheduler=INFO, scheduler
log4j.additivity.info.release.scheduler=false

Related

correct setting for log4j to redirect logs(warning,info,error) to file

i am using log4j for logging my log4j.properties file is:
# Root logger option
log4j.rootLogger=INFO, stdout, file
# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#outputs to Tomcat home
log4j.appender.file.File= D:/log_file.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
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
its not working for warning Please help thanks
Please see this:
log4j.rootLogger=INFO, file
log4j.rootLogger=DEBUG, file
log4j.rootLogger=WARNING, file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=<Location_of_Log_file>
log4j.appender.file.MaxFileSize=20MB
log4j.appender.file.MaxBackupIndex=10
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

Log4j's RollingFileAppender doesn't create a file

I have a Spring Boot application which uses Log4j. I've set up RollingFileAppender and ConsoleAppender. The second one works fine, but RollingFile Appender doesn't create a file.
Here's a file with properties:
log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=log4j-application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
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
Any ideas will be very appreciated.
Use this configurarion
log4j.rootLogger=DEBUG, consoleAppender, fileAppender
log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
log4j.appender.fileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
log4j.appender.fileAppender.File=demoApplication.log
For more info visit this link http://howtodoinjava.com/2013/04/08/how-to-configure-log4j-using-properties-file/
Or maybe you can try the following for daily-logging
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.File=logfile.log
log4j.appender.logfile.Threshold = INFO
log4j.appender.logfile.DatePattern='_'yyyyMMdd'.log'
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

log4j not zipping log files

I am not able to understand what I am missing here. Below is my log4j.properties
# Root logger option
log4j.rootLogger=INFO, file
# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.rollingPolicy.ActiveFileName=${catalina.home}/logs/broker.log
log4j.appender.file.rollingPolicy.FileNamePattern=${catalina.home}/logs/broker-.%d{yyyyMMdd-HH}.log.gz
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
you forget to set MaxFileSize,just add the following to your RollingFileAppender configuration and having the filename end in .gz will automagically compress your logfiles:
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.rollingPolicy.ActiveFileName=${catalina.home}/logs/broker.log
log4j.appender.file.rollingPolicy.FileNamePattern=${catalina.home}/logs/broker-.%d{yyyyMMdd-HH}.log.gz
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.MaxFileSize=10M
log4j.appender.file.MaxBackupIndex=3
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
hope that helped

Log4j not send logs to linux SYSLOG

I want implement log4j to my java project. I put the jar in lib folder and configure log4j.properties file. Logs successfuly shows in console, but I can't see that logs in SYSLOG app (KSYSLOG) of my kubuntu. this is my properties file:
log4j.rootLogger=INFO, stdout, SYSLOG, file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a SYSLOG
log4j.appender.SYSLOG=org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG.syslogHost=127.0.0.1
log4j.appender.SYSLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.conversionPattern=%d{ISO8601} %-5p [%t] %c{2} %x - %m%n
log4j.appender.SYSLOG.Facility=LOCAL1
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/var/log/ log4j-application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
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
what is incorrect in this properties file or what I do incorrect?
I found the solutin in this thread Writing log data to syslog using log4j
we must add
$ModLoad imudp
$UDPServerRun 514
to rsyslog.conf and restart rsyslog

Log messages except from a particular package to a file

I am using log4j for logging , my requirement is to log all the logs from package
pkg1 (except pkg1.pkg2) to pkg1.log
and pkg1.pkg2 (a sub package of the above package) to pkg2.log
is this possible ? I know i can achieve it through custom appenders but is there a way I can do it through configurations.
I am using the below properties file. Here the problem is PKG1.log contains logs from PKG2 also.
# Root logger option
log4j.rootLogger=WARN, CONSOLE
# Direct log messages to a log file
log4j.logger.com.pkg1=DEBUG, PKG1 log4j.appender.PKG1=org.apache.log4j.RollingFileAppender
log4j.appender.PKG1.File=logs/PKG1.log
log4j.appender.PKG1.MaxFileSize=1MB
log4j.appender.PKG1.MaxBackupIndex=1
log4j.appender.PKG1.layout=org.apache.log4j.PatternLayout
log4j.appender.PKG1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.logger.com.pkg1.pkg2=DEBUG, PKG2
log4j.appender.PKG2=org.apache.log4j.RollingFileAppender
log4j.appender.PKG2.File=logs/PKG2.log
log4j.appender.PKG2.MaxFileSize=1MB
log4j.appender.PKG2.MaxBackupIndex=1
log4j.appender.PKG2.layout=org.apache.log4j.PatternLayout
log4j.appender.PKG2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Direct log messages to CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
You can stop PKG2's logs from reaching it's ancestor's appenders by disabling the additivity flag in the configuration:
log4j.appender.PKG2.additivity=false

Categories

Resources