001: /* Copyright (C) 2003 Finalist IT Group
002: *
003: * This file is part of JAG - the Java J2EE Application Generator
004: *
005: * JAG is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: * JAG is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: * You should have received a copy of the GNU General Public License
014: * along with JAG; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: */
017:
018: package com.finalist.util.log;
019:
020: import java.text.SimpleDateFormat;
021: import java.util.Date;
022: import java.util.logging.Formatter;
023: import java.util.logging.LogRecord;
024:
025: /**
026: * A class wich specifies the format for the log messages
027: * @author Ronald Kramp - Finalist IT Group
028: * @version $Revision: 1.1 $, $Date: 2004/11/12 14:06:44 $
029: */
030: public final class LogFormatter extends Formatter {
031:
032: /** a variable for specifying a given data format */
033: private SimpleDateFormat dateFormatter;
034:
035: /**
036: * a variable the will set the number of last packages will be shown
037: * example number=3 com.finalist.appname.util.log.LogFormatter
038: * will be util.log.LogFormatter
039: */
040: private int showNumberOfLastPackages;
041:
042: /** a variable for specifying the separator between the Classname and the message */
043: private String messageSeparator;
044:
045: /**
046: * Constrcutor for making a LogFormatter
047: * @param showNumberOfLastPackages the number of packages to log
048: * @param datePattern the pattern of the date to log
049: * @param messageSeparator the message separator
050: */
051: public LogFormatter(int showNumberOfLastPackages,
052: String datePattern, String messageSeparator) {
053: this .dateFormatter = new SimpleDateFormat(datePattern);
054: this .messageSeparator = messageSeparator;
055: this .showNumberOfLastPackages = showNumberOfLastPackages;
056: }
057:
058: /**
059: * Formatting a logrecord to a single line.
060: * sequence of the logline: Logevel date Classname separator message exception
061: * @param rec the log record to format
062: * @return String the line to log
063: */
064: public String format(LogRecord rec) {
065: StringBuffer buf = new StringBuffer(1000);
066: StringBuffer packageClass = new StringBuffer(50);
067: String loggerName = rec.getLoggerName();
068:
069: if (this .showNumberOfLastPackages <= 0) {
070: packageClass.append(loggerName);
071: }
072:
073: for (int i = 0; i < this .showNumberOfLastPackages; i++) {
074: int index = loggerName.lastIndexOf(".");
075:
076: if (index > -1) {
077: packageClass.insert(0, loggerName.substring(index,
078: loggerName.length()));
079: loggerName = loggerName.substring(0, index);
080: } else {
081: packageClass.insert(0, loggerName);
082: break;
083: }
084: }
085: if (packageClass.charAt(0) == '.') {
086: packageClass.deleteCharAt(0);
087: }
088: //String level = rec.getLevel().toString();
089: //level = (level.length() >= 5) ? level.substring(0, 5) : level + " ";
090:
091: buf.append(rec.getLevel());
092: buf.append(" ");
093: buf.delete(8, buf.length());
094: buf.insert(8, dateFormatter.format(new Date(rec.getMillis())));
095: buf.append(" ");
096: buf.append(packageClass);
097: buf.append(" ");
098: buf.append(this .messageSeparator);
099: buf.append(" ");
100: buf.append(formatMessage(rec));
101: buf.append((rec.getThrown() != null) ? rec.getThrown()
102: .toString() : "");
103: buf.append('\n');
104: return buf.toString();
105: }
106: }
|