1 14 package org.compiere.util; 15 16 import java.sql.*; 17 import java.util.*; 18 import java.io.*; 19 import java.net.*; 20 import java.awt.*; 21 import java.text.*; 22 23 import org.compiere.*; 24 import org.compiere.db.*; 25 26 33 public final class Log implements Serializable 34 { 35 36 private static final int l0_Error = 0; 37 38 public static final int l1_User = 1; 39 40 public static final int l2_Sub = 2; 41 42 public static final int l3_Util = 3; 43 44 public static final int l4_Data = 4; 45 46 public static final int l5_DData = 5; 47 48 public static final int l6_Database = 6; 49 50 51 private static int s_traceLevel = 0; 52 53 private static PrintWriter s_out = null; 54 55 private static boolean s_console = false; 56 57 58 private static Logger s_log = Logger.getCLogger(Log.class); 59 60 61 66 public static void initLog () 67 { 68 initLog(false, -1); 69 } 71 78 public static void initLog (boolean writeToTempLogFile, int traceLevel) 79 { 80 int level = 0; 83 try 84 { 85 if (traceLevel < 0) 86 level = Integer.parseInt(Ini.getProperty(Ini.P_DEBUGLEVEL)); 87 else 88 level = traceLevel; 89 if (level < 0) 90 level = 0; 91 } 92 catch (Exception e) 93 { 94 System.err.println("Log.initLog - Cannot parse Trace level - Set to 9"); 95 level = 9; 96 } 97 setTraceLevel(level); 98 99 if (Ini.isClient()) 101 setOutput(writeToTempLogFile); 102 103 if (level < l6_Database) 105 return; 106 108 if (level < 10) 110 return; 111 printProperties (System.getProperties(), "System", writeToTempLogFile); 112 } 114 121 public static void printProperties (Properties p, String description, boolean logLevel10) 122 { 123 if (p == null) 124 return; 125 if (logLevel10) 126 Log.trace(9, "Log.printProperties = " + description, "Size=" + p.size() 127 + ", Hash=" + p.hashCode() + "\n" + getLocalHost()); 128 else 129 System.out.println("Log.printProperties = " + description + ", Size=" + p.size() 130 + ", Hash=" + p.hashCode() + "\n" + getLocalHost()); 131 132 Object [] pp = p.keySet().toArray(); 133 Arrays.sort(pp); 134 for (int i = 0; i < pp.length; i++) 135 { 136 String key = pp[i].toString(); 137 String value = p.getProperty(key); 138 if (logLevel10) 139 Log.trace(10, key, value); 140 else 141 System.out.println(" " + key + " = " + value); 142 } 143 } 145 151 public static StringBuffer getInfo (StringBuffer sb, Properties ctx) 152 { 153 if (sb == null) 154 sb = new StringBuffer (); 155 if (ctx == null) 156 ctx = Env.getCtx(); 157 CConnection cc = CConnection.get(); 159 sb.append("\n\n===Environment===").append(Compiere.getCheckSum()) 160 .append(Compiere.getSummaryAscii()) 161 .append("\n").append(getLocalHost()) 162 .append("\n").append(cc.toStringLong()) 163 .append("\n").append(cc.getInfo()); 164 sb.append("\n\n===Context==="); 166 String [] context = Env.getEntireContext(ctx); 167 Arrays.sort(context); 168 for (int i = 0; i < context.length; i++) 169 sb.append("\n").append(context[i]); 170 sb.append("\n\n===System==="); 172 Object [] pp = System.getProperties().keySet().toArray(); 173 Arrays.sort(pp); 174 for (int i = 0; i < pp.length; i++) 175 { 176 String key = pp[i].toString(); 177 String value = System.getProperty(key); 178 sb.append("\n").append(key).append("=").append(value); 179 } 180 return sb; 181 } 183 187 private static String getLocalHost() 188 { 189 try 190 { 191 InetAddress id = InetAddress.getLocalHost(); 192 return id.toString(); 193 } 194 catch (Exception e) 195 { 196 error("Log.getLocalHost", e); 197 } 198 return "-no local host info -"; 199 } 201 202 203 210 public static void trace (int level, String message, Object addlInfo) 211 { 212 if (level < 0 || level > s_traceLevel) 213 return; 214 StringBuffer sb = new StringBuffer (message); 215 sb.append(" - "); 216 if (addlInfo == null) 217 sb.append("null"); 218 else 219 sb.append(addlInfo); 220 trace (level, sb.toString()); 221 } 223 230 public static void trace (int level, String message, Object [] parameter) 231 { 232 if (level < 0 || level > s_traceLevel || message == null) 233 return; 234 String msg = null; 236 try 237 { 238 msg = MessageFormat.format(message, parameter); 239 } 240 catch (Exception e) 241 { 242 System.err.println("Log.trace - invalid message - " + e.getMessage()); 243 msg = message; 244 } 245 trace(level, msg); 246 } 248 254 public static void trace (int level, Object msg) 255 { 256 if (level < 0 || level > s_traceLevel || msg == null) 257 return; 258 if (!Ini.isClient()) 259 { 260 if (level == l0_Error) 261 s_log.error(msg); 262 else if (level == l1_User || level == l2_Sub) 263 s_log.info(msg); 264 else 265 s_log.debug(msg); 266 return; 267 } 268 269 String message = msg.toString(); 270 if (message.length() == 0) 271 return; 272 Timestamp ts = new Timestamp(System.currentTimeMillis()); 274 String tstr = ts.toString() + "00"; 275 StringBuffer sb = new StringBuffer (); 276 if (level == 0) 278 { sb.append("===========> ").append(message); 280 if (Ini.isClient()) 281 Toolkit.getDefaultToolkit().beep(); 282 } 283 else 285 { 286 sb.append(tstr.substring(11, 23)); 287 sb.append(" ".substring(0, level << 1)).append(message); 289 } 290 291 if (s_out == null) 293 setOutput(false); 294 295 s_out.println(sb.toString()); 297 298 if (level == l0_Error) 300 { 301 String errMsg = ts.toString() + " " + message; 302 if (!s_console) 303 System.err.println (sb.toString()); 304 writeDBLog(".", errMsg); 306 if (Ini.isClient()) 308 Toolkit.getDefaultToolkit().beep(); 309 } 310 } 312 318 public static void print (String message) 319 { 320 if (s_out != null) 321 s_out.println(message); 322 } 324 330 public static boolean error (String description) 331 { 332 if (Ini.isClient()) 333 trace (l0_Error, description); 334 else 335 s_log.error(description); 336 return true; 337 } 339 346 public static boolean error (String description, Exception e) 347 { 348 if (!Ini.isClient()) 349 { 350 s_log.error (description, e); 351 return true; 352 } 353 354 StringBuffer output = new StringBuffer (); 355 output.append(description).append(" - "); 356 output.append(e.getClass().getName()).append(": "); 357 if (e.getMessage() == null) 358 output.append(e.toString()); 359 else 360 output.append(e.getMessage()); 361 if (!(e instanceof SQLException)) 362 { 363 StackTraceElement [] elements = e.getStackTrace(); 364 for (int i = 0; i < 3 || i < elements.length; i++) 365 output.append(" > ").append(elements[i].toString()); 366 } 367 trace (l0_Error, output.toString()); 368 e.printStackTrace(s_out); 369 return true; 370 } 372 379 public static boolean error(String description, SQLException sqlEx) 380 { 381 if (description == null || sqlEx == null || description.length() == 0) 382 throw new IllegalArgumentException ("DB.printException - required parameter missing"); 383 if (!Ini.isClient()) 384 { 385 s_log.error(description, sqlEx); 386 return true; 387 } 388 error("SQL Exception: " + description); if (sqlEx == null) 390 return true; 391 SQLException ex = sqlEx; 393 while (ex != null) 394 { 395 StringBuffer buffer = new StringBuffer (); 396 buffer.append(ex.getMessage()); 397 buffer.append("; State=").append(ex.getSQLState()).append("; ErrorCode=").append(ex.getErrorCode()); 398 error(buffer.toString()); ex = ex.getNextException(); 400 } 401 return true; 402 } 404 405 406 411 public static void traceJDBC (boolean traceDB) 412 { 413 System.out.println("Log.traceJDBC = " + traceDB); 414 if (traceDB && getTraceLevel() >= l6_Database) 415 DriverManager.setLogWriter(s_out); else 417 DriverManager.setLogWriter(null); 418 } 420 425 public static void setTraceLevel(int traceLevel) 426 { 427 if (traceLevel == s_traceLevel) 428 return; 429 s_traceLevel = traceLevel; 430 Ini.setProperty(Ini.P_DEBUGLEVEL, s_traceLevel); 431 if (Ini.isClient()) 433 { 434 if (traceLevel == l0_Error) 435 Logger.getRootLogger().setLevel(org.apache.log4j.Level.ERROR); 436 else if (traceLevel == l1_User || traceLevel == l3_Util) 437 Logger.getRootLogger().setLevel(org.apache.log4j.Level.INFO); 438 else 439 Logger.getRootLogger().setLevel(org.apache.log4j.Level.ALL); 440 } 441 s_log.info("setTraceLevel - " + traceLevel); 442 } 444 448 public static int getTraceLevel() 449 { 450 return s_traceLevel; 451 } 453 458 public static boolean isTraceLevel(int level) 459 { 460 return level <= s_traceLevel; 461 } 463 464 465 469 public static void setOutput(PrintWriter out) 470 { 471 if (out == null) 472 throw new IllegalArgumentException ("Log.setOutput - PrintWriter cannot be null"); 473 474 if (s_out != null) 476 { 477 s_out.flush(); 478 s_out.close(); 479 } 480 s_out = out; 482 s_console = false; 483 if (getTraceLevel() > 0) 484 System.out.println("Log.setOutput - PW=" + s_out.toString()); 485 } 487 491 public static void setOutput(String filename) 492 { 493 if (filename == null || filename.length() == 0) 494 throw new IllegalArgumentException ("Log.setOutput - Invalid filename"); 495 496 if (s_out != null) 498 { 499 s_out.flush(); 500 s_out.close(); 501 } 502 try 504 { 505 Time t = new Time(System.currentTimeMillis()); 506 String logName = filename + t; 507 FileWriter fw = new FileWriter(logName); 508 s_out = new PrintWriter(fw, true); 509 s_console = false; 510 if (getTraceLevel() > 0) 511 System.out.println("Log.setOutput - File=" + logName); 512 } 513 catch (Exception e) 514 { 515 System.err.println("========> Log.setOutput - " + e.getMessage()); 516 s_out = new PrintWriter(System.out, true); 517 s_console = true; 518 if (getTraceLevel() > 0) 519 System.out.println("Log.setOutput - C=" + s_out.toString()); 520 } 521 } 523 527 public static void setOutput (boolean writeToTempLogFile) 528 { 529 if (s_out != null) 531 { 532 s_out.flush(); 533 s_out.close(); 534 } 535 536 try 537 { 538 if (writeToTempLogFile) 539 { 540 String dirName = Ini.getProperty(Ini.P_TEMP_DIR); 541 File tempDir = new File(dirName); 542 File tempFile = File.createTempFile("Compiere", ".log", tempDir); 543 s_out = new PrintWriter(new FileOutputStream(tempFile), true); s_console = false; 545 if (getTraceLevel() > 0) 546 System.out.println("Log.setOutput - File=" + tempFile.getAbsolutePath()); 547 } 548 else 549 { 550 s_out = new PrintWriter(System.out, true); 551 s_console = true; 552 } 555 } 556 catch (IOException e) 557 { 558 System.err.println("========> Log.setOutput - " + e.getMessage()); 559 s_out = new PrintWriter(System.out, true); 560 s_console = true; 561 } 563 } 565 566 567 568 569 private static ValueNamePair s_lastError = null; 570 571 577 public static boolean saveError (String AD_Message, String message) 578 { 579 return saveError (AD_Message, message, true); 580 } 582 583 590 public static boolean saveError (String AD_Message, String message, boolean issueError) 591 { 592 s_lastError = new ValueNamePair (AD_Message, message); 593 if (issueError) 595 error(AD_Message + " - " + message); 596 return true; 597 } 599 603 public static ValueNamePair retrieveError() 604 { 605 ValueNamePair vp = s_lastError; 606 s_lastError = null; 607 return vp; 608 } 610 611 612 613 618 protected static void writeDBLog (String summary, String msg) 619 { 620 if (msg == null || msg.length() == 0) 622 return; 623 if (!DB.isConnected()) 625 return; 626 627 if (msg.indexOf("AD_Error") != -1) 629 return; 630 Properties ctx = Env.getCtx(); 632 StringBuffer sql = new StringBuffer (); 634 try 635 { 636 sql.append("INSERT INTO AD_Error " 637 + "(AD_Error_ID, AD_Client_ID, AD_Org_ID, CreatedBy, UpdatedBy," 638 + "Name, Code, AD_Language) VALUES ("); 639 sql.append("AD_Error_Seq.nextval,"); sql.append(Env.getContextAsInt(ctx, 0, "AD_Client_ID")).append(","); sql.append(Env.getContextAsInt(ctx, 0, "AD_Org_ID")).append(","); sql.append(Env.getContextAsInt(ctx, "#AD_User_ID")).append(","); sql.append(Env.getContextAsInt(ctx, "#AD_User_ID")).append(","); sql.append(DB.TO_STRING(summary)).append(","); sql.append(DB.TO_STRING(msg, 2000)).append(","); sql.append("'").append(Env.getAD_Language(ctx)).append("')"); 648 String sqlExec = DB.getDatabase().convertStatement(sql.toString()); 649 Connection con = DB.getConnectionRW(); 651 if (con == null) 652 { 653 System.err.println("===========> Log.writeDB - No Connection - Error not logged in DB -"); 654 System.err.println(msg); 655 return; 656 } 657 Statement stmt = con.createStatement(); 658 stmt.executeUpdate(sqlExec); 659 stmt.close(); } 661 catch (Exception e) 662 { 663 System.err.println("===========> Log.writeDB - Message=" + msg + " - Error: " + e); 664 } 665 } 667 } 669 | Popular Tags |