1 19 20 package org.apache.cayenne.access; 21 22 import java.lang.reflect.Array ; 23 import java.sql.SQLException ; 24 import java.util.List ; 25 26 import org.apache.cayenne.access.jdbc.ParameterBinding; 27 import org.apache.cayenne.conn.DataSourceInfo; 28 import org.apache.cayenne.util.IDUtil; 29 import org.apache.cayenne.util.Util; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 44 public class QueryLogger { 45 46 private static final Log logObj = LogFactory.getLog(QueryLogger.class); 47 48 public static final int TRIM_VALUES_THRESHOLD = 30; 49 50 53 static ThreadLocal logLevel = new ThreadLocal (); 54 55 64 public static void sqlLiteralForObject(StringBuffer buffer, Object object) { 65 if (object == null) { 66 buffer.append("NULL"); 67 } 68 else if (object instanceof String ) { 69 buffer.append('\''); 70 String literal = (String ) object; 72 if (literal.length() > TRIM_VALUES_THRESHOLD) { 73 literal = literal.substring(0, TRIM_VALUES_THRESHOLD) + "..."; 74 } 75 76 int curPos = 0; 77 int endPos = 0; 78 79 while ((endPos = literal.indexOf('\'', curPos)) >= 0) { 80 buffer.append(literal.substring(curPos, endPos + 1)).append('\''); 81 curPos = endPos + 1; 82 } 83 84 if (curPos < literal.length()) 85 buffer.append(literal.substring(curPos)); 86 87 buffer.append('\''); 88 } 89 else if (object instanceof Byte ) { 91 IDUtil.appendFormattedByte(buffer, ((Byte ) object).byteValue()); 92 } 93 else if (object instanceof Number ) { 94 buffer.append(object); 96 } 97 else if (object instanceof java.sql.Date ) { 98 buffer.append('\'').append(object).append('\''); 99 } 100 else if (object instanceof java.sql.Time ) { 101 buffer.append('\'').append(object).append('\''); 102 } 103 else if (object instanceof java.util.Date ) { 104 long time = ((java.util.Date ) object).getTime(); 105 buffer.append('\'').append(new java.sql.Timestamp (time)).append('\''); 106 } 107 else if (object instanceof java.util.Calendar ) { 108 long time = ((java.util.Calendar ) object).getTimeInMillis(); 109 buffer.append(object.getClass().getName()).append('(').append( 110 new java.sql.Timestamp (time)).append(')'); 111 } 112 else if (object instanceof Character ) { 113 buffer.append(((Character ) object).charValue()); 114 } 115 else if (object instanceof Boolean ) { 116 buffer.append('\'').append(object).append('\''); 117 } 118 else if (object instanceof ParameterBinding) { 119 sqlLiteralForObject(buffer, ((ParameterBinding) object).getValue()); 120 } 121 else if (object.getClass().isArray()) { 122 buffer.append("< "); 123 124 int len = Array.getLength(object); 125 boolean trimming = false; 126 if (len > TRIM_VALUES_THRESHOLD) { 127 len = TRIM_VALUES_THRESHOLD; 128 trimming = true; 129 } 130 131 for (int i = 0; i < len; i++) { 132 if (i > 0) { 133 buffer.append(","); 134 } 135 sqlLiteralForObject(buffer, Array.get(object, i)); 136 } 137 138 if (trimming) { 139 buffer.append("..."); 140 } 141 142 buffer.append('>'); 143 } 144 else { 145 buffer.append(object.getClass().getName()).append("@").append( 146 System.identityHashCode(object)); 147 } 148 } 149 150 151 152 155 public static void log(String message) { 156 if (message != null) { 157 logObj.info(message); 158 } 159 } 160 161 166 public static void logConnect(String dataSource) { 167 if (isLoggable()) { 168 logObj.info("Connecting. JNDI path: " + dataSource); 169 } 170 } 171 172 175 public static void logConnect(String url, String userName, String password) { 176 177 if (isLoggable()) { 178 StringBuffer buf = new StringBuffer ("Opening connection: "); 179 180 buf.append(url); 182 buf.append("\n\tLogin: ").append(userName); 183 buf.append("\n\tPassword: *******"); 184 185 logObj.info(buf.toString()); 186 } 187 } 188 189 194 public static void logPoolCreated(DataSourceInfo dsi) { 195 if (isLoggable()) { 196 StringBuffer buf = new StringBuffer ("Created connection pool: "); 197 198 if (dsi != null) { 199 buf.append(dsi.getDataSourceUrl()); 201 202 if (dsi.getAdapterClassName() != null) { 203 buf.append("\n\tCayenne DbAdapter: ").append( 204 dsi.getAdapterClassName()); 205 } 206 207 buf.append("\n\tDriver class: ").append(dsi.getJdbcDriver()); 208 209 if (dsi.getMinConnections() >= 0) { 210 buf.append("\n\tMin. connections in the pool: ").append( 211 dsi.getMinConnections()); 212 } 213 if (dsi.getMaxConnections() >= 0) { 214 buf.append("\n\tMax. connections in the pool: ").append( 215 dsi.getMaxConnections()); 216 } 217 } 218 else { 219 buf.append(" pool information unavailable"); 220 } 221 222 logObj.info(buf.toString()); 223 } 224 } 225 226 229 public static void logConnectSuccess() { 230 logObj.info("+++ Connecting: SUCCESS."); 231 } 232 233 236 public static void logConnectFailure(Throwable th) { 237 logObj.info("*** Connecting: FAILURE.", th); 238 } 239 240 243 public static void logQuery(String queryStr, List params) { 244 logQuery(queryStr, params, -1); 245 } 246 247 255 public static void logQuery(String queryStr, List params, long time) { 256 if (isLoggable()) { 257 StringBuffer buf = new StringBuffer (queryStr); 258 if (params != null && params.size() > 0) { 259 buf.append(" [bind: "); 260 sqlLiteralForObject(buf, params.get(0)); 261 262 int len = params.size(); 263 for (int i = 1; i < len; i++) { 264 buf.append(", "); 265 sqlLiteralForObject(buf, params.get(i)); 266 } 267 268 buf.append(']'); 269 } 270 271 if (time > 5) { 273 buf.append(" - prepared in ").append(time).append(" ms."); 274 } 275 276 logObj.info(buf.toString()); 277 } 278 } 279 280 283 public static void logQueryParameters(String label, List parameters) { 284 285 if (isLoggable() && parameters.size() > 0) { 286 int len = parameters.size(); 287 StringBuffer buf = new StringBuffer ("["); 288 buf.append(label).append(": "); 289 290 sqlLiteralForObject(buf, parameters.get(0)); 291 292 for (int i = 1; i < len; i++) { 293 buf.append(", "); 294 sqlLiteralForObject(buf, parameters.get(i)); 295 } 296 297 buf.append(']'); 298 logObj.info(buf.toString()); 299 } 300 } 301 302 305 public static void logSelectCount(int count) { 306 logSelectCount(count, -1); 307 } 308 309 312 public static void logSelectCount(int count, long time) { 313 if (isLoggable()) { 314 StringBuffer buf = new StringBuffer (); 315 316 if (count == 1) { 317 buf.append("=== returned 1 row."); 318 } 319 else { 320 buf.append("=== returned ").append(count).append(" rows."); 321 } 322 323 if (time >= 0) { 324 buf.append(" - took ").append(time).append(" ms."); 325 } 326 327 logObj.info(buf.toString()); 328 } 329 } 330 331 334 public static void logUpdateCount(int count) { 335 if (isLoggable()) { 336 337 if (count < 0) { 338 logObj.info("=== updated ? rows"); 339 } 340 else { 341 String countStr = (count == 1) ? "=== updated 1 row." : "=== updated " 342 + count 343 + " rows."; 344 logObj.info(countStr); 345 } 346 } 347 } 348 349 352 public static void logBeginTransaction(String transactionLabel) { 353 logObj.info("--- " + transactionLabel); 354 } 355 356 359 public static void logCommitTransaction(String transactionLabel) { 360 logObj.info("+++ " + transactionLabel); 361 } 362 363 366 public static void logRollbackTransaction(String transactionLabel) { 367 logObj.info("*** " + transactionLabel); 368 } 369 370 373 public static void logQueryError(Throwable th) { 374 if (isLoggable()) { 375 if (th != null) { 376 th = Util.unwindException(th); 377 } 378 379 logObj.info("*** error.", th); 380 381 if (th instanceof SQLException ) { 382 SQLException sqlException = ((SQLException ) th).getNextException(); 383 while (sqlException != null) { 384 logObj.info("*** nested SQL error.", sqlException); 385 sqlException = sqlException.getNextException(); 386 } 387 } 388 } 389 } 390 391 394 public static void logQueryStart(int count) { 395 if (isLoggable()) { 396 String countStr = (count == 1) ? "--- will run 1 query." : "--- will run " 397 + count 398 + " queries."; 399 logObj.info(countStr); 400 } 401 } 402 403 409 public static boolean isLoggable() { 410 return logObj.isInfoEnabled(); 411 } 412 } 413 | Popular Tags |