1 64 65 70 package com.jcorporate.expresso.core.logging; 71 72 import com.jcorporate.expresso.core.db.DBException; 73 import com.jcorporate.expresso.core.registry.ExpressoThread; 74 import com.jcorporate.expresso.services.dbobj.LogEntry; 75 import org.apache.log4j.Logger; 76 77 import java.io.ByteArrayOutputStream ; 78 import java.io.PrintStream ; 79 import java.util.Vector ; 80 81 82 90 public class LogHandler 91 extends ExpressoThread { 92 private static Vector queue = new Vector (3); 93 private static boolean trace = false; 94 private static LogHandler myInstance = null; 95 private static int maxLevel = 9; 96 private static boolean daemonMode = false; 97 private static Logger log = Logger.getLogger(LogHandler.class); 98 99 103 private static int idleTimes = 0; 104 private static int maxIdleTimes = 10; 105 106 110 private static int maxWrites = 5; 111 112 115 private int sleepTime = 30; 116 private static String thisClass = LogHandler.class.getName(); 117 118 121 public LogHandler() { 122 123 132 } 133 134 137 public synchronized void addToQueue(LogEntry le) { 138 queue.addElement(le); 139 } 140 141 147 public static synchronized void staticAddToQueue(LogEntry le) { 148 startUp(); 149 150 if (myInstance != null) { 151 queue.addElement(le); 152 } 153 } 154 155 156 159 private synchronized void checkQueue() 160 throws LogException { 161 String myName = (thisClass + "checkQueue()"); 162 163 if (queue.size() == 0) { 164 idleTimes++; 165 166 if (trace) { 167 System.err.println(myName + ":Queue empty. Idle " + idleTimes + 168 " times"); 169 } 170 171 return; 172 } 173 if (trace) { 174 System.err.println(myName + ":Checking queue. " + queue.size() + 175 " entries"); 176 } 177 try { 178 LogEntry oneLogEntry = null; 179 int writeCount = 0; 180 181 while (queue.size() > 0) { 182 oneLogEntry = (LogEntry) queue.firstElement(); 183 queue.removeElementAt(0); 184 oneLogEntry.add(); 185 writeCount++; 186 187 if (writeCount > maxWrites) { 188 break; 189 } 190 } 191 192 } catch (Exception de) { 193 de.printStackTrace(System.err); 194 throw new LogException(myName + 195 ":Database error storing log queue", de); 196 } 197 } 198 199 200 206 private static String encode(String color) { 207 if (color.equalsIgnoreCase("RED")) { 208 return ("R"); 209 } else if (color.equalsIgnoreCase("GREEN")) { 210 return ("G"); 211 } else if (color.equalsIgnoreCase("BLUE")) { 212 return ("B"); 213 } else if (color.equalsIgnoreCase("YELLOW")) { 214 return ("Y"); 215 } else { 216 return (""); 217 } 218 } 219 220 224 public void finalize() { 225 try { 226 flush(); 227 } catch (LogException e) { 228 } 229 } 230 231 234 public static void flush() 235 throws LogException { 236 startUp(); 237 myInstance.checkQueue(); 238 } 239 240 241 247 public static void log(int newLevel, String msg) { 248 String myName = (thisClass + "log(int, String)"); 249 250 if (newLevel <= maxLevel) { 251 if (trace) { 252 System.err.println(msg); 253 } 254 255 startUp(); 256 257 try { 258 LogEntry myLog = new LogEntry(); 259 myLog.setField("MessageText", msg); 260 myLog.setField("MessageLevel", "" + newLevel); 261 myLog.setTimeStamp(); 262 myInstance.addToQueue(myLog); 263 } catch (DBException de) { 264 System.err.println(myName + ":Unable to log '" + msg + "'"); 265 de.printStackTrace(System.err); 266 } 267 } 268 269 } 270 271 272 280 public static void log(int newLevel, String objectName, String msg) 281 throws LogException { 282 String myName = (thisClass + "log(int, String, String)"); 283 284 if (newLevel <= maxLevel) { 285 if (trace) { 286 System.err.println(objectName + ":" + msg); 287 } 288 289 startUp(); 290 291 try { 292 LogEntry myLog = new LogEntry(); 293 myLog.setField("ObjectName", objectName); 294 myLog.setField("MessageText", msg); 295 myLog.setField("MessageLevel", "" + newLevel); 296 myLog.setTimeStamp(); 297 myInstance.addToQueue(myLog); 298 } catch (DBException de) { 299 throw new LogException(myName + ":" + de.getMessage()); 300 } 301 } 302 303 } 304 305 306 315 public static void log(int newLevel, String objectName, String msg, 316 String color) { 317 String myName = (thisClass + "log(int, String, String, " + 318 "String)"); 319 320 if (newLevel <= maxLevel) { 321 if (trace) { 322 System.err.println(objectName + ":" + msg); 323 } 324 325 startUp(); 326 327 try { 328 LogEntry myLog = new LogEntry(); 329 myLog.setField("ObjectName", objectName); 330 myLog.setField("MessageText", msg); 331 myLog.setField("MessageColor", encode(color)); 332 myLog.setField("MessageLevel", "" + newLevel); 333 myLog.setTimeStamp(); 334 myInstance.addToQueue(myLog); 335 } catch (DBException de) { 336 System.err.println(myName + ":Unable to log '" + msg + "'"); 337 de.printStackTrace(System.err); 338 } 339 } 340 341 } 342 343 344 354 public static void log(int newLevel, String objectName, String msg, 355 String color, String uid, String jobNumber) { 356 log("default", "", newLevel, objectName, msg, color, uid, jobNumber); 357 } 358 359 360 366 public static void log(Throwable e) 367 throws LogException { 368 log("default", "", e); 369 } 370 371 372 377 public static void log(String msg) { 378 log(0, msg); 379 } 380 381 382 388 public static void log(String objectName, Throwable e) 389 throws LogException { 390 log("default", objectName, e); 391 } 392 393 394 401 public static void log(String objectName, String msg) 402 throws LogException { 403 log(0, objectName, msg); 404 } 405 406 407 419 public static void log(String dbName, String channelName, int level, 420 String objectName, Throwable e, String color, 421 String uid, String jobNumber) 422 throws LogException { 423 String myName = (thisClass + 424 "log(String, String, int, String, Exception," + 425 "String, String, String)"); 426 String message = e.getMessage(); 427 428 if (e instanceof DBException) { 429 DBException de = (DBException) e; 430 message = de.getMessage() + ":" + de.getDBMessage(); 431 } 432 if (trace) { 433 System.err.println(objectName + ":" + message); 434 e.printStackTrace(System.err); 435 } 436 437 startUp(); 438 439 try { 440 LogEntry myLog = new LogEntry(); 441 myLog.setDataContext(dbName); 442 443 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 444 e.printStackTrace(new PrintStream (bos)); 445 myLog.setField("MessageText", bos.toString()); 446 myLog.setField("MessageColor", color); 447 myLog.setField("ObjectName", objectName); 448 myLog.setField("LogChannel", channelName); 449 myLog.setField("ExpUid", uid); 450 myLog.setField("JobNumber", jobNumber); 451 myLog.setTimeStamp(); 452 myInstance.addToQueue(myLog); 453 } catch (DBException de) { 454 throw new LogException(myName + ":" + de.getMessage()); 455 } 456 } 457 458 459 471 public static void log(String dbName, String channelName, int newLevel, 472 String objectName, String msg, String color, 473 String uid, String jobNumber) { 474 String myName = (thisClass + "log(String, String, int, " + 475 "String, String, String, String, String)"); 476 477 if (newLevel <= maxLevel) { 478 if (trace) { 479 System.err.println(objectName + ":" + msg + ":" + uid + ":" + 480 jobNumber); 481 } 482 483 startUp(); 484 485 try { 486 LogEntry myLog = new LogEntry(); 487 myLog.setDataContext(dbName); 488 myLog.setField("ObjectName", objectName); 489 myLog.setField("MessageText", msg); 490 myLog.setField("MessageColor", encode(color)); 491 myLog.setField("ExpUid", uid); 492 myLog.setField("JobNumber", jobNumber); 493 myLog.setField("MessageLevel", "" + newLevel); 494 myLog.setField("LogChannel", channelName); 495 myLog.setTimeStamp(); 496 myInstance.addToQueue(myLog); 497 } catch (DBException de) { 498 System.err.println(myName + ":Unable to log '" + msg + "'"); 499 de.printStackTrace(System.err); 500 } 501 } 502 503 } 504 505 506 513 public static void log(String dbName, String objectName, Throwable e) 514 throws LogException { 515 String myName = (thisClass + "log(String, Exception)"); 516 String message = e.getMessage(); 517 518 if (e instanceof DBException) { 519 DBException de = (DBException) e; 520 message = de.getMessage() + ":" + de.getDBMessage(); 521 } 522 if (trace) { 523 System.err.println(objectName + ":" + message); 524 e.printStackTrace(System.err); 525 } 526 527 startUp(); 528 529 try { 530 LogEntry myLog = new LogEntry(); 531 myLog.setDataContext(dbName); 532 533 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 534 e.printStackTrace(new PrintStream (bos)); 535 myLog.setField("MessageText", bos.toString()); 536 myLog.setField("MessageColor", encode("RED")); 537 myLog.setField("ObjectName", objectName); 538 myLog.setTimeStamp(); 539 myInstance.addToQueue(myLog); 540 } catch (DBException de) { 541 throw new LogException(myName + ":" + de.getMessage()); 542 } 543 } 544 545 546 553 public static void log(String objectName, String msg, String color) { 554 log(0, objectName, msg, color); 555 } 556 557 558 567 public static void log(String objectName, String msg, String color, 568 String uid, String jobNumber) { 569 log(0, objectName, msg, color, uid, jobNumber); 570 } 571 572 573 576 public void run() { 577 super.run(); 578 String myName = (thisClass + "run()"); 579 580 try { 581 log.info(myName + " Log Handler starts"); 582 583 if (trace) { 584 System.err.println(myName + ":Log Handler starts"); 585 } 586 while (true) { 587 if (idleTimes >= maxIdleTimes) { 588 if (trace) { 589 System.err.println(myName + 590 ":Log handler idle more than " + 591 maxIdleTimes + 592 " times - shutting down"); 593 } 594 595 return; 596 } 597 try { 598 checkQueue(); 599 } catch (LogException ae) { 600 log.error(myName + "Unable to check log queue: " + 601 ae.getMessage()); 602 } 603 604 yield(); 605 sleep(sleepTime * 1000); 606 } 607 608 } catch (InterruptedException ie) { 609 log.info(myName + ": LogHandler was interrupted:" + 610 ie.getMessage()); 611 } catch (Throwable t) { 612 System.err.println("Test"); 613 } 614 } 615 616 622 public static void setMax(int newMax) { 623 if (newMax < 0) { 624 maxLevel = 0; 625 } else if (newMax > 9) { 626 maxLevel = 9; 627 } else { 628 maxLevel = newMax; 629 } 630 } 631 632 638 public static void setDaemonMode(boolean newValue) { 639 daemonMode = newValue; 640 } 641 642 646 private static void setupLevels() { 647 maxLevel = 9; 648 649 } 665 666 667 671 public synchronized static void startUp() { 672 idleTimes = 0; 673 674 boolean restart = false; 675 676 if (myInstance == null) { 677 restart = true; 678 } else { 679 if (!myInstance.isAlive()) { 680 restart = true; 681 } 682 } 683 if (restart) { 684 setupLevels(); 685 myInstance = new LogHandler(); 686 myInstance.setDaemon(daemonMode); 687 myInstance.start(); 688 } 689 } 690 691 692 701 public static void tryLog(int newLevel, String objectName, String msg, 702 String color) { 703 log(newLevel, objectName, msg, color); 704 } 705 706 713 public static void tryLog(String objectName, Exception e) { 714 String myName = (thisClass + "tryLog(String, Exception)"); 715 String message = e.getMessage(); 716 717 if (e instanceof DBException) { 718 DBException de = (DBException) e; 719 message = de.getMessage() + ":" + de.getDBMessage(); 720 } 721 try { 722 startUp(); 723 log(objectName, e); 724 } catch (LogException he) { 725 System.err.println(myName + ":Unable to log exception '" + 726 message + "' from object '" + objectName + 727 "':" + he.getMessage()); 728 he.printStackTrace(System.err); 729 e.printStackTrace(System.err); 730 } 731 } 732 733 740 public static void tryLog(String objectName, String msg) { 741 String myName = (thisClass + "tryLog(String, String)"); 742 743 try { 744 log(objectName, msg); 745 } catch (LogException he) { 746 System.err.println(myName + ":Unable to log message '" + msg + 747 "' from object '" + objectName + "':" + 748 he.getMessage()); 749 he.printStackTrace(System.err); 750 } 751 } 752 753 764 public static void tryLog(String dbName, String channelName, int newLevel, 765 String objectName, String msg, String color) { 766 log(dbName, channelName, newLevel, objectName, msg, color, "", ""); 767 } 768 769 782 public static void tryLog(String dbName, String channelName, int newLevel, 783 String objectName, String msg, String color, 784 String newUid, String newJobNumber) { 785 log(dbName, channelName, newLevel, objectName, msg, color, newUid, 786 newJobNumber); 787 } 788 789 797 public static void tryLog(String objectName, String msg, String color) { 798 log(objectName, msg, color); 799 } 800 801 811 public static void tryLog(String objectName, String msg, String color, 812 String uid, String jobNumber) { 813 log(objectName, msg, color, uid, jobNumber); 814 } 815 816 } 817 | Popular Tags |