1
/*-------------------------------------------------------------------------
3
* Copyright (c) 2005, PostgreSQL Global Development Group
6
* $PostgreSQL: pgjdbc/org/postgresql/core/Logger.java,v 1.2 2005/11/24 06:18:28 oliver Exp $
8
*-------------------------------------------------------------------------
10
package org.postgresql.core;
12
import java.text.SimpleDateFormat;
13
import java.text.FieldPosition;
14
import java.sql.DriverManager;
15
import java.io.PrintWriter;
16
import java.util.Date;
18
import org.postgresql.Driver;
21
* Poor man's logging infrastructure. This just deals with maintaining a per-
22
* connection ID and log level, and timestamping output.
24
public final class Logger {
25
// For brevity we only log the time, not date or timezone (the main reason
26
// for the timestamp is to see delays etc. between log lines, not to pin
27
// down an instant in time)
28
private final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS ");
29
private final FieldPosition dummyPosition = new FieldPosition(0);
30
private final StringBuffer buffer = new StringBuffer();
31
private final String connectionIDString;
33
private int level = 0;
36
connectionIDString = "(driver) ";
39
public Logger(int connectionID) {
40
connectionIDString = "(" + connectionID + ") ";
43
public void setLogLevel(int level) {
47
public int getLogLevel() {
51
public boolean logDebug() {
52
return level >= Driver.DEBUG;
55
public boolean logInfo() {
56
return level >= Driver.INFO;
59
public void debug(String str) {
63
public void debug(String str, Throwable t) {
68
public void info(String str) {
72
public void info(String str, Throwable t) {
77
public void log(String str, Throwable t) {
78
PrintWriter writer = DriverManager.getLogWriter();
84
dateFormat.format(new Date(), buffer, dummyPosition);
85
buffer.append(connectionIDString);
88
// synchronize to ensure that the exception (if any) does
89
// not get split up from the corresponding log message
90
synchronized (writer) {
91
writer.println(buffer.toString());
93
t.printStackTrace(writer);