001: /*
002: * Created on Mar 22, 2005
003: */
004: package uk.org.ponder.util;
005:
006: import java.io.PrintWriter;
007: import java.io.StringWriter;
008: import java.text.MessageFormat;
009: import java.util.Date;
010: import java.util.logging.ConsoleHandler;
011: import java.util.logging.FileHandler;
012: import java.util.logging.Formatter;
013: import java.util.logging.Handler;
014: import java.util.logging.LogRecord;
015: import java.util.logging.Logger;
016:
017: /**
018: * A disused formatting class for use with JDK logging.
019: * @author Antranig Basman (antranig@caret.cam.ac.uk)
020: *
021: */
022: public class FineFormatter extends Formatter {
023:
024: public static void configureLogger(Logger logger) {
025: Handler[] existing = logger.getHandlers();
026: if (existing.length > 0) {
027: System.out.println("**** removing " + existing.length
028: + " existing handlesr");
029: }
030: for (int i = 0; i < existing.length; ++i) {
031: logger.removeHandler(existing[i]);
032: }
033: logger.setUseParentHandlers(false);
034: FineFormatter finef = new FineFormatter();
035: ConsoleHandler console = new ConsoleHandler();
036: console.setFormatter(finef);
037: logger.addHandler(console);
038: FileHandler file;
039: try {
040: file = new FileHandler();
041: file.setFormatter(finef);
042: //logger.addHandler(file);
043: } catch (Exception e) {
044: e.printStackTrace();
045: }
046: }
047:
048: Date dat = new Date();
049: private final static String format = "{0,date,yyyy-MM-dd'T'HH:mm:ss.SSS'Z'}";
050: private MessageFormat formatter;
051:
052: private Object args[] = new Object[1];
053:
054: // Line separator string. This is the value of the line.separator
055: // property at the moment that the SimpleFormatter was created.
056: private String lineSeparator = System.getProperty("line.separator");
057:
058: /**
059: * Format the given LogRecord.
060: *
061: * @param record
062: * the log record to be formatted.
063: * @return a formatted log record
064: */
065: public synchronized String format(LogRecord record) {
066: StringBuffer sb = new StringBuffer();
067: // Minimize memory allocations here.
068: dat.setTime(record.getMillis());
069: args[0] = dat;
070: StringBuffer text = new StringBuffer();
071: if (formatter == null) {
072: formatter = new MessageFormat(format);
073: }
074: formatter.format(args, text, null);
075: sb.append(text);
076: sb.append(" ");
077: if (record.getSourceClassName() != null) {
078: sb.append(record.getSourceClassName());
079: } else {
080: sb.append(record.getLoggerName());
081: }
082: if (record.getSourceMethodName() != null) {
083: sb.append(" ");
084: sb.append(record.getSourceMethodName());
085: }
086: sb.append(lineSeparator);
087: String message = formatMessage(record);
088: sb.append(record.getLevel().getLocalizedName());
089: sb.append(": ");
090: sb.append(message);
091: sb.append(lineSeparator);
092: if (record.getThrown() != null) {
093: try {
094: StringWriter sw = new StringWriter();
095: PrintWriter pw = new PrintWriter(sw);
096: record.getThrown().printStackTrace(pw);
097: pw.close();
098: sb.append(sw.toString());
099: } catch (Exception ex) {
100: }
101: }
102: return sb.toString();
103: }
104: }
|