
oracle.toplink.essentials.logging.DefaultSessionLog Maven / Gradle / Ivy
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* glassfish/bootstrap/legal/CDDLv1.0.txt or
* https://glassfish.dev.java.net/public/CDDLv1.0.html.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
// Copyright (c) 1998, 2007, Oracle. All rights reserved.
package oracle.toplink.essentials.logging;
import java.io.*;
import oracle.toplink.essentials.exceptions.*;
import oracle.toplink.essentials.internal.helper.*;
/**
* Purpose: Default log used for the session when message logging is enabled.
* The session can log information such as,
* - all SQL executed
*
- informational messages
*
- debugging information
*
- all exceptions that occur within TopLink
*
* As well information about the message can be logged such as,
* - the session logging the message
*
- the connection executing the SQL
*
- the thread in which the log entry occured
*
- the exact time (to milliseconds) that the log entry occured
*
- the stack trace to the exception
*
* @see SessionLog
* @see Session#logMessage(String)
*
* @author Big Country
*/
public class DefaultSessionLog extends AbstractSessionLog implements Serializable {
/** The filename associated with this DefaultSessionLog, if it is being written out to a file **/
protected String fileName;
/**
* PUBLIC:
* Create a new default session log.
*/
public DefaultSessionLog() {
super();
this.level = INFO;
}
/**
* PUBLIC:
* Create a new default session log for the given writer.
*/
public DefaultSessionLog(Writer writer) {
this();
this.initialize(writer);
}
/**
* Initialize the log.
*/
protected void initialize(Writer writer) {
this.writer = writer;
}
/**
* INTERNAL:
* Log the entry.
* This writes the log entries information to a writer such as System.out or a file.
* This must be synchronized as it will be called by many threads in three-tier.
*/
public synchronized void log(SessionLogEntry entry) {
if (!shouldLog(entry.getLevel())) {
return;
}
try {
printPrefixString(entry.getLevel());
this.getWriter().write(getSupplementDetailString(entry));
if (entry.hasException()) {
if (entry.getLevel() == SEVERE) {
entry.getException().printStackTrace(new PrintWriter(getWriter()));
} else if (entry.getLevel() <= WARNING) {
if (shouldLogExceptionStackTrace()) {
entry.getException().printStackTrace(new PrintWriter(getWriter()));
} else {
writeMessage(entry.getException().toString());
}
}
} else {
writeMessage(formatMessage(entry));
}
getWriter().write(Helper.cr());
getWriter().flush();
} catch (IOException exception) {
throw ValidationException.logIOError(exception);
}
}
/**
* PUBLIC:
* Set the writer that will receive the
* formatted log entries for a file name.
*/
public void setWriter(String aFileName) {
if (aFileName != null) {
try {
this.writer = new FileWriter(aFileName);
this.fileName = aFileName;
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* PUBLIC:
* For the given writer, return it's associated filename.
* If associated writer does not have a filename, return null.
*/
public String getWriterFilename() {
return fileName;
}
/**
* Append the specified message information to the writer.
*/
protected void writeMessage(String message) throws IOException {
this.getWriter().write(message);
}
/**
* Append the separator string to the writer.
*/
protected void writeSeparator() throws IOException {
this.getWriter().write("--");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy