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,8 +292,23 @@ public class AuditorImpl implements Auditor { ...@@ -285,8 +292,23 @@ public class AuditorImpl implements Auditor {
} }
} }
/* if this new logic still causes problems one may want to
* use log4j or change the file format from YYYYmmdd-nnn to YYYYmmdd-HHMM */
/**
* Sets <b>xmlWriter</b> so this class can use it to write audit logs<br>
* The audit filename <b>currentAuditFile</b> will be `jive.audit-YYYYmmdd-nnn.log´<br>
* `nnn´ will be reset to `000´ when a new log file is created the next day <br>
* `nnn´ will be increased for log files which belong to the same day<br>
* <b>WARNING:</b> If log files of the current day are deleted and the server is restarted then
* the value of `nnn´ may be random (it's calculated by `Math.max(files.length, filesIndex);´
* with `filesIndex=0´ and `files.length=nr(existing jive.audit-YYYYmmdd-???.log files)´ -
* 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
* @throws IOException
*/
private void createAuditFile(Date auditDate) throws IOException { private void createAuditFile(Date auditDate) throws IOException {
close(); final String filePrefix = "jive.audit-" + dateFormat.format(auditDate) + "-";
if (currentDateLimit == null || auditDate.after(currentDateLimit)) { if (currentDateLimit == null || auditDate.after(currentDateLimit)) {
// Set limit date after which we need to rollover the audit file (based on the date) // Set limit date after which we need to rollover the audit file (based on the date)
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
...@@ -296,9 +318,8 @@ public class AuditorImpl implements Auditor { ...@@ -296,9 +318,8 @@ public class AuditorImpl implements Auditor {
calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999); calendar.set(Calendar.MILLISECOND, 999);
currentDateLimit = calendar.getTime(); currentDateLimit = calendar.getTime();
filesIndex = 0;
} }
final String filePrefix = "jive.audit-" + dateFormat.format(auditDate) + "-";
// Get list of existing audit files // Get list of existing audit files
FilenameFilter filter = new FilenameFilter() { FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) { public boolean accept(File dir, String name) {
...@@ -306,47 +327,29 @@ public class AuditorImpl implements Auditor { ...@@ -306,47 +327,29 @@ public class AuditorImpl implements Auditor {
} }
}; };
File[] files = baseFolder.listFiles(filter); File[] files = baseFolder.listFiles(filter);
if (files.length == 0) { // if some daily files were already deleted then files.length will be smaller than filesIndex
// This is the first audit file for the day // see also WARNING above
currentAuditFile = new File(logDir, filePrefix + "000.log"); filesIndex = Math.max(files.length, filesIndex);
} if (filesIndex >= maxTotalFilesDay)
else { {
// Search the last index used for the day // don't close this file, continue auditing to it
File lastFile = files[files.length - 1];
StringTokenizer tokenizer = new StringTokenizer(lastFile.getName(), "-.");
// Skip "jive"
tokenizer.nextToken();
// Skip "audit"
tokenizer.nextToken();
// Skip "date"
tokenizer.nextToken();
int index = Integer.parseInt(tokenizer.nextToken()) + 1;
if (index > 999) {
Log.warn("Failed to created audit file. Max limit of 999 files has been reached " +
"for the date: " + dateFormat.format(auditDate));
return; return;
} }
currentAuditFile = new File(logDir, File tmpAuditFile = new File(logDir, filePrefix + StringUtils.zeroPadString(Integer.toString(filesIndex), 3) + ".log");
filePrefix + StringUtils.zeroPadString(Integer.toString(index), 3) + ".log"); if ( (filesIndex == maxTotalFilesDay-1) && !tmpAuditFile.exists() )
} {
Log.warn("Creating last audit file for this date: " + dateFormat.format(auditDate));
// Find the next available log file name
/*for (int i = 0; i < 1000; i++) {
currentAuditFile = new File(logDir,
filePrefix + StringUtils.zeroPadString(Integer.toString(i), 3) + ".log");
if (!currentAuditFile.exists()) {
break;
} }
while ( (filesIndex<(maxTotalFilesDay-1)) && (tmpAuditFile.exists()) )
{
Log.debug("Audit file '"+ tmpAuditFile.getName() +"' does already exist.");
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 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(currentAuditFile), "UTF-8"));
writer.write("<jive xmlns=\"http://www.jivesoftware.org\">"); writer.write("<jive xmlns=\"http://www.jivesoftware.org\">");
xmlWriter = new org.jivesoftware.util.XMLWriter(writer); xmlWriter = new org.jivesoftware.util.XMLWriter(writer);
} }
......
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