1 /* Simple1LineFormatter.java -- A simple 1-line logging formatter
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.classpath.debug;
41 import gnu.java.security.action.GetPropertyAction;
43 import java.io.PrintWriter;
44 import java.io.StringWriter;
45 import java.security.AccessController;
46 import java.text.DateFormat;
47 import java.text.DecimalFormat;
48 import java.text.NumberFormat;
49 import java.text.SimpleDateFormat;
50 import java.util.Date;
51 import java.util.logging.Formatter;
52 import java.util.logging.LogRecord;
55 * A simple 1-line formatter to use instead of the 2-line SimpleFormatter used
56 * by default in the JDK logging handlers.
58 * The fixed format of this formatter is as follows:
61 * <li>Date: As a yyyy-MM-dd string.</li>
62 * <li>Time: As a HH:mm:ss.SSSS Z string.</li>
63 * <li>Thread identifier, right-justified, and framed in an 11-digit field.</li>
64 * <li>Class name, without its package name, left-justified, and framed in a
65 * 32-character field.</li>
66 * <li>Method name, left-justified, and framed in a 32-character field.</li>
67 * <li>Level name, left-justified, and framed in a 6-character field.</li>
68 * <li>User message and arguments.</li>
69 * <li>Platform-dependent line-separator.</li>
70 * <li>Optionally, if the log-record contains a thrown exception, that
71 * exception's stack trace is appended to the output.</li>
74 * Here is an example of the output generated by this formatter:
77 * 2006-02-27 21:59:12.0881 +1100 -1343151280 EncodedKeyFactory engineGeneratePublic() FINER - ENTRY java.security.spec.X509EncodedKeySpec@b00d7fc0
78 * 2006-02-27 21:59:12.0887 +1100 -1343151280 EncodedKeyFactory engineGeneratePublic() FINE - Exception in DSSPublicKey.valueOf(). Ignore
79 * java.security.InvalidParameterException: Unexpected OID: 1.2.840.113549.1.1.1
80 * at gnu.java.security.key.dss.DSSKeyPairX509Codec.decodePublicKey (DSSKeyPairX509Codec.java:205)
81 * at gnu.java.security.key.dss.DSSPublicKey.valueOf (DSSPublicKey.java:136)
82 * at gnu.java.security.jce.sig.EncodedKeyFactory.engineGeneratePublic (EncodedKeyFactory.java:218)
83 * at java.security.KeyFactory.generatePublic (KeyFactory.java:219)
84 * at gnu.java.security.x509.X509Certificate.parse (X509Certificate.java:657)
85 * at gnu.java.security.x509.X509Certificate.<init> (X509Certificate.java:163)
87 * 2006-02-27 21:59:12.0895 +1100 -1343151280 RSAKeyPairX509Codec decodePublicKey() FINER - ENTRY [B@b00d7fd0
88 * 2006-02-27 21:59:12.0897 +1100 -1343151280 RSAKeyPairX509Codec decodePublicKey() FINER - RETURN gnu.java.security.key.rsa.GnuRSAPublicKey@b00fb940
91 public class Simple1LineFormatter
94 private static final String DAT_PATTERN = "yyyy-MM-dd HH:mm:ss.SSSS Z ";
95 private static final String THREAD_PATTERN = " #########0;-#########0";
96 private static final String SPACES_32 = " ";
97 private static final String SPACES_6 = " ";
98 private static final String LS = (String) AccessController.doPrivileged
99 (new GetPropertyAction("line.separator"));
100 private DateFormat dateFormat;
101 private NumberFormat threadFormat;
103 // default 0-arguments constructor
105 public String format(LogRecord record)
107 if (dateFormat == null)
108 dateFormat = new SimpleDateFormat(DAT_PATTERN);
110 if (threadFormat == null)
111 threadFormat = new DecimalFormat(THREAD_PATTERN);
113 StringBuilder sb = new StringBuilder(180)
114 .append(dateFormat.format(new Date(record.getMillis())))
115 .append(threadFormat.format(record.getThreadID()))
117 String s = record.getSourceClassName();
119 sb.append(SPACES_32);
123 int i = s.lastIndexOf(".");
125 s = s.substring(i + 1);
127 s = (s + SPACES_32).substring(0, 32);
130 sb.append(s).append(" ");
131 s = record.getSourceMethodName();
133 sb.append(SPACES_32);
137 if (s.endsWith("()"))
138 s = (s.trim() + SPACES_32).substring(0, 32);
140 s = (s.trim() + "()" + SPACES_32).substring(0, 32);
143 sb.append(s).append(" ");
144 s = String.valueOf(record.getLevel());
148 s = (s.trim() + SPACES_6).substring(0, 6);
150 sb.append(s).append(" - ").append(formatMessage(record)).append(LS);
151 Throwable cause = record.getThrown();
154 StringWriter sw = new StringWriter();
155 cause.printStackTrace(new PrintWriter(sw, true));
156 sb.append(sw.toString());
159 return sb.toString();