001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of jCommonTk.
006: *
007: * jCommonTk is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License (Version 2) as published by
009: * the Free Software Foundation.
010: *
011: * jCommonTk is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with jCommonTk; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jcommontk.utils;
020:
021: import java.io.OutputStream;
022: import java.io.PrintWriter;
023: import java.io.StringWriter;
024: import java.text.SimpleDateFormat;
025: import java.util.Date;
026: import java.util.StringTokenizer;
027: import java.util.logging.Formatter;
028: import java.util.logging.Handler;
029: import java.util.logging.LogRecord;
030:
031: public class LoggerUtils {
032: public static class OutputStreamHandler extends Handler {
033: OutputStream out;
034:
035: public OutputStreamHandler(OutputStream out,
036: boolean showSourceInfo, boolean indent) {
037: this .out = out;
038: setFormatter(new SimpleFormatter(showSourceInfo, indent));
039: }
040:
041: public void flush() {
042: try {
043: out.flush();
044: } catch (Exception e) {
045: e.printStackTrace();
046: }
047: }
048:
049: public void close() {
050: }
051:
052: public void publish(LogRecord record) {
053: if (isLoggable(record))
054: try {
055: out.write(getFormatter().format(record).getBytes());
056: } catch (Exception e) {
057: e.printStackTrace(System.err);
058: }
059: }
060: }
061:
062: public static class SimpleFormatter extends Formatter {
063: Date d = new Date();
064: SimpleDateFormat df = new SimpleDateFormat("MM-dd-yy HH:mm:ss");
065: boolean showSourceInfo, indent;
066:
067: public SimpleFormatter(boolean showSourceInfo, boolean indent) {
068: this .showSourceInfo = showSourceInfo;
069: this .indent = indent;
070: }
071:
072: public String format(LogRecord record) {
073: d.setTime(record.getMillis());
074:
075: StringWriter sw = new StringWriter();
076: PrintWriter out = new PrintWriter(sw);
077:
078: out.print(df.format(d) + " " + record.getLevel() + " - ");
079:
080: if (showSourceInfo)
081: out.println(record.getSourceClassName() + "::"
082: + record.getSourceMethodName());
083:
084: if (indent) {
085: StringTokenizer strtok = new StringTokenizer(record
086: .getMessage(), "\n");
087:
088: while (strtok.hasMoreTokens())
089: out.println(" " + strtok.nextToken());
090: } else
091: out.println(record.getMessage());
092:
093: Object[] params = record.getParameters();
094:
095: if (params != null)
096: out.println("Parameters: "
097: + StringUtils.toString(params));
098:
099: Throwable t = record.getThrown();
100:
101: if (t != null)
102: t.printStackTrace(out);
103:
104: return sw.toString();
105: }
106: }
107: }
|