1 package com.jme.util.logging;
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5 import java.text.MessageFormat;
7 import java.util.logging.Formatter;
8 import java.util.logging.LogRecord;
10 public class BetterFormatter extends Formatter {
12 Date dat = new Date();
13 // private final static String format = "{0,date} {0,time}";
14 private final static String format = "{0,time}";
15 private MessageFormat formatter;
17 private Object args[] = new Object[1];
19 // Line separator string. This is the value of the line.separator
20 // property at the moment that the SimpleFormatter was created.
21 private String lineSeparator = (String) java.security.AccessController
22 .doPrivileged(new sun.security.action.GetPropertyAction(
26 * Format the given LogRecord.
29 * the log record to be formatted.
30 * @return a formatted log record
32 public synchronized String format(LogRecord record) {
33 StringBuffer sb = new StringBuffer();
35 dat.setTime(record.getMillis());
37 StringBuffer text = new StringBuffer();
38 if (formatter == null) {
39 formatter = new MessageFormat(format);
41 formatter.format(args, text, null);
45 sb.append(record.getThreadID());
48 if (record.getSourceClassName() != null) {
49 sb.append(record.getSourceClassName());
51 sb.append(record.getLoggerName());
53 if (record.getSourceMethodName() != null) {
55 sb.append(record.getSourceMethodName());
60 String message = formatMessage(record);
61 sb.append(record.getLevel().getLocalizedName());
66 Object[] params = record.getParameters();
69 for (int i=0; i<params.length; i++) {
71 if (i < params.length - 1) {
76 // sb.append(lineSeparator);
79 // Find all the threads
80 // ThreadGroup root = Thread.currentThread().getThreadGroup().getParent();
81 // while (root.getParent() != null) {
82 // root = root.getParent();
84 // visit(sb, root, 0);
86 if (record.getThrown() != null) {
87 sb.append(lineSeparator);
89 StringWriter sw = new StringWriter();
90 PrintWriter pw = new PrintWriter(sw);
91 record.getThrown().printStackTrace(pw);
93 sb.append(sw.toString());
94 } catch (Exception ex) {
98 sb.append(lineSeparator);
100 return sb.toString();
103 // This method recursively visits all thread groups under `group'.
104 private void visit(StringBuffer sb, ThreadGroup group, int level) {
105 // Get threads in `group'
106 int numThreads = group.activeCount();
107 Thread[] threads = new Thread[numThreads*2];
108 numThreads = group.enumerate(threads, false);
110 // Enumerate each thread in `group'
111 for (int i=0; i<numThreads; i++) {
113 Thread thread = threads[i];
115 sb.append(thread.getName() + "[" + thread.getId() + "]");
116 sb.append(lineSeparator);
119 // Get thread subgroups of `group'
120 int numGroups = group.activeGroupCount();
121 ThreadGroup[] groups = new ThreadGroup[numGroups*2];
122 numGroups = group.enumerate(groups, false);
124 // Recursively visit each subgroup
125 for (int i=0; i<numGroups; i++) {
126 visit(sb, groups[i], level+1);