KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > logging > ServerFormatter


1
2 /*
3  * The contents of this file are subject to the terms
4  * of the Common Development and Distribution License
5  * (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the license at
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
10  * glassfish/bootstrap/legal/CDDLv1.0.txt.
11  * See the License for the specific language governing
12  * permissions and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL
15  * Header Notice in each file and include the License file
16  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
17  * If applicable, add the following below the CDDL Header,
18  * with the fields enclosed by brackets [] replaced by
19  * you own identifying information:
20  * "Portions Copyrighted [year] [name of copyright owner]"
21  *
22  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23  */

24 package com.sun.enterprise.server.logging;
25
26
27
28 import java.util.logging.SimpleFormatter JavaDoc;
29 import java.util.logging.LogRecord JavaDoc;
30
31 import java.text.MessageFormat JavaDoc;
32
33 import java.io.PrintWriter JavaDoc;
34 import java.io.StringWriter JavaDoc;
35
36
37 /**
38  * Class ServerFormatter is a formatter designed for use by the ServerHandler
39  * and designed for use by ereport (the web core native logging facility).
40  * ereport is responsible for example for outputting the message header.
41  */

42 public class ServerFormatter extends SimpleFormatter JavaDoc {
43
44     private MessageFormat JavaDoc formatter;
45
46     // Line separator string. This is the value of the line.separator
47
// property at the moment that the SimpleFormatter was created.
48
// If we need to change the default line-seperator, it should be
49
// done here.
50
private static final String JavaDoc lineSeparator =
51         (String JavaDoc) java.security.AccessController
52             .doPrivileged(new sun.security.action
53                 .GetPropertyAction("line.separator"));
54     private static final String JavaDoc fieldSeparator = " ";
55
56     /**
57      * Method format
58      *
59      *
60      * @param record
61      *
62      * @return
63      */

64     public synchronized String JavaDoc format(LogRecord JavaDoc record) {
65
66         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
67
68         // Get localized message after formatting per resource bundle if any.
69
String JavaDoc message = formatMessage(record);
70
71         sb.append(message);
72
73         // Get stack trace if message was a result of exception.
74
if (record.getThrown() != null) {
75             try {
76                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
77                 PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
78
79                 record.getThrown().printStackTrace(pw);
80                 pw.close();
81                 sb.append(lineSeparator);
82                 sb.append(sw.toString());
83             } catch (Exception JavaDoc ex) {}
84         }
85         sb.append(lineSeparator);
86
87         return sb.toString();
88     }
89 }
90
91
Popular Tags