1 64 65 69 70 package com.jcorporate.expresso.core.logging; 71 72 import com.jcorporate.expresso.core.db.DBConnection; 73 import com.jcorporate.expresso.core.db.DBConnectionPool; 74 import com.jcorporate.expresso.core.db.DBException; 75 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 76 import com.jcorporate.expresso.services.dbobj.LogEntry; 77 78 import javax.servlet.ServletContext ; 79 import java.io.ByteArrayOutputStream ; 80 import java.io.IOException ; 81 import java.io.PrintStream ; 82 83 84 96 public class Log { 97 private static ServletContext myContext = null; 98 private static String thisClass = ("com.jcorporate.expresso." + 99 "core.logging.Log."); 100 private static boolean consoleOutput = false; 101 private static int maxLevel = 9; 102 private static DBConnectionPool myPool = null; 103 104 107 public Log() { 108 } 109 110 117 private static boolean checkLog() 118 throws LogException { 119 String myName = (thisClass + "checkLog()"); 120 121 try { 122 if (myPool != null) { 123 return true; 124 } 125 126 myPool = DBConnectionPool.getInstance("default"); 127 setMax(9); 128 129 return true; 130 } catch (DBException de) { 131 throw new LogException(myName + ":Unable to read setup values:" + 132 de.getMessage()); 133 } 134 135 } 136 137 138 145 public static String clear() 146 throws IOException , DBException { 147 String myName = (thisClass + "clear()"); 148 DBConnection myConnection = null; 149 150 try { 151 if (checkLog()) { 152 myConnection = myPool.getConnection(myName); 153 154 LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT); 155 myConnection.executeUpdate("DELETE FROM " + 156 myLog.getJDBCMetaData().getTargetSQLTable(myLog.getDataContext())); 157 } else { 158 throw new DBException(myName + 159 ":Unable to clear log, logging " + 160 "not initialized"); 161 } 162 } catch (LogException le) { 163 throw new DBException(le.getMessage()); 164 } finally { 165 if (myPool != null) { 166 myPool.release(myConnection); 167 } 168 } 169 170 return ("Log Cleared"); 171 } 172 173 174 180 private static String encode(String color) { 181 if (color.equalsIgnoreCase("RED")) { 182 return ("R"); 183 } else if (color.equalsIgnoreCase("GREEN")) { 184 return ("G"); 185 } else if (color.equalsIgnoreCase("BLUE")) { 186 return ("B"); 187 } else if (color.equalsIgnoreCase("YELLOW")) { 188 return ("Y"); 189 } else { 190 return (""); 191 } 192 } 193 194 200 public static void log(int newLevel, String msg) 201 throws LogException { 202 String myName = (thisClass + "log(int, String)"); 203 204 if (newLevel <= maxLevel) { 205 if (consoleOutput) { 206 System.err.println(msg); 207 } 208 209 DBConnection myConnection = null; 210 211 if (checkLog()) { 212 try { 213 myConnection = myPool.getConnection(myName); 214 215 LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT); 216 myLog.setField("MessageText", msg); 217 myLog.setField("MessageLevel", "" + newLevel); 218 myLog.add(); 219 } catch (DBException de) { 220 System.err.println(myName + ":Unable to log '" + msg + 221 "'"); 222 de.printStackTrace(System.err); 223 } finally { 224 if (myPool != null) { 225 myPool.release(myConnection); 226 } 227 } 228 } 229 230 if (myContext != null) { 231 myContext.log(msg); 232 } 233 } 234 235 } 236 237 238 246 public static void log(int newLevel, String objectName, String msg) 247 throws LogException { 248 String myName = (thisClass + "log(int, String, String)"); 249 250 if (newLevel <= maxLevel) { 251 if (consoleOutput) { 252 System.err.println(objectName + ":" + msg); 253 } 254 255 DBConnection myConnection = null; 256 257 if (checkLog()) { 258 try { 259 myConnection = myPool.getConnection(myName); 260 261 LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT); 262 myLog.setField("ObjectName", objectName); 263 myLog.setField("MessageText", msg); 264 myLog.setField("MessageLevel", "" + newLevel); 265 myLog.add(); 266 } catch (DBException de) { 267 throw new LogException(myName + ":" + de.getMessage()); 268 } finally { 269 if (myPool != null) { 270 myPool.release(myConnection); 271 } 272 } 273 } 274 if (myContext != null) { 275 myContext.log(objectName + "|" + msg); 276 } 277 } 278 279 } 280 281 282 292 public static void log(int newLevel, String objectName, String msg, 293 String color) 294 throws LogException { 295 String myName = (thisClass + "log(int, String, String, " + 296 "String)"); 297 298 if (newLevel <= maxLevel) { 299 if (consoleOutput) { 300 System.err.println(objectName + ":" + msg); 301 } 302 303 DBConnection myConnection = null; 304 305 if (checkLog()) { 306 try { 307 myConnection = myPool.getConnection(myName); 308 309 LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT); 310 myLog.setField("ObjectName", objectName); 311 myLog.setField("MessageText", msg); 312 myLog.setField("MessageColor", encode(color)); 313 myLog.setField("MessageLevel", "" + newLevel); 314 myLog.add(); 315 } catch (DBException de) { 316 System.err.println(myName + ":Unable to log '" + msg + 317 "'"); 318 de.printStackTrace(System.err); 319 } finally { 320 if (myPool != null) { 321 myPool.release(myConnection); 322 } 323 } 324 } 325 if (myContext != null) { 326 myContext.log(objectName + ":" + msg); 327 } 328 } 329 330 } 331 332 333 343 public static void log(int newLevel, String objectName, String msg, 344 String color, String uid, String jobNumber) 345 throws LogException { 346 String myName = (thisClass + "log(int, String, String, " + 347 "String, String, String)"); 348 349 if (newLevel <= maxLevel) { 350 if (consoleOutput) { 351 System.err.println(objectName + ":" + msg + ":" + uid + 352 ":" + jobNumber); 353 } 354 355 DBConnection myConnection = null; 356 357 if (checkLog()) { 358 try { 359 myConnection = myPool.getConnection(myName); 360 361 LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT); 362 myLog.setField("ObjectName", objectName); 363 myLog.setField("MessageText", msg); 364 myLog.setField("MessageColor", encode(color)); 365 myLog.setField("ExpUid", uid); 366 myLog.setField("JobNumber", jobNumber); 367 myLog.setField("MessageLevel", "" + newLevel); 368 myLog.add(); 369 } catch (DBException de) { 370 System.err.println(myName + ":Unable to log '" + msg + 371 "'"); 372 de.printStackTrace(System.err); 373 } finally { 374 if (myPool != null) { 375 myPool.release(myConnection); 376 } 377 } 378 } 379 if (myContext != null) { 380 myContext.log(objectName + ":" + msg + ":" + uid + ":" + 381 jobNumber); 382 } 383 } 384 385 } 386 387 388 394 public static void log(Exception e) 395 throws LogException { 396 String myName = (thisClass + "log(Exception)"); 397 String message = e.getMessage(); 398 399 if (e instanceof DBException) { 400 DBException de = (DBException) e; 401 message = de.getMessage() + ":" + de.getDBMessage(); 402 } 403 if (consoleOutput) { 404 System.err.println(message); 405 e.printStackTrace(); 406 } 407 408 DBConnection myConnection = null; 409 410 if (checkLog()) { 411 try { 412 myConnection = myPool.getConnection(myName); 413 414 LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT); 415 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 416 e.printStackTrace(new PrintStream (bos)); 417 myLog.setField("MessageText", bos.toString()); 418 myLog.setField("MessageColor", encode("RED")); 419 myLog.add(); 420 } catch (DBException de) { 421 System.err.println(myName + ":Unable to log exception"); 422 e.printStackTrace(System.err); 423 de.printStackTrace(System.err); 424 } finally { 425 if (myPool != null) { 426 myPool.release(myConnection); 427 } 428 } 429 } 430 if (myContext != null) { 431 myContext.log("unknown", e); 432 } 433 } 434 435 436 441 public static void log(String msg) 442 throws LogException { 443 log(0, msg); 444 } 445 446 447 453 public static void log(String objectName, Exception e) 454 throws LogException { 455 String myName = (thisClass + "log(String, Exception)"); 456 String message = e.getMessage(); 457 458 if (e instanceof DBException) { 459 DBException de = (DBException) e; 460 message = de.getMessage() + ":" + de.getDBMessage(); 461 } 462 if (consoleOutput) { 463 System.err.println(objectName + ":" + message); 464 e.printStackTrace(System.err); 465 } 466 467 DBConnection myConnection = null; 468 469 if (checkLog()) { 470 try { 471 myConnection = myPool.getConnection(myName); 472 473 LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT); 474 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 475 e.printStackTrace(new PrintStream (bos)); 476 myLog.setField("MessageText", bos.toString()); 477 myLog.setField("MessageColor", encode("RED")); 478 myLog.setField("ObjectName", objectName); 479 myLog.add(); 480 } catch (DBException de) { 481 throw new LogException(myName + ":" + de.getMessage()); 482 } finally { 483 if (myPool != null) { 484 myPool.release(myConnection); 485 } 486 } 487 } 488 if (myContext != null) { 489 myContext.log(objectName, e); 490 } 491 } 492 493 494 501 public static void log(String objectName, String msg) 502 throws LogException { 503 log(0, objectName, msg); 504 } 505 506 507 515 public static void log(String objectName, String msg, String color) 516 throws LogException { 517 log(0, objectName, msg, color); 518 } 519 520 521 530 public static void log(String objectName, String msg, String color, 531 String userName, String jobNumber) 532 throws LogException { 533 log(0, objectName, msg, color, userName, jobNumber); 534 } 535 536 537 543 public static void setConsoleOutput(boolean newFlag) { 544 consoleOutput = newFlag; 545 } 546 547 552 public static void setContext(ServletContext newContext) 553 throws LogException { 554 String myName = (thisClass + "setContext(ServletContext)"); 555 556 if (newContext == null) { 557 log(myName, "Null context", "RED"); 558 559 return; 560 } 561 if (myContext == null) { 562 myContext = newContext; 563 } 564 } 565 566 567 573 public static void setMax(int newMax) { 574 if (newMax < 0) { 575 maxLevel = 0; 576 } else if (newMax > 9) { 577 maxLevel = 9; 578 } else { 579 maxLevel = newMax; 580 } 581 } 582 583 592 public static void tryLog(int newLevel, String objectName, String msg, 593 String color) { 594 String myName = (thisClass + "tryLog(int, String, String, " + 595 "String)"); 596 597 try { 598 log(newLevel, objectName, msg, color); 599 } catch (LogException he) { 600 System.err.println(myName + ":Unable to log message '" + msg + 601 "' from object '" + objectName + "':" + 602 he.getMessage()); 603 he.printStackTrace(); 604 } 605 } 606 607 614 public static void tryLog(String objectName, Exception e) { 615 String myName = (thisClass + "tryLog(String, Exception)"); 616 String message = e.getMessage(); 617 618 if (e instanceof DBException) { 619 DBException de = (DBException) e; 620 message = de.getMessage() + ":" + de.getDBMessage(); 621 } 622 try { 623 log(objectName, e); 624 } catch (LogException he) { 625 System.err.println(myName + ":Unable to log exception '" + 626 message + "' from object '" + objectName + 627 "':" + he.getMessage()); 628 he.printStackTrace(System.err); 629 e.printStackTrace(System.err); 630 } 631 } 632 633 640 public static void tryLog(String objectName, String msg) { 641 String myName = (thisClass + "tryLog(String, String)"); 642 643 try { 644 log(objectName, msg); 645 } catch (LogException he) { 646 System.err.println(myName + ":Unable to log message '" + msg + 647 "' from object '" + objectName + "':" + 648 he.getMessage()); 649 he.printStackTrace(); 650 } 651 } 652 653 661 public static void tryLog(String objectName, String msg, String color) { 662 String myName = (thisClass + "tryLog(String, String, " + 663 "String)"); 664 665 try { 666 log(objectName, msg, color); 667 } catch (LogException he) { 668 System.err.println(myName + ":Unable to log message '" + msg + 669 "' from object '" + objectName + "':" + 670 he.getMessage()); 671 he.printStackTrace(); 672 } 673 } 674 675 685 public static void tryLog(String objectName, String msg, String color, 686 String userName, String jobNumber) { 687 String myName = (thisClass + "tryLog(String, String, " + 688 "String, String, String)"); 689 690 try { 691 log(objectName, msg, color, userName, jobNumber); 692 } catch (LogException he) { 693 System.err.println(myName + ":Unable to log message '" + msg + 694 "' from object '" + objectName + "':" + 695 he.getMessage()); 696 he.printStackTrace(); 697 } 698 } 699 700 } 701 702 | Popular Tags |