001: //severSimpleLogFormatter.java
002: //-------------------------------------
003: //part of YACY
004: //(C) by Michael Peter Christen; mc@anomic.de
005: //first published on http://www.anomic.de
006: //Frankfurt, Germany, 2004
007: //
008: //This file is contributed by Martin Thelian
009: //last major change: $LastChangedDate: 2005-09-20 21:49:47 +0000 (Di, 20 Sep 2005) $ by $LastChangedBy: theli $
010: //Revision: $LastChangedRevision: 757 $
011: //
012: //This program is free software; you can redistribute it and/or modify
013: //it under the terms of the GNU General Public License as published by
014: //the Free Software Foundation; either version 2 of the License, or
015: //(at your option) any later version.
016: //
017: //This program is distributed in the hope that it will be useful,
018: //but WITHOUT ANY WARRANTY; without even the implied warranty of
019: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: //GNU General Public License for more details.
021: //
022: //You should have received a copy of the GNU General Public License
023: //along with this program; if not, write to the Free Software
024: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
025: //
026: //Using this software in any meaning (reading, learning, copying, compiling,
027: //running) means that you agree that the Author(s) is (are) not responsible
028: //for cost, loss of data or any harm that may be caused directly or indirectly
029: //by usage of this softare or this documentation. The usage of this software
030: //is on your own risk. The installation and usage (starting/running) of this
031: //software may allow other people or application to access your computer and
032: //any attached devices and is highly dependent on the configuration of the
033: //software which must be done by the user of the software; the author(s) is
034: //(are) also not responsible for proper configuration and usage of the
035: //software, even if provoked by documentation provided together with
036: //the software.
037: //
038: //Any changes to this file according to the GPL as documented in the file
039: //gpl.txt aside this file in the shipment you received can be done to the
040: //lines that follows this copyright notice here, but changes must not be
041: //done inside the copyright notive above. A re-distribution must contain
042: //the intact and unchanged copyright notice.
043: //Contributions and changes to the program code must be marked as such.
044:
045: package de.anomic.server.logging;
046:
047: import java.io.PrintWriter;
048: import java.io.StringWriter;
049: import java.text.FieldPosition;
050: import java.text.SimpleDateFormat;
051: import java.util.Date;
052: import java.util.logging.LogRecord;
053: import java.util.logging.SimpleFormatter;
054:
055: public class serverSimpleLogFormatter extends SimpleFormatter {
056:
057: private Date date = new Date();
058: private final FieldPosition position = new FieldPosition(0);
059:
060: // e.g. 2005/05/25 11:22:53
061: private final SimpleDateFormat formatter = new SimpleDateFormat(
062: "yyyy/MM/dd HH:mm:ss");
063:
064: private final StringBuffer buffer = new StringBuffer();
065:
066: public serverSimpleLogFormatter() {
067: super ();
068: }
069:
070: public synchronized String format(LogRecord record) {
071:
072: StringBuffer buffer = this .buffer;
073: buffer.setLength(0);
074:
075: // adding the loglevel
076: int logLevel = record.getLevel().intValue();
077: if (logLevel == serverLog.LOGLEVEL_SEVERE)
078: this .buffer.append(serverLog.LOGTOKEN_SEVERE);
079: else if (logLevel == serverLog.LOGLEVEL_WARNING)
080: this .buffer.append(serverLog.LOGTOKEN_WARNING);
081: else if (logLevel == serverLog.LOGLEVEL_CONFIG)
082: this .buffer.append(serverLog.LOGTOKEN_CONFIG);
083: else if (logLevel == serverLog.LOGLEVEL_INFO)
084: this .buffer.append(serverLog.LOGTOKEN_INFO);
085: else if (logLevel == serverLog.LOGLEVEL_FINE)
086: this .buffer.append(serverLog.LOGTOKEN_FINE);
087: else if (logLevel == serverLog.LOGLEVEL_FINER)
088: this .buffer.append(serverLog.LOGTOKEN_FINER);
089: else if (logLevel == serverLog.LOGLEVEL_FINEST)
090: this .buffer.append(serverLog.LOGTOKEN_FINEST);
091: else
092: this .buffer.append(serverLog.LOGTOKEN_FINE);
093: this .buffer.append(' ');
094:
095: // adding the logging date
096: this .date.setTime(record.getMillis());
097: this .position.setBeginIndex(0);
098: this .formatter.format(this .date, this .buffer, this .position);
099:
100: // adding the logger name
101: buffer.append(' ');
102: buffer.append(record.getLoggerName());
103:
104: // adding the logging message
105: buffer.append(' ');
106: buffer.append(formatMessage(record));
107:
108: // adding the stack trace if available
109: buffer.append(System.getProperty("line.separator"));
110: if (record.getThrown() != null) {
111: StringWriter writer = null;
112: try {
113: writer = new StringWriter();
114: PrintWriter printer = new PrintWriter(writer);
115: record.getThrown().printStackTrace(printer);
116: buffer.append(writer.toString());
117: } catch (Exception e) {
118: buffer.append("Failed to get stack trace: "
119: + e.getMessage());
120: } finally {
121: if (writer != null)
122: try {
123: writer.close();
124: } catch (Exception ex) {
125: }
126: }
127: }
128: return buffer.toString();
129: }
130: }
|