001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.lib.jdbc;
020:
021: import java.sql.Connection;
022:
023: import org.apache.openjpa.lib.log.Log;
024: import org.apache.openjpa.lib.log.NoneLogFactory;
025: import org.apache.openjpa.lib.util.Localizer;
026:
027: /**
028: * Provies basic logging facilities to a DataSource.
029: *
030: * @author Marc Prud'hommeaux
031: * @nojavadoc
032: */
033: public class DataSourceLogs {
034:
035: private static final Localizer _loc = Localizer
036: .forPackage(DataSourceLogs.class);
037:
038: private Log _jdbcLog = null;
039: private Log _sqlLog = null;
040:
041: public DataSourceLogs() {
042: }
043:
044: /**
045: * The log to write JDBC messages to.
046: */
047: public Log getJDBCLog() {
048: return (_jdbcLog == null) ? NoneLogFactory.NoneLog
049: .getInstance() : _jdbcLog;
050: }
051:
052: /**
053: * The log to write JDBC messages to.
054: */
055: public void setJDBCLog(Log log) {
056: _jdbcLog = log;
057: }
058:
059: /**
060: * Return true if JDBC logging is enabled.
061: */
062: public boolean isJDBCEnabled() {
063: return (_jdbcLog != null && _jdbcLog.isTraceEnabled());
064: }
065:
066: /**
067: * The log to write SQL messages to.
068: */
069: public Log getSQLLog() {
070: return (_sqlLog == null) ? NoneLogFactory.NoneLog.getInstance()
071: : _sqlLog;
072: }
073:
074: /**
075: * The log to write SQL messages to.
076: */
077: public void setSQLLog(Log log) {
078: _sqlLog = log;
079: }
080:
081: /**
082: * Return true if SQL logging is enabled.
083: */
084: public boolean isSQLEnabled() {
085: return (_sqlLog != null && _sqlLog.isTraceEnabled());
086: }
087:
088: /**
089: * Log a JDBC message on behalf of the given connection.
090: */
091: public void logJDBC(String msg, Connection conn) {
092: log(msg, conn, _jdbcLog);
093: }
094:
095: /**
096: * Log a JDBC message on behalf of the given connection.
097: */
098: public void logJDBC(String msg, long startTime, Connection conn) {
099: log(msg, conn, _jdbcLog, startTime);
100: }
101:
102: /**
103: * Log a SQL message on behalf of the given connection.
104: */
105: public void logSQL(String msg, Connection conn) {
106: log(msg, conn, _sqlLog);
107: }
108:
109: /**
110: * Log a SQL message on behalf of the given connection.
111: */
112: public void logSQL(String msg, long startTime, Connection conn) {
113: log(msg, conn, _sqlLog, startTime);
114: }
115:
116: /**
117: * Log a message to the given logger.
118: */
119: private static void log(String msg, Connection conn, Log log) {
120: log(msg, conn, log, -1);
121: }
122:
123: /**
124: * Log a message to the given logger.
125: */
126: private static void log(String msg, Connection conn, Log log,
127: long startTime) {
128: if (log == null || !log.isTraceEnabled())
129: return;
130:
131: long totalTime = -1;
132: if (startTime != -1)
133: totalTime = System.currentTimeMillis() - startTime;
134:
135: StringBuffer buf = new StringBuffer(25 + msg.length());
136: buf.append("<t ").append(Thread.currentThread().hashCode());
137: if (conn != null)
138: buf.append(", ").append(conn);
139: buf.append("> ");
140:
141: // if the time != -1, append time profiling information
142: if (totalTime != -1)
143: buf.append("[").append(totalTime).append(" ms] ");
144:
145: buf.append(msg);
146: log.trace(_loc.get("datasource-trace-data", buf.toString()));
147: }
148: }
|