KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > util > log > LogFormatter


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.util.log;
19
20 import java.text.SimpleDateFormat JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.logging.Formatter JavaDoc;
23 import java.util.logging.LogRecord JavaDoc;
24
25 /**
26  * A class wich specifies the format for the log messages
27  * @author Ronald Kramp - Finalist IT Group
28  * @version $Revision: 1.1 $, $Date: 2004/11/12 14:06:44 $
29  */

30 public final class LogFormatter extends Formatter JavaDoc {
31
32    /** a variable for specifying a given data format */
33    private SimpleDateFormat JavaDoc dateFormatter;
34
35    /**
36     * a variable the will set the number of last packages will be shown
37     * example number=3 com.finalist.appname.util.log.LogFormatter
38     * will be util.log.LogFormatter
39     */

40    private int showNumberOfLastPackages;
41
42    /** a variable for specifying the separator between the Classname and the message */
43    private String JavaDoc messageSeparator;
44
45
46    /**
47     * Constrcutor for making a LogFormatter
48     * @param showNumberOfLastPackages the number of packages to log
49     * @param datePattern the pattern of the date to log
50     * @param messageSeparator the message separator
51     */

52    public LogFormatter(int showNumberOfLastPackages, String JavaDoc datePattern, String JavaDoc messageSeparator) {
53       this.dateFormatter = new SimpleDateFormat JavaDoc(datePattern);
54       this.messageSeparator = messageSeparator;
55       this.showNumberOfLastPackages = showNumberOfLastPackages;
56    }
57
58
59    /**
60     * Formatting a logrecord to a single line.
61     * sequence of the logline: Logevel date Classname separator message exception
62     * @param rec the log record to format
63     * @return String the line to log
64     */

65    public String JavaDoc format(LogRecord JavaDoc rec) {
66       StringBuffer JavaDoc buf = new StringBuffer JavaDoc(1000);
67       StringBuffer JavaDoc packageClass = new StringBuffer JavaDoc(50);
68       String JavaDoc loggerName = rec.getLoggerName();
69
70       if (this.showNumberOfLastPackages <= 0) {
71          packageClass.append(loggerName);
72       }
73
74       for (int i = 0; i < this.showNumberOfLastPackages; i++) {
75          int index = loggerName.lastIndexOf(".");
76
77          if (index > -1) {
78             packageClass.insert(0, loggerName.substring(index, loggerName.length()));
79             loggerName = loggerName.substring(0, index);
80          }
81          else {
82             packageClass.insert(0, loggerName);
83             break;
84          }
85       }
86       if (packageClass.charAt(0) == '.') {
87          packageClass.deleteCharAt(0);
88       }
89       //String level = rec.getLevel().toString();
90
//level = (level.length() >= 5) ? level.substring(0, 5) : level + " ";
91

92       buf.append(rec.getLevel());
93       buf.append(" ");
94       buf.delete(8, buf.length());
95       buf.insert(8, dateFormatter.format(new Date JavaDoc(rec.getMillis())));
96       buf.append(" ");
97       buf.append(packageClass);
98       buf.append(" ");
99       buf.append(this.messageSeparator);
100       buf.append(" ");
101       buf.append(formatMessage(rec));
102       buf.append((rec.getThrown() != null) ? rec.getThrown().toString() : "");
103       buf.append('\n');
104       return buf.toString();
105    }
106 }
Popular Tags