Dailyrollingfileappender not creating backup of more than 1 day - log4j

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

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.

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

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);

Log4j: DailyRollingFileAppender with MaxFileSize Option

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!

Logging with log4j on tomcat jruby-rack for a Rails 3 application

I just spent the better part of 3 hours trying to get my Rails application logging with Log4j. I've finally got it working, but I'm not sure if what I did is correct. I tried various methods to no avail until my very last attempt. So I'm really looking for some validation here, perhaps some pointers and tips as well -- anything would be appreciated to be honest. I've summarized all my feeble methods into three attempts below. I'm hoping for some enlightenment on where I went wrong with each attempt -- even if it means I get ripped up.
Thanks for the help in advance!
System Specs
Rails 3.0
Windows Server 2008
Log4j 1.2
Tomact 6.0.29
Java 6
Attempt 1 - Configured Tomcat to Use Log4J
I basically followed the guide on the Apache Tomcat website here. The steps are:
Create a log4j.properties file in $CATALINA_HOME/lib
Download and copy the log4j-x.y.z.jar into $CATALINA_HOME/lib
Replace $CATALINA_HOME/bin/tomcat-juli.jar with the tomcat-juli.jar from the Apache Tomcat Extras folder
Copy tomcat-juli-adapters.jar from the Apache Tomcat Extras folder into $CATALINA_HOME/lib
Delete $CATALINA_BASE/conf/logging.properties
Start Tomcat (as a service)
Expected Results According to the Guide
I should have seen a tomcat.log file in my $CATALINA_BASE/logs folder.
Actual Results
No tomcat.log
Saw three of the standard logs instead
jakarta_service_20101231.log
stderr_20101231.log
stdout_20101231.log
Question
Shouldn't I have at least seen a tomcat.log file?
Attempt 2 - Use default Tomcat logging (commons-logging)
Reverted all the changes from the previous setup
Modified $CATALINA_BASE/conf/logging.properties by doing the following:
Adding a setting for my application in the handlers line: 5rails3.org.apache.juli.FileHandler
Adding Handler specific properties
5rails3.org.apache.juli.FileHandler.level = FINE
5rails3.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
5rails3.org.apache.juli.FileHandler.prefix = rails3.
Adding Facility specific properties
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/rails3].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/rails3].handlers = 4host-manager.org.apache.juli.FileHandler
Modified my web.xml by adding the following context parameter as per the Logging section of the jruby-rack README (I also modified my warbler.rb accordingly, but I opted to change the web.xml directly to test things faster).
<context-param>
<param-name>jruby.rack.logging</param-name>
<param-value>commons_logging</param-value>
</context-param>
Restarted Tomcat
Results
A log file was created (rails3.log), however there was no log information in the file.
Attempt 2A - Use Log4j with existing set up
I decided to give Log4j another whirl with this new web.xml setting.
Copied the log4j.jar into my WEB-INF/lib folder
Created a log4j.properties file and put it into WEB-INF/classes
log4j.rootLogger=INFO, R
log4j.logger.javax.servlet=DEBUG
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${catalina.base}/logs/rails3.log
log4j.appender.R.MaxFileSize=5036KB
log4j.appender.R.MaxBackupIndex=4
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n
Restarted Tomcat
Results
Same as Attempt 2
NOTE: I used log4j.logger.javax.servlet=DEBUG because I read in the jruby-rack README that all logging output is automatically redirected to the javax.servlet.ServletContext#log method. So I thought this would capture it, but I was obviously wrong.
Question
Why didn't this work?
Isn't Log4J using the commons_logging API?
Attempt 3 - Tried out slf4j (WORKED)
A bit uncertain as to why Attempt 2A didn't work, I thought to myself, maybe I can't use commons_logging for the jruby.rack.logging parameter because it's probably not using commons_logging API... (but I was still not sure). I saw slf4j as an option. I have never heard of it and out of curiosity, I decided to look it up. After reading about it briefly, I thought it was as good of a shot as any and decided to try it out following the instructions here.
Continuing from the setup of Attempt 2A:
Copied slf4j-api-1.6.1.jar and slf4j-simple-1.6.1.jar into my WEB-INF/lib folder
I also copied slf4j-log4j12-1.6.1.jar into my WEB-INF/lib folder
Restarted Tomcat
And VIOLA! I now have logging information going into my rails3.log file.
So the big question is:
WTF?
Even though logging seems to be working now, I'm really not sure if what I did is right. So like I said earlier, I'm really looking for some validation more or less. I'd also appreciate any pointers/tips/advice if you have any. Thanks!
We also did various experiments and finally settled with slf4j option. Coming from Java background we knew slf4j, so we didn't go further.
<context-param>
<param-name>jruby.rack.logging</param-name>
<param-value>slf4j</param-value>
</context-param>
btw, there is no need to copy slf4j-simple-1.6.1.jar into tomcat/lib or WEB-INF/lib when using slf4j-log4jxxx.jar
It's possible that your early tests with commons-logging were showing zero bytes files because of bufferSize.
If your entries get flushed to the file when you stop Tomcat then this is indeed the case and you are falling foul of the default 8Kb buffer.
To disable, for immediate flushing try...
org.apache.juli.FileHandler.bufferSize = -1
on the specific handler...
So for localhost...
2localhost.org.apache.juli.FileHandler.bufferSize = -1

Resources