KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > logging > DefaultSessionLog


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

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.logging;
23
24 import java.io.*;
25 import oracle.toplink.essentials.exceptions.*;
26 import oracle.toplink.essentials.internal.helper.*;
27
28 /**
29  * <p><b>Purpose</b>: Default log used for the session when message logging is enabled.
30  * The session can log information such as,<ul>
31  * <li> all SQL executed
32  * <li> informational messages
33  * <li> debugging information
34  * <li> all exceptions that occur within TopLink
35  * </ul>
36  * As well information about the message can be logged such as,<ul>
37  * <li> the session logging the message
38  * <li> the connection executing the SQL
39  * <li> the thread in which the log entry occured
40  * <li> the exact time (to milliseconds) that the log entry occured
41  * <li> the stack trace to the exception
42  * </ul>
43  * @see SessionLog
44  * @see Session#logMessage(String)
45  *
46  * @author Big Country
47  */

48 public class DefaultSessionLog extends AbstractSessionLog implements Serializable {
49
50     /** The filename associated with this DefaultSessionLog, if it is being written out to a file **/
51     protected String JavaDoc fileName;
52
53     /**
54      * PUBLIC:
55      * Create a new default session log.
56      */

57     public DefaultSessionLog() {
58         super();
59         this.level = INFO;
60     }
61
62     /**
63      * PUBLIC:
64      * Create a new default session log for the given writer.
65      */

66     public DefaultSessionLog(Writer writer) {
67         this();
68         this.initialize(writer);
69     }
70
71     /**
72      * Initialize the log.
73      */

74     protected void initialize(Writer writer) {
75         this.writer = writer;
76     }
77
78     /**
79      * INTERNAL:
80      * Log the entry.
81      * This writes the log entries information to a writer such as System.out or a file.
82      * This must be synchronized as it will be called by many threads in three-tier.
83      */

84     public synchronized void log(SessionLogEntry entry) {
85         if (!shouldLog(entry.getLevel())) {
86             return;
87         }
88
89         try {
90             printPrefixString(entry.getLevel());
91             this.getWriter().write(getSupplementDetailString(entry));
92
93             if (entry.hasException()) {
94                 if (entry.getLevel() == SEVERE) {
95                     entry.getException().printStackTrace(new PrintWriter(getWriter()));
96                 } else if (entry.getLevel() <= WARNING) {
97                     if (shouldLogExceptionStackTrace()) {
98                         entry.getException().printStackTrace(new PrintWriter(getWriter()));
99                     } else {
100                         writeMessage(entry.getException().toString());
101                     }
102                 }
103             } else {
104                 writeMessage(formatMessage(entry));
105             }
106             getWriter().write(Helper.cr());
107             getWriter().flush();
108         } catch (IOException exception) {
109             throw ValidationException.logIOError(exception);
110         }
111     }
112
113     /**
114      * PUBLIC:
115      * Set the writer that will receive the
116      * formatted log entries for a file name.
117      */

118     public void setWriter(String JavaDoc aFileName) {
119         if (aFileName != null) {
120             try {
121                 this.writer = new FileWriter(aFileName);
122                 this.fileName = aFileName;
123             } catch (IOException e) {
124                 e.printStackTrace();
125             }
126         }
127     }
128
129     /**
130      * PUBLIC:
131      * For the given writer, return it's associated filename.
132      * If associated writer does not have a filename, return null.
133      */

134     public String JavaDoc getWriterFilename() {
135         return fileName;
136     }
137
138     /**
139      * Append the specified message information to the writer.
140      */

141     protected void writeMessage(String JavaDoc message) throws IOException {
142         this.getWriter().write(message);
143     }
144
145     /**
146      * Append the separator string to the writer.
147      */

148     protected void writeSeparator() throws IOException {
149         this.getWriter().write("--");
150     }
151 }
152
Popular Tags