~ubuntu-branches/ubuntu/wily/libpgjava/wily

« back to all changes in this revision

Viewing changes to org/postgresql/core/Logger.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2008-04-26 22:01:11 UTC
  • mfrom: (3.1.4 gutsy)
  • Revision ID: james.westby@ubuntu.com-20080426220111-yasgxtas5smx2qm3
Tags: 8.2-504-2
* Updated description to mention PostgreSQL 8.3 as supported.
  Closes: #398348
* Removed libpgjava transitional package. Closes: #477557
* Moved debhelper and cdbs from Build-Depends-Indep to Build-Depends.
* Added Homepage, Vcs-Svn and Vcs-Browser fields.
* Added watch file.
* Added myself to Uploaders.
* Removed Stafan and Wolfgang from Uploaders.
* Updated Standards-Version to 3.7.3
* Updated debhelper level to 5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
*
 
3
* Copyright (c) 2005, PostgreSQL Global Development Group
 
4
*
 
5
* IDENTIFICATION
 
6
*   $PostgreSQL: pgjdbc/org/postgresql/core/Logger.java,v 1.2 2005/11/24 06:18:28 oliver Exp $
 
7
*
 
8
*-------------------------------------------------------------------------
 
9
*/
 
10
package org.postgresql.core;
 
11
 
 
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;
 
17
 
 
18
import org.postgresql.Driver;
 
19
 
 
20
/**
 
21
 * Poor man's logging infrastructure. This just deals with maintaining a per-
 
22
 * connection ID and log level, and timestamping output.
 
23
 */
 
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;
 
32
 
 
33
    private int level = 0;
 
34
 
 
35
    public Logger() {
 
36
        connectionIDString = "(driver) ";
 
37
    }
 
38
 
 
39
    public Logger(int connectionID) {
 
40
        connectionIDString = "(" + connectionID + ") ";
 
41
    }
 
42
 
 
43
    public void setLogLevel(int level) {
 
44
        this.level = level;
 
45
    }
 
46
 
 
47
    public int getLogLevel() {
 
48
        return level;
 
49
    }
 
50
 
 
51
    public boolean logDebug() {
 
52
        return level >= Driver.DEBUG;
 
53
    }
 
54
 
 
55
    public boolean logInfo() {
 
56
        return level >= Driver.INFO;
 
57
    }
 
58
 
 
59
    public void debug(String str) {
 
60
        debug(str, null);
 
61
    }
 
62
 
 
63
    public void debug(String str, Throwable t) {
 
64
        if (logDebug())
 
65
            log(str, t);
 
66
    }
 
67
 
 
68
    public void info(String str) {
 
69
        info(str, null);
 
70
    }
 
71
 
 
72
    public void info(String str, Throwable t) {
 
73
        if (logInfo())
 
74
            log(str, t);
 
75
    }
 
76
 
 
77
    public void log(String str, Throwable t) {
 
78
        PrintWriter writer = DriverManager.getLogWriter();
 
79
        if (writer == null)
 
80
            return;
 
81
 
 
82
        synchronized (this) {
 
83
            buffer.setLength(0);
 
84
            dateFormat.format(new Date(), buffer, dummyPosition);
 
85
            buffer.append(connectionIDString);
 
86
            buffer.append(str);
 
87
            
 
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());        
 
92
                if (t != null)
 
93
                    t.printStackTrace(writer);
 
94
            }
 
95
        }
 
96
    }
 
97
}