001: /*
002: Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
003:
004: This file is part of the JODB (Java Objects Database) open source project.
005:
006: JODB is free software; you can redistribute it and/or modify it under
007: the terms of version 2 of the GNU General Public License as published
008: by the Free Software Foundation.
009:
010: JODB is distributed in the hope that it will be useful, but WITHOUT ANY
011: WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013: for more details.
014:
015: You should have received a copy of the GNU General Public License along
016: with this program; if not, write to the Free Software Foundation, Inc.,
017: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019: package com.mobixess.jodb.util.logging;
020:
021: import java.io.*;
022: import java.text.*;
023: import java.util.Date;
024: import java.util.logging.Formatter;
025: import java.util.logging.Level;
026: import java.util.logging.LogRecord;
027: import java.util.logging.Logger;
028:
029: /**
030: * Print a brief summary of the LogRecord in a human readable format. The
031: * summary will typically be 1 or 2 lines.
032: *
033: * @version 1.14, 12/19/03
034: * @since 1.4
035: */
036:
037: public class SimpleFileFormatter extends Formatter {
038:
039: Date dat = new Date();
040:
041: private final static String format = "{0,date} {0,time}";
042:
043: private MessageFormat formatter;
044:
045: private Object args[] = new Object[1];
046:
047: // Line separator string. This is the value of the line.separator
048: // property at the moment that the SimpleFormatter was created.
049: @SuppressWarnings("unchecked")
050: private String lineSeparator = (String) java.security.AccessController
051: .doPrivileged(new sun.security.action.GetPropertyAction(
052: "line.separator"));
053:
054: /**
055: * Format the given LogRecord.
056: *
057: * @param record
058: * the log record to be formatted.
059: * @return a formatted log record
060: */
061: public synchronized String format(LogRecord record) {
062: StringBuffer sb = new StringBuffer();
063: // Minimize memory allocations here.
064: dat.setTime(record.getMillis());
065: args[0] = dat;
066: StringBuffer text = new StringBuffer();
067: if (formatter == null) {
068: formatter = new MessageFormat(format);
069: }
070: formatter.format(args, text, null);
071: sb.append(text);
072: sb.append(" ");
073:
074: // if (record.getSourceClassName() != null) {
075: // sb.append(record.getSourceClassName());
076: // } else {
077: // sb.append(record.getLoggerName());
078: // }
079: // if (record.getSourceMethodName() != null) {
080: // sb.append(".");
081: // sb.append(record.getSourceMethodName());
082: // }
083:
084: sb.append(getLineAndSrc());
085:
086: sb.append(" Thread: " + record.getThreadID());
087:
088: sb.append(lineSeparator);
089: String message = formatMessage(record);
090: sb.append("\t" + record.getLevel().getLocalizedName());
091: sb.append(": ");
092: sb.append(message);
093: sb.append(lineSeparator);
094: if (record.getThrown() != null) {
095: try {
096: StringWriter sw = new StringWriter();
097: PrintWriter pw = new PrintWriter(sw);
098: record.getThrown().printStackTrace(pw);
099: pw.close();
100: sb.append(sw.toString());
101: } catch (Exception ex) {
102: }
103: sb.append(lineSeparator);
104: }
105: String result = sb.toString();
106:
107: if (record.getLevel().intValue() <= Level.INFO.intValue()) {
108: System.out.print(result);
109: } else {
110: System.err.print(result);
111: }
112: return result;
113: }
114:
115: private String[] LOGGING_CLASSES_FULL = new String[] { Logger.class
116: .getName() };
117:
118: private String[] LOGGING_CLASSES_PREFIXES = new String[] {
119: "com.spsoft.msgboard.management.userBrowser.core.container.BrowserAppLogger",
120: "java.security.AccessController" };
121:
122: // Private method to infer the caller's class and method names
123: protected String getLineAndSrc() {
124: // Get the stack trace.
125: StackTraceElement stack[] = (new Throwable()).getStackTrace();
126: // First, search back to a method in the Logger class.
127: int ix = 0;
128: while (ix < stack.length) {
129: StackTraceElement frame = stack[ix];
130: String cname = frame.getClassName();
131: if (isLoggingClass(cname)) {
132: break;
133: }
134: ix++;
135: }
136: // Now search for the first frame before the "Logger" class.
137: while (ix < stack.length) {
138: StackTraceElement frame = stack[ix];
139: String cname = frame.getClassName();
140: if (!isLoggingClass(cname)) {
141: // We've found the relevant frame.
142: //setSourceClassName(cname);
143: //setSourceMethodName(frame.getMethodName());
144: return frame.getClassName() + '.'
145: + frame.getMethodName() + '('
146: + frame.getFileName() + ':'
147: + frame.getLineNumber() + ')';
148:
149: }
150: ix++;
151: }
152: // We haven't found a suitable frame, so just punt. This is
153: // OK as we are only committed to making a "best effort" here.
154: return "";
155: }
156:
157: private boolean isLoggingClass(String name) {
158: for (int i = 0; i < LOGGING_CLASSES_FULL.length; i++) {
159: if (LOGGING_CLASSES_FULL[i].equals(name)) {
160: return true;
161: }
162: }
163: for (int i = 0; i < LOGGING_CLASSES_PREFIXES.length; i++) {
164: if (name.startsWith(LOGGING_CLASSES_PREFIXES[i])) {
165: return true;
166: }
167: }
168: return false;
169: }
170:
171: }
|