close() needs to be synchronized. Also some widening.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@2256 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2007-11-12 19:32:42 +00:00
parent 04ced65ed9
commit e9ab7a2bab
+30 -38
View File
@@ -36,10 +36,9 @@ import java.util.Date;
import com.samskivert.Log; import com.samskivert.Log;
/** /**
* Used by various services to generate audit logs which can be useful for * Used by various services to generate audit logs which can be useful for auditing, debugging and
* auditing, debugging and other logly necessities. The audit logger * other logly necessities. The audit logger automatically rolls over its logs at midnight to
* automatically rolls over its logs at midnight to facilitate the * facilitate the collection, processing and possible archiving of the logs.
* collection, processing and possible archiving of the logs.
*/ */
public class AuditLogger public class AuditLogger
{ {
@@ -65,14 +64,13 @@ public class AuditLogger
} }
/** /**
* Writes the supplied message to the log, prefixed by a date and * Writes the supplied message to the log, prefixed by a date and timestamp. A newline will be
* timestamp. A newline will be appended to the message. * appended to the message.
*/ */
public synchronized void log (String message) public synchronized void log (String message)
{ {
// construct the message // construct the message
StringBuffer buf = new StringBuffer( StringBuffer buf = new StringBuffer(message.length() + TIMESTAMP_LENGTH);
message.length() + TIMESTAMP_LENGTH);
_format.format(new Date(), buf, _fpos); _format.format(new Date(), buf, _fpos);
buf.append(message); buf.append(message);
@@ -85,27 +83,24 @@ public class AuditLogger
// log an error if we failed to write the log message // log an error if we failed to write the log message
if (!wrote) { if (!wrote) {
// be careful about logging zillions of errors if something // be careful about logging zillions of errors if something bad happens to our log file
// bad happens to our log file
if (_throttle.throttleOp()) { if (_throttle.throttleOp()) {
_throttled++; _throttled++;
} else { } else {
if (_throttled > 0) { if (_throttled > 0) {
Log.warning("Suppressed " + _throttled + Log.warning("Suppressed " + _throttled + " intervening error messages.");
" intervening error messages.");
_throttled = 0; _throttled = 0;
} }
Log.warning("Failed to write audit log message " + Log.warning("Failed to write audit log message [file=" + _logPath +
"[file=" + _logPath + ", msg=" + message + "]."); ", msg=" + message + "].");
} }
} }
} }
/** /**
* Closes this audit log (generally only done when the server is * Closes this audit log (generally only done when the server is shutting down.
* shutting down.
*/ */
public void close () public synchronized void close ()
{ {
if (_logWriter != null) { if (_logWriter != null) {
log("log_closed"); log("log_closed");
@@ -115,8 +110,8 @@ public class AuditLogger
} }
/** /**
* Opens our log file, sets up our print writer and writes a message * Opens our log file, sets up our print writer and writes a message to it indicating that it
* to it indicating that it was opened. * was opened.
*/ */
protected void openLog (boolean freakout) protected void openLog (boolean freakout)
{ {
@@ -156,8 +151,8 @@ public class AuditLogger
// rename the old file // rename the old file
String npath = _logPath.getPath() + "." + _dayStamp; String npath = _logPath.getPath() + "." + _dayStamp;
if (!_logPath.renameTo(new File(npath))) { if (!_logPath.renameTo(new File(npath))) {
Log.warning("Failed to rename audit log file " + Log.warning("Failed to rename audit log file [path=" + _logPath +
"[path=" + _logPath + ", npath=" + npath + "]."); ", npath=" + npath + "].");
} }
// open our new log file // open our new log file
@@ -185,12 +180,6 @@ public class AuditLogger
_rollover.schedule(nextCheck); _rollover.schedule(nextCheck);
} }
/** The path to our log file. */
protected File _logPath;
/** We actually write to this feller here. */
protected PrintWriter _logWriter;
/** The interval that rolls over the log file. */ /** The interval that rolls over the log file. */
protected Interval _rollover = new Interval() { protected Interval _rollover = new Interval() {
public void expired () { public void expired () {
@@ -198,6 +187,12 @@ public class AuditLogger
} }
}; };
/** The path to our log file. */
protected File _logPath;
/** We actually write to this feller here. */
protected PrintWriter _logWriter;
/** Suppress freakouts if our log file becomes hosed. */ /** Suppress freakouts if our log file becomes hosed. */
protected Throttle _throttle = new Throttle(2, 5*60*1000L); protected Throttle _throttle = new Throttle(2, 5*60*1000L);
@@ -210,19 +205,16 @@ public class AuditLogger
/** Used to format log file suffixes. */ /** Used to format log file suffixes. */
protected SimpleDateFormat _dayFormat = new SimpleDateFormat("yyyyMMdd"); protected SimpleDateFormat _dayFormat = new SimpleDateFormat("yyyyMMdd");
/** Used to format timestamps. */
protected SimpleDateFormat _format = new SimpleDateFormat(TIMESTAMP_FORMAT);
/** Annoying parameter required by the Format.format() method that appends to a string
* buffer. */
protected FieldPosition _fpos = new FieldPosition(SimpleDateFormat.DATE_FIELD);
/** Timestamp format used on all log messages. */ /** Timestamp format used on all log messages. */
protected static final String TIMESTAMP_FORMAT = protected static final String TIMESTAMP_FORMAT = "yyyy/MM/dd HH:mm:ss:SSS ";
"yyyy/MM/dd HH:mm:ss:SSS ";
/** The length of the timestamp format. */ /** The length of the timestamp format. */
protected static final int TIMESTAMP_LENGTH = TIMESTAMP_FORMAT.length(); protected static final int TIMESTAMP_LENGTH = TIMESTAMP_FORMAT.length();
/** Used to format timestamps. */
protected SimpleDateFormat _format =
new SimpleDateFormat(TIMESTAMP_FORMAT);
/** Annoying parameter required by the Format.format() method that
* appends to a string buffer. */
protected FieldPosition _fpos =
new FieldPosition(SimpleDateFormat.DATE_FIELD);
} }