1 16 package org.apache.log4j.lf5.util; 17 18 import org.apache.log4j.lf5.LogLevel; 19 import org.apache.log4j.lf5.LogRecord; 20 import org.apache.log4j.lf5.viewer.LogBrokerMonitor; 21 22 import java.awt.*; 23 import java.util.Arrays ; 24 import java.util.List ; 25 26 31 32 34 public class LogMonitorAdapter { 35 public static final int LOG4J_LOG_LEVELS = 0; 39 public static final int JDK14_LOG_LEVELS = 1; 40 44 private LogBrokerMonitor _logMonitor; 48 private LogLevel _defaultLevel = null; 49 50 private LogMonitorAdapter(List userDefinedLevels) { 54 super(); 55 _defaultLevel = (LogLevel) userDefinedLevels.get(0); 57 _logMonitor = new LogBrokerMonitor(userDefinedLevels); 58 59 _logMonitor.setFrameSize(getDefaultMonitorWidth(), 60 getDefaultMonitorHeight()); 61 _logMonitor.setFontSize(12); 62 _logMonitor.show(); 63 } 64 75 public static LogMonitorAdapter newInstance(int loglevels) { 76 LogMonitorAdapter adapter; 77 if (loglevels == JDK14_LOG_LEVELS) { 78 adapter = newInstance(LogLevel.getJdk14Levels()); 79 adapter.setDefaultLevel(LogLevel.FINEST); 80 adapter.setSevereLevel(LogLevel.SEVERE); 81 } else { 82 adapter = newInstance(LogLevel.getLog4JLevels()); 83 adapter.setDefaultLevel(LogLevel.DEBUG); 84 adapter.setSevereLevel(LogLevel.FATAL); 85 } 86 return adapter; 87 } 88 89 97 public static LogMonitorAdapter newInstance(LogLevel[] userDefined) { 98 if (userDefined == null) { 99 return null; 100 } 101 return newInstance(Arrays.asList(userDefined)); 102 } 103 104 112 public static LogMonitorAdapter newInstance(List userDefinedLevels) { 113 return new LogMonitorAdapter(userDefinedLevels); 114 } 115 116 121 public void addMessage(LogRecord record) { 122 _logMonitor.addMessage(record); 123 } 124 125 130 public void setMaxNumberOfRecords(int maxNumberOfRecords) { 131 _logMonitor.setMaxNumberOfLogRecords(maxNumberOfRecords); 132 } 133 134 140 public void setDefaultLevel(LogLevel level) { 141 _defaultLevel = level; 142 } 143 144 149 public LogLevel getDefaultLevel() { 150 return _defaultLevel; 151 } 152 153 158 public void setSevereLevel(LogLevel level) { 159 AdapterLogRecord.setSevereLevel(level); 160 } 161 162 167 public LogLevel getSevereLevel() { 168 return AdapterLogRecord.getSevereLevel(); 169 } 170 171 181 public void log(String category, LogLevel level, String message, 182 Throwable t, String NDC) { 183 AdapterLogRecord record = new AdapterLogRecord(); 184 record.setCategory(category); 185 record.setMessage(message); 186 record.setNDC(NDC); 187 record.setThrown(t); 188 189 if (level == null) { 190 record.setLevel(getDefaultLevel()); 191 } else { 192 record.setLevel(level); 193 } 194 195 addMessage(record); 196 } 197 198 204 public void log(String category, String message) { 205 log(category, null, message); 206 } 207 208 216 public void log(String category, LogLevel level, String message, String NDC) { 217 log(category, level, message, null, NDC); 218 } 219 220 228 public void log(String category, LogLevel level, String message, 229 Throwable t) { 230 log(category, level, message, t, null); 231 } 232 233 240 public void log(String category, LogLevel level, String message) { 241 log(category, level, message, null, null); 242 } 243 244 252 protected static int getScreenWidth() { 253 try { 254 return Toolkit.getDefaultToolkit().getScreenSize().width; 255 } catch (Throwable t) { 256 return 800; 257 } 258 } 259 260 265 protected static int getScreenHeight() { 266 try { 267 return Toolkit.getDefaultToolkit().getScreenSize().height; 268 } catch (Throwable t) { 269 return 600; 270 } 271 } 272 273 protected static int getDefaultMonitorWidth() { 274 return (3 * getScreenWidth()) / 4; 275 } 276 277 protected static int getDefaultMonitorHeight() { 278 return (3 * getScreenHeight()) / 4; 279 } 280 284 } 288 289 | Popular Tags |