Patch from Charlie to add magic formatting to the AuditLogger.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@2439 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
samskivert
2008-09-22 16:47:46 +00:00
parent 5248d137fc
commit 12292b89f2
2 changed files with 32 additions and 28 deletions
@@ -64,12 +64,14 @@ public class AuditLogger
} }
/** /**
* Writes the supplied message to the log, prefixed by a date and timestamp. A newline will be * Builds a log entry out of the given message and args using
* appended to the message. * {@link Logger#format(Object, Object...)}, prefixes it with the current date and time,
* appends a newline to it and writes it to the log.
*/ */
public synchronized void log (String message) public synchronized void log (String message, Object...args)
{ {
// construct the message // construct the message
message = Logger.format(message, args);
StringBuffer buf = new StringBuffer(message.length() + TIMESTAMP_LENGTH); StringBuffer buf = new StringBuffer(message.length() + TIMESTAMP_LENGTH);
_format.format(new Date(), buf, _fpos); _format.format(new Date(), buf, _fpos);
buf.append(message); buf.append(message);
+27 -25
View File
@@ -39,6 +39,33 @@ package com.samskivert.util;
*/ */
public abstract class Logger public abstract class Logger
{ {
/**
* Formats the given message and array of alternating key value pairs like so:
*
* <pre>message [key=value, key=value, key=value]</pre>
*/
public static String format (Object message, Object... args)
{
StringBuilder buf = new StringBuilder();
buf.append(message);
if (args != null && args.length > 1) {
buf.append(" [");
for (int ii = 0, nn = args.length/2; ii < nn; ii++) {
if (ii > 0) {
buf.append(", ");
}
buf.append(args[2*ii]).append("=");
try {
StringUtil.toString(buf, args[2*ii+1]);
} catch (Throwable t) {
buf.append("<toString() failure: " + t + ">");
}
}
buf.append("]");
}
return buf.toString();
}
/** /**
* Used to create logger instances. This is only public so that the log factory can be * Used to create logger instances. This is only public so that the log factory can be
* configured programmatically via {@link Logger#setFactory}. * configured programmatically via {@link Logger#setFactory}.
@@ -110,31 +137,6 @@ public abstract class Logger
*/ */
public abstract void error (Object message, Object... args); public abstract void error (Object message, Object... args);
/**
* Format messages and arguments. For use by logger implementations.
*/
protected String format (Object message, Object[] args)
{
StringBuilder buf = new StringBuilder();
buf.append(message);
if (args != null && args.length > 1) {
buf.append(" [");
for (int ii = 0, nn = args.length/2; ii < nn; ii++) {
if (ii > 0) {
buf.append(", ");
}
buf.append(args[2*ii]).append("=");
try {
StringUtil.toString(buf, args[2*ii+1]);
} catch (Throwable t) {
buf.append("<toString() failure: " + t + ">");
}
}
buf.append("]");
}
return buf.toString();
}
/** /**
* Extracts the exception from the message and arguments (if there is one). For use by logger * Extracts the exception from the message and arguments (if there is one). For use by logger
* implementations. * implementations.