Log4j: DailyRollingFileAppender with MaxFileSize Option - log4j

I am using this log4j.properties
log4j.rootCategory=Info, A1
# A1 is a DailyRollingFileAppender
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.file=D:/MyWeb.log
log4j.appender.A1.datePattern='.'yyyy-MM-dd
log4j.appender.A1.append=true
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-22d{dd/MMM/yyyy HH:mm:ss} - %m%n
I want to display logs in the Date Wise Order , so I am using DailyRollingFileAppender.
But the issue is that this log file currently cannot hold much data (meaning when lot of requests are made on that day) it looses the previous log data
I tried to use the option MaxFileSize:
log4j.appender.A1.MaxFileSize=10MB
But on to the server console its giving error that property MaxFileSize isn't supported .
Please tell me if there is any other way that the log appears date wise and it can hold as much data as specified.

You could extend the FileAppender class and implement your custom version. More details
DailyRollingFileAppender

You could use DailyRollingFileAppender with the hourly backup option. This will rollover the logs every hour.
Hourly Usage:
log4j.appender.A1.datePattern='.'yyyy-MM-dd-HH

Use the RollingFileAppender, you are using the wrong Appender!

Related

Cannot change zookeeper log filename

Zookeeper is creating the logs with a name zookeeper-root-hostname.out but this is my log4j.properties:
zookeeper.root.logger=INFO, CONSOLE
zookeeper.console.threshold=INFO
zookeeper.log.dir=.
zookeeper.log.file=zookeeper.log
zookeeper.log.threshold=INFO
zookeeper.log.maxfilesize=256MB
zookeeper.log.maxbackupindex=20
zookeeper.tracelog.dir=${zookeeper.log.dir}
zookeeper.tracelog.file=zookeeper_trace.log
log4j.rootLogger=${zookeeper.root.logger}
#
# console
# Add "console" to rootlogger above if you want to use this
#
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=${zookeeper.console.threshold}
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}#%L] - %m%n
#
# Add ROLLINGFILE to rootLogger to get log file output
#
log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLINGFILE.Threshold=${zookeeper.log.threshold}
log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file}
log4j.appender.ROLLINGFILE.MaxFileSize=${zookeeper.log.maxfilesize}
log4j.appender.ROLLINGFILE.MaxBackupIndex=${zookeeper.log.maxbackupindex}
log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}#%L] - %m%n
If a change the property zookeeper.log.file=zookeeper.log the file is created with the same name, How can I change the filename of the log?
Update
I found that the file zkServer.sh set the variable ZOO_LOG_FILE and overrides the value that is define in the log4j.properties:
ZOO_LOG_FILE=zookeeper-$USER-server-$HOSTNAME.log
I can modify this file but Is it ok to change that manually?
In typical Java application fashion the logging situation with Zookeeper is incredibly complicated, custom, and poorly documented. The official Administrators manual logging section is a tiny paragraph with about zero information, but suggests you can rely on your existing knowledge of the third party library log4j configured to proxy through the other third party library SLF4J. This actually wouldn't be too bad since that complicated arrangement is common as many popular open source projects ignore the built-in logging framework that ships will Java, but later you'll discover using the log4j.properties file in the conf directory actually doesn't work as expected because custom properties are used in the provided start up script (zkServer.sh) that override many things.
In order to set the file name note the file conf/log4j.properties contains the following variable assignment that is overridden by the start up script (zkServer.sh):
zookeeper.log.file=zookeeper.log
Either set the environment variables externally, modify the start up script or update the log4j.properties to not use the variable:
#log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file}
log4j.appender.ROLLINGFILE.File=/the/actual/path/goes/here.log
You may also want to enable the ROLLINGFILE like so:
#log4j.rootLogger=${zookeeper.root.logger}
log4j.rootLogger=INFO,ROLLINGFILE
After searching I found this link but I didn't found how to change the variable for the log, I noticed that inside the file zkServer.sh is defined the name of the file, so I needed to change this variable:
_ZOO_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper-$USER-server-$HOSTNAME.out"
I don't know if it's ok to change that variable but now is working as expected.

Dailyrollingfileappender not creating backup of more than 1 day

My log config is as follows,but i can see only 1 backup file being created and all the other backup just goes off / deleted.
log4j.appender.application=org.apache.log4j.DailyRollingFileAppender
log4j.appender.application.File=${log.root.path}/test.log
log4j.appender.application.DatePattern='.'yyyy-MM-dd
log4j.appender.application.layout=org.apache.log4j.PatternLayout
log4j.appender.application.layout.ConversionPattern=%d{dd MMM
HH\:mm\:ss\:SSS} [%t] %-5p [%c\:%M\:%L] srcIp=%X{srcIp} -
remoteHost=%X{remoteHost} -port=%X{port} - activityType=%X{activitytype} -
activityStatus=%X{activitystatus} -applicationUser=%X{applicationUser} -
APITXNID=%X{apiTxnId} -CustomerRefNum=%X{CustomerRefNum} -%m%n%n
Your configuration looks fine, there are several thing you could try to do:
Try replace log4j.appender.application.DatePattern from '.'yyyy-MM-dd to '.'yyyy-MM-dd-HH-mm and see if files would be created every minute. If it works, chances are that some external cleanup script deletes old logs.
Add -Dlog4jdebug.true to VM options to verify what is effective log4j conf file is used.
Also consider using API Documentation warning
DailyRollingFileAppender has been observed to exhibit synchronization issues and data loss. The log4j extras companion includes alternatives which should be considered for new deployments and which are discussed in the documentation for org.apache.log4j.rolling.RollingFileAppender.
You can have the same effect as your config above, by replacing log4j dependency with apache-log4j-extras, which implements log4j 1.2.x, but has extra features, such as org.apache.log4j.rolling.RollingFileAppender with TimeBasedRollingPolicy
Then replace in your log4j.properties
log4j.appender.application=org.apache.log4j.DailyRollingFileAppender
log4j.appender.application.File=${log.root.path}/test.log
log4j.appender.application.DatePattern='.'yyyy-MM-dd
with
log4j.appender.application=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.application.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.application.rollingPolicy.FileNamePattern=${log.root.path}/test.log.%d{yyyy-MM-dd-HH-mm}
log4j.appender.application.File=${log.root.path}/test.log

Log4j 1.2.17 - How to do log rolling based on size

I would like my logfile to roll when it reaches to a given size (lets say 100MB) and rolled file should be archived(.gz) Also, the zip file should have current time stamp in its name.
Is it possible to achieve this using log4j 1.2?
You can define the parameter maxFileSize and maxBackupIndex for a RollingFileAppender
For compression check this answer : https://stackoverflow.com/a/3329513/1811730
This link can help : Rotate & Archiving File On Size
If you can, don't use log4j 1.2 but see for log4j 2

Log4j Properties File for Monthly Backup

What is the required configuration in log4j.properties file in order to backup log file in the end of each month (monthly rolling) and continue to have the same name for active log file?
So I found that DatePattern does the trick:
log4j.appender.applog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.applog.encoding=UTF-8
log4j.appender.applog.layout=org.apache.log4j.PatternLayout
log4j.appender.applog.layout.ConversionPattern=%d{yyyyMMdd HH:mm:ss} %m%n
log4j.appender.applog.File=log/process.log
log4j.appender.applog.DatePattern='.'yyyy-MM
Here, name of the active log file is always "process.log" and at the beginning of each month the file is saved with the name "process.log.yyyy-MM" when the first log of that month is pushed to log4j and the active log file is reset.
You can choose the DailyRollingFileAppender and the date pattern configuration. Refer to http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html
and
http://www.tutorialspoint.com/log4j/log4j_logging_files.htm

How can i configure Log4j to generate new log file every time the java application runs

How can I configure Log4j to generate new log file every time the java application runs?
I want the log name to vary every time like [Log file name]_[CurrentTime].log
I am using .property file to configure log4j
i am using RollingFileAppender but this did not worked for me.here is my configuration
log4j.appender.dest4=org.apache.log4j.RollingFileAppender
log4j.appender.dest4.File=.\\\\log4j\\\\user.log
log4j.appender.dest4.layout=org.apache.log4j.PatternLayout
log4j.appender.dest4.layout.ConversionPattern=[%d{dd/MMM/yyyy HH:mm:ss}] : %-5p: %m%n
log4j.appender.dest4.DatePattern='.'yyyy-MM
You might want to take a look at the RollingFileAppender
HereĀ“s an interesting post about the subject which applies to log4j as well: How do I configure a RollingFileAppender to roll by date and size with log4net?
Hope it helps.
You can set FileAppender dynamically
SimpleLayout layout = new SimpleLayout();
FileAppender appender = new FileAppender(layout,"logname."+new Date().toLocaleString(),false);
logger.addAppender(appender);

Resources