1 package org.jzonic.jlo.processor; 2 3 import org.jzonic.jlo.LogEvent; 4 import org.jzonic.jlo.LogGenerator; 5 import org.jzonic.jlo.LogRecord; 6 7 import java.util.List ; 8 import java.util.Vector ; 9 10 17 public class AsynchronousLogProcessor extends AbstractLogProcessor implements Runnable { 18 19 private static List events = new Vector (); 20 private static long counter = 0; 21 private static Thread threadCleanerUpper = null; 22 private boolean running = false; 23 int milliSecondSleepTime = 500; 24 25 28 public AsynchronousLogProcessor() { 29 running = true; 30 Thread thread = new Thread (this); 31 thread.setDaemon(true); 32 thread.start(); 33 } 34 35 public void run() { 36 LogEvent le; 37 while ( running ) { 38 while ( getEvents().iterator().hasNext() ) { 39 le = getNextRecord(); 40 handlePipes(le); 41 handle(le); 42 handleSpecialChannels(le.getLogRecord()); 43 } 44 try { 46 Thread.sleep(this.milliSecondSleepTime); 47 } 48 catch (Exception e) { 49 } 51 } 52 } 53 54 private synchronized List getEvents() { 55 return events; 56 } 57 58 65 public void processEvent(LogGenerator lg,LogRecord lr) { 66 LogEvent le = new LogEvent(lg.getHandler(), lg.getFormatter(), lr); 67 if ( lg.getFilter() != null ) { 68 if ( lg.getFilter().match(lr.getMessage() )) { 69 getEvents().add(le); 70 } 71 } 72 else { 73 getEvents().add(le); 74 } 75 } 76 77 public void flush() { 78 while ( getEvents().iterator().hasNext() ) { 79 LogEvent le = getNextRecord(); 80 if (le.getFormatter() != null) { 81 String msg = le.getFormatter().formatMessage(le.getLogRecord()); 82 le.getHandler().publish(msg); 83 } else { 84 le.getHandler().publish(le.getLogRecord()); 85 } 86 87 } 88 } 89 90 private synchronized LogEvent getNextRecord() { 91 LogEvent le = (LogEvent) getEvents().iterator().next(); 92 getEvents().remove(le); 93 return le; 94 } 95 96 public String getProcessorName() { 97 return "AsynchronousLogProcessor"; 98 } 99 100 } 101 | Popular Tags |