1 16 17 package org.apache.log4j.lf5; 18 19 import org.apache.log4j.lf5.util.Resource; 20 import org.apache.log4j.lf5.viewer.LogBrokerMonitor; 21 import org.apache.log4j.AppenderSkeleton; 22 import org.apache.log4j.spi.LocationInfo; 23 import org.apache.log4j.spi.LoggingEvent; 24 25 import java.awt.*; 26 27 35 36 38 public class LF5Appender extends AppenderSkeleton { 39 43 47 protected LogBrokerMonitor _logMonitor; 48 protected static LogBrokerMonitor _defaultLogMonitor; 49 protected static AppenderFinalizer _finalizer; 50 51 55 59 67 public LF5Appender() { 68 this(getDefaultInstance()); 69 } 70 71 80 public LF5Appender(LogBrokerMonitor monitor) { 81 82 if (monitor != null) { 83 _logMonitor = monitor; 84 } 85 } 86 87 91 97 public void append(LoggingEvent event) { 98 String category = event.getLoggerName(); 100 String logMessage = event.getRenderedMessage(); 101 String nestedDiagnosticContext = event.getNDC(); 102 String threadDescription = event.getThreadName(); 103 String level = event.getLevel().toString(); 104 long time = event.timeStamp; 105 LocationInfo locationInfo = event.getLocationInformation(); 106 107 Log4JLogRecord record = new Log4JLogRecord(); 109 110 record.setCategory(category); 111 record.setMessage(logMessage); 112 record.setLocation(locationInfo.fullInfo); 113 record.setMillis(time); 114 record.setThreadDescription(threadDescription); 115 116 if (nestedDiagnosticContext != null) { 117 record.setNDC(nestedDiagnosticContext); 118 } else { 119 record.setNDC(""); 120 } 121 122 if (event.getThrowableInformation() != null) { 123 record.setThrownStackTrace(event.getThrowableInformation()); 124 } 125 126 try { 127 record.setLevel(LogLevel.valueOf(level)); 128 } catch (LogLevelFormatException e) { 129 record.setLevel(LogLevel.WARN); 132 } 133 134 if (_logMonitor != null) { 135 _logMonitor.addMessage(record); 136 } 137 } 138 139 143 public void close() { 144 } 145 146 151 public boolean requiresLayout() { 152 return false; 153 } 154 155 168 public void setCallSystemExitOnClose(boolean callSystemExitOnClose) { 169 _logMonitor.setCallSystemExitOnClose(callSystemExitOnClose); 170 } 171 172 181 public boolean equals(LF5Appender compareTo) { 182 return _logMonitor == compareTo.getLogBrokerMonitor(); 184 } 185 186 public LogBrokerMonitor getLogBrokerMonitor() { 187 return _logMonitor; 188 } 189 190 public static void main(String [] args) { 191 new LF5Appender(); 192 } 193 194 public void setMaxNumberOfRecords(int maxNumberOfRecords) { 195 _defaultLogMonitor.setMaxNumberOfLogRecords(maxNumberOfRecords); 196 } 197 201 204 protected static synchronized LogBrokerMonitor getDefaultInstance() { 205 if (_defaultLogMonitor == null) { 206 try { 207 _defaultLogMonitor = 208 new LogBrokerMonitor(LogLevel.getLog4JLevels()); 209 _finalizer = new AppenderFinalizer(_defaultLogMonitor); 210 211 _defaultLogMonitor.setFrameSize(getDefaultMonitorWidth(), 212 getDefaultMonitorHeight()); 213 _defaultLogMonitor.setFontSize(12); 214 _defaultLogMonitor.show(); 215 216 } catch (SecurityException e) { 217 _defaultLogMonitor = null; 218 } 219 } 220 221 return _defaultLogMonitor; 222 } 223 224 229 protected static int getScreenWidth() { 230 try { 231 return Toolkit.getDefaultToolkit().getScreenSize().width; 232 } catch (Throwable t) { 233 return 800; 234 } 235 } 236 237 242 protected static int getScreenHeight() { 243 try { 244 return Toolkit.getDefaultToolkit().getScreenSize().height; 245 } catch (Throwable t) { 246 return 600; 247 } 248 } 249 250 protected static int getDefaultMonitorWidth() { 251 return (3 * getScreenWidth()) / 4; 252 } 253 254 protected static int getDefaultMonitorHeight() { 255 return (3 * getScreenHeight()) / 4; 256 } 257 261 262 266 } 267 | Popular Tags |