Unable to generate log with DailyRollingAppendar - java

DailyRollingAppendar for log for each minute has been configured.But only app.log getting generated but no further app.log generation with time after a minute.Tried with both log4j1.2.16 & 1.2.17, PLease help me where what else to be done
log4j.rootLogger=DEBUG, RollingAppender, stdout
# LOGFILE is set to be a DailyRollingFileAppender appender
log4j.appender.RollingAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingAppender.File=/Users/mine/Documents/Softwares/Tomcat/apache-tomcat-8.0.26/logs/app.log
log4j.appender.RollingAppender.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.RollingAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingAppender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %C::%M()::%L - %m%n

Related

Log4J 2 configuration: How to split log files by size and by day at the same time?

I need to create a new log file once a day, or if log file size become 250 MB. So how can i do this through log4j.properties file? Dividing by day is working, but if size become more than 250MB nothing happens.
My current property file is:
# Root logger option
log4j.rootLogger=INFO,console, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.encoding=UTF-8
log4j.appender.file.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.RollingPolicy.FileNamePattern=./logs/application_%d{dd-MM-yyyy}.log
log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.TriggeringPolicy.maxFileSize=250000
log4j.appender.rollingFile.MaxFileSize=10MB
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
# configuration to print on 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
As per the following log4j link https://logging.apache.org/log4php/docs/appenders/rolling-file.html, you have to add the following lines.
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.TriggeringPolicy.maxFileSize=250000
Note:
File Appender should be RollingFileAppender
Triggering Policy should be SizeBasedTriggeringPolicy
EDIT:
# Direct log messages to a log filelog4j.appender.file.encoding=UTF-8
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.File=${LOG_DIR}/application.log
log4j.appender.file.RollingPolicy.FileNamePattern=./logs/application_%d{dd-MM-yyyy}.log
log4j.appender.rfile.layout=org.apache.log4j.PatternLayout
log4j.appender.rfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file.MaxFileSize=250MB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.append=true
You can refer complete example below.
https://howtodoinjava.com/log4j/log4j-rolling-file-appender/

log4j, need to display only INFO and ERROR message in log file

I am new to log4j and worked to setup on eclipse and its running. I understood the chain of priority in the levels and this is my properties file config:
log4j.rootLogger=ALL, file, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
logrj.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %c{1} - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/justfortesting.log
log4j.appender.file.Append=true
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.MaxFileSize=1KB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
My question is
Is it possible to print only INFO and ERROR type of messages in log files with properties file configuration
Finally I got the solution for my requirement. Here I used concept of filters i.e LevelRangeFilter. And for every logging level we defined appender.
log4j.rootLogger=DEBUG, file, console, file1
log4j.appender.console=org.apache.log4j.ConsoleAppender
logrj.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %c{1} - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/justfortesting.log
log4j.appender.file.Append=true
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.filter.a=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.file.filter.a.LevelMin=INFO
log4j.appender.file.filter.a.LevelMax=INFO
log4j.appender.file.MaxFileSize=1KB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
log4j.appender.file1=org.apache.log4j.RollingFileAppender
log4j.appender.file1.File=logs/justfortesting.log
log4j.appender.file1.Append=true
log4j.appender.file1.ImmediateFlush=true
log4j.appender.file1.Threshold=DEBUG
log4j.appender.file1.filter.g=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.file1.filter.g.LevelMin=ERROR
log4j.appender.file1.filter.g.LevelMax=ERROR
log4j.appender.file1.MaxFileSize=1KB
log4j.appender.file1.MaxBackupIndex=2
log4j.appender.file1.layout=org.apache.log4j.PatternLayout
log4j.appender.file1.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
log4j.logger.com.log4j=DEBUG, file, console, file1
log4j.additivity.com.log4j=false
If any one suggest to reduce the above properties configuration for same requirement,, please comment.
Try
log4j.appender.file.Threshold=INFO
Also see related SO question and documentation

log4j:WARN No such property [maxFileSize] in org.apache.log4j.DailyRollingFileAppender

Everything works just as fine. But showing this error.
My log4j.properties file like :
# Root logger option
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.DailyRollingFileAppender
log4j.appender.myAppender.DatePattern='.'yyyy-MM-dd
log4j.appender.file.File=D:\\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
Thanks in advance. Just tell me how to do it. I just want log file on daily rolling .
DailyRollingFileAppender doesn't support MaxFileSize, RollingFileAppender does.
DailyRollingFileAppender is for rolling files based on the date and time of the log entry, so if you want to use it you should remove the MaxFileSize property.
I changed the code to
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
from
log4j.appender.FILE=org.apache.log4j.FileAppender
and it worked fine in log4j.properties file

Tomcat Save Log History

I am using log4j in webapp deployed on tomcat, but cuurently my logs comes under catalina.log file, but only error logs remain in history, i want my all logs to be remain in history , additionally it will be good if if i get logs datewise.
my logs4j looks like this
log4j.rootLogger=INFO, myConsoleAppender
log4j.appender.myConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.myConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myConsoleAppender.layout.ConversionPattern= [%t] %d{dd MMM yyyy HH:mm:ss,SSS} : %c %x : %m%n
i am only using info level of logs.
Try this
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%-5p : %d{yyyy-MM-dd HH:mm:ss} %c{1}:%L - %m%n
# Define the daily rolling file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=/logs/app.log
log4j.appender.FILE.DatePattern='.'yyyy-MMM-dd
# Define the layout for the file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern= %-5p : %d{yyyy-MM-dd HH:mm:ss} %c{1}:%L - %m%n
Above will create a log file on the tomcat installation directory under the folder logson daily basis. You can change the log file path by modifying the log4j.appender.FILE.File=/path/to/logfile

Log4j SMTP Appender

Hi I am a novice and the application I am currently coding for uses struts 1.2 and java. We currently use Log4j for log files but I need to implement SMTP Appender so that our errors are emailed to us.
I've tried everything to get errors to be emailed with no luck. Below is our log4j.properties file.
Any suggestions?
Thanks!
log4j.rootLogger= INFO, stdout, logfile, mail
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
# Keep three backup files
log4j.appender.logfile.MaxBackupIndex=3
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=C:/LOGS/WIRE.log
log4j.appender.logfile.MaxFileSize=2048KB
#email appender
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.BufferSize=1
log4j.appender.mail.SMTPHost=smtp.serverhere.com
log4j.appender.mail.From=johndoe#serverhere.com
log4j.appender.mail.To=johndoe#serverhere.com
log4j.appender.mail.Subject=Application Error
log4j.appender.mail.threshold=error
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=%d %p [%c] - <%m>%n
# Hibernates use of the org.apache classes spews out stuff like mad.
log4j.logger.org.apache=INFO
# Springframework is very talkative too.
log4j.logger.org.springframework=INFO
# acegisecurity
#log4j.logger.org.acegisecurity = INFO
# Quartz trigger checking
log4j.logger.org.quartz.impl.jdbcjobstore=INFO
#log4j.logger.org.springframework.scheduling.quartz=INFO
By default, the appender only sends an email when something is logged at the ERROR or FATAL levels.
As a side note, it appears that your threshold property may have improper case. I believe:
log4j.appender.mail.threshold=error
should be
log4j.appender.mail.Threshold=error
Edit...
Log4j can be put into debug mode by configuring the log4j.debug configuration property. This might provide some output regarding the SMTP appender.
Define the root logger in error level and override this for selected packages. The mail appender will remain in the error level.
# Log appenders
#log4j.rootLogger=INFO, CONSOLE
log4j.rootLogger=ERROR, FILE, MAIL
# Log levels
log4j.logger.com.example.application=INFO
log4j.logger.com.example.application.package=DEBUG
# CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5r %-5p [%t] %c{2} - %m%n
log4j.appender.CONSOLE.Encoding=UTF-8
# FILE
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.File=/tmp/application.log
log4j.appender.FILE.MaxFileSize=1000KB
log4j.appender.FILE.MaxBackupIndex=99
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5r %-5p [%t] %c{2} - %m%n
log4j.appender.FILE.Encoding=UTF-8
# MAIL
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.BufferSize=1
log4j.appender.MAIL.SMTPHost=smtp.example.com
log4j.appender.MAIL.From=application#example.com
log4j.appender.MAIL.To=developer#example.com
log4j.appender.MAIL.Subject=Exception in Application
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5r %-5p [%t] %c{2} - %m%n

Categories