Commit 5e665c38 authored by God Ly's avatar God Ly Committed by it2000

OF-301 createAuditFile() rewritten

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@11677 b35dd754-fafc-0310-a699-88a17e54d16e
parent b4356fe3
...@@ -34,7 +34,6 @@ import java.util.Collections; ...@@ -34,7 +34,6 @@ import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.StringTokenizer;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
...@@ -103,6 +102,14 @@ public class AuditorImpl implements Auditor { ...@@ -103,6 +102,14 @@ public class AuditorImpl implements Auditor {
*/ */
private BlockingQueue<AuditPacket> logQueue = new LinkedBlockingQueue<AuditPacket>(); private BlockingQueue<AuditPacket> logQueue = new LinkedBlockingQueue<AuditPacket>();
/**
* Allow only a limited number of files for each day, max. three digits (000-999)
*/
private final int maxTotalFilesDay = 1000;
/**
* Track the current index number `...-nnn.log´
*/
private int filesIndex = 0;
/** /**
* Timer to save queued logs to the XML file. * Timer to save queued logs to the XML file.
*/ */
...@@ -285,71 +292,67 @@ public class AuditorImpl implements Auditor { ...@@ -285,71 +292,67 @@ public class AuditorImpl implements Auditor {
} }
} }
private void createAuditFile(Date auditDate) throws IOException { /* if this new logic still causes problems one may want to
close(); * use log4j or change the file format from YYYYmmdd-nnn to YYYYmmdd-HHMM */
if (currentDateLimit == null || auditDate.after(currentDateLimit)) { /**
// Set limit date after which we need to rollover the audit file (based on the date) * Sets <b>xmlWriter</b> so this class can use it to write audit logs<br>
Calendar calendar = Calendar.getInstance(); * The audit filename <b>currentAuditFile</b> will be `jive.audit-YYYYmmdd-nnn.log´<br>
calendar.setTime(auditDate); * `nnn´ will be reset to `000´ when a new log file is created the next day <br>
calendar.set(Calendar.HOUR, 23); * `nnn´ will be increased for log files which belong to the same day<br>
calendar.set(Calendar.MINUTE, 59); * <b>WARNING:</b> If log files of the current day are deleted and the server is restarted then
calendar.set(Calendar.SECOND, 59); * the value of `nnn´ may be random (it's calculated by `Math.max(files.length, filesIndex);´
calendar.set(Calendar.MILLISECOND, 999); * with `filesIndex=0´ and `files.length=nr(existing jive.audit-YYYYmmdd-???.log files)´ -
currentDateLimit = calendar.getTime(); * if there are 10 audit files (033-043) then nnn will be 10 instead of 44).<br>
} * If `nnn=999´ then all audit data will be written to this file till the next day.<br>
* @param auditDate
final String filePrefix = "jive.audit-" + dateFormat.format(auditDate) + "-"; * @throws IOException
// Get list of existing audit files */
FilenameFilter filter = new FilenameFilter() { private void createAuditFile(Date auditDate) throws IOException {
public boolean accept(File dir, String name) { final String filePrefix = "jive.audit-" + dateFormat.format(auditDate) + "-";
return name.startsWith(filePrefix) && name.endsWith(".log"); if (currentDateLimit == null || auditDate.after(currentDateLimit)) {
} // Set limit date after which we need to rollover the audit file (based on the date)
}; Calendar calendar = Calendar.getInstance();
File[] files = baseFolder.listFiles(filter); calendar.setTime(auditDate);
if (files.length == 0) { calendar.set(Calendar.HOUR, 23);
// This is the first audit file for the day calendar.set(Calendar.MINUTE, 59);
currentAuditFile = new File(logDir, filePrefix + "000.log"); calendar.set(Calendar.SECOND, 59);
} calendar.set(Calendar.MILLISECOND, 999);
else { currentDateLimit = calendar.getTime();
// Search the last index used for the day filesIndex = 0;
File lastFile = files[files.length - 1]; }
StringTokenizer tokenizer = new StringTokenizer(lastFile.getName(), "-."); // Get list of existing audit files
// Skip "jive" FilenameFilter filter = new FilenameFilter() {
tokenizer.nextToken(); public boolean accept(File dir, String name) {
// Skip "audit" return name.startsWith(filePrefix) && name.endsWith(".log");
tokenizer.nextToken(); }
// Skip "date" };
tokenizer.nextToken(); File[] files = baseFolder.listFiles(filter);
int index = Integer.parseInt(tokenizer.nextToken()) + 1; // if some daily files were already deleted then files.length will be smaller than filesIndex
if (index > 999) { // see also WARNING above
Log.warn("Failed to created audit file. Max limit of 999 files has been reached " + filesIndex = Math.max(files.length, filesIndex);
"for the date: " + dateFormat.format(auditDate)); if (filesIndex >= maxTotalFilesDay)
return; {
} // don't close this file, continue auditing to it
currentAuditFile = new File(logDir, return;
filePrefix + StringUtils.zeroPadString(Integer.toString(index), 3) + ".log"); }
} File tmpAuditFile = new File(logDir, filePrefix + StringUtils.zeroPadString(Integer.toString(filesIndex), 3) + ".log");
if ( (filesIndex == maxTotalFilesDay-1) && !tmpAuditFile.exists() )
{
// Find the next available log file name Log.warn("Creating last audit file for this date: " + dateFormat.format(auditDate));
/*for (int i = 0; i < 1000; i++) { }
currentAuditFile = new File(logDir, while ( (filesIndex<(maxTotalFilesDay-1)) && (tmpAuditFile.exists()) )
filePrefix + StringUtils.zeroPadString(Integer.toString(i), 3) + ".log"); {
if (!currentAuditFile.exists()) { Log.debug("Audit file '"+ tmpAuditFile.getName() +"' does already exist.");
break; filesIndex++;
} tmpAuditFile = new File(logDir, filePrefix + StringUtils.zeroPadString(Integer.toString(filesIndex), 3) + ".log");
} }
currentAuditFile = tmpAuditFile;
if (currentAuditFile == null) { close();
Log.warn("Audit log not create since there are more than 999 files for the date: " + // always append to an existing file (after restart)
dateFormat.format(auditDate)); writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(currentAuditFile, true), "UTF-8"));
return; writer.write("<jive xmlns=\"http://www.jivesoftware.org\">");
}*/ xmlWriter = new org.jivesoftware.util.XMLWriter(writer);
}
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(currentAuditFile), "UTF-8"));
writer.write("<jive xmlns=\"http://www.jivesoftware.org\">");
xmlWriter = new org.jivesoftware.util.XMLWriter(writer);
}
/** /**
* Saves the queued entries to an XML file and checks that very old files are deleted. * Saves the queued entries to an XML file and checks that very old files are deleted.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment