Context:
I want to use log4j to write audit-related logs to a specific log file, let's say audit.log. I don't want to use syslogappender(udp based) because I don't want to be tolerant to data loss. Plus, I am using logrotate to rotate the audit.log when the file gets to certain size.
Problem:
I am encountering is that, when logrotate rotates the file audit.log to audit.log.1, log4j keeps writing to audit.log.1 other than writing to the audit.log.
Possible approaches:
I know I can use rollingfileappender to do the log rotation other than use logrotate, so when rollingfileappender rolls the file, it switch to the new file without hassles. But the reason I can't use rollingfileappender is that I want to use logrotate's post rotate feature to trigger some scripts after the rotation happens which can't be provided by rollingfileappender.
Another desperate way I can think of is to write a log4j customized appender myself to close the log file(audit.log.1) and open the new one(audit.log) when it detects the file is rotated.
I never used ExternallyRolledFileAppender, but if it's possible to use logrotate post rotate to send the signal to ExternallyRolledFileAppender and make log4j aware the file is rotated, and start writing to the new file?
Question:
Just wondering is there some appender like that already been invented/written? or do I have other options to solve this?
Roll over on the first day of each week depending upon the locale. Following is a sample configuration file log4j.properties to generate log files rolling over at midday and midnight of each day.
You have AppenderRef="rolling", but your Appender is named "rollingfile". These need to match. Also, log4j probably issues a warning that there is a problem with the configuration. I recommend you specify <Configuration status="WARN" > at the start of your config instead of FATAL so that you can see these warnings.
Log4j provides org.apache.log4j.FileAppender class to write your logging information into a file. FileAppender has the following configurable parameters: The default value of this flag is true, which means the output stream to the file being flushed with each append operation. We can use any character - encoding.
Well, the appender works fine. I want to use appender so that it will clear the log file daily, but when it first created the log file, it doesn't write any log message into the file created by , so I came here to get some advise
Check out logrotate
's copytruncate
option, it might help your case:
copytruncate
Truncate the original log file to zero size in place
after creating a copy, instead of moving the old log
file and optionally creating a new one. It can be
used when some program cannot be told to close its
logfile and thus might continue writing (appending) to
the previous log file forever. Note that there is a
very small time slice between copying the file and
truncating it, so some logging data might be lost.
When this option is used, the create option will have
no effect, as the old log file stays in place
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With