Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j fileappender doesn't switch to the new file when logrotate rotates the log file

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:

  1. 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.

  2. 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.

  3. 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?

like image 640
Shengjie Avatar asked May 24 '12 12:05

Shengjie


People also ask

What time does Log4j roll over?

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.

Why is Log4j not working with my Appender?

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.

How to write logging information to a file in Log4j?

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.

Does Appender work with log files?

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


1 Answers

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
like image 104
holygeek Avatar answered Oct 22 '22 11:10

holygeek