1 22 package org.jboss.services.loggingmonitor; 23 24 import java.util.Timer ; 25 import java.util.TimerTask ; 26 27 import javax.management.MalformedObjectNameException ; 28 29 import org.apache.log4j.Appender; 30 import org.apache.log4j.Level; 31 import org.apache.log4j.Logger; 32 import org.apache.log4j.PatternLayout; 33 import org.jboss.logging.appender.DailyRollingFileAppender; 34 import org.jboss.system.ServiceMBeanSupport; 35 import org.w3c.dom.DOMException ; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.NodeList ; 39 import org.w3c.dom.Text ; 40 41 49 public class LoggingMonitor extends ServiceMBeanSupport 50 implements LoggingMonitorMBean 51 { 52 54 public final static String MONITORED_MBEAN_ELEMENT = "monitoredmbean"; 55 public final static String MBEAN_NAME_ATTRIBUTE = "name"; 56 public final static String MBEAN_LOGGER_ATTRIBUTE = "logger"; 57 public final static String MBEAN_ATTRIBUTE_ELEMENT = "attribute"; 58 59 public final static String DEFAULT_PATTERN_LAYOUT = "%d %-5p [%c] %m%n"; 60 61 63 private String filename; 64 private boolean appendToFile; 65 private RolloverPeriod rolloverPeriod; 66 private MonitoredMBean[] monitoredObjects; 67 private long monitorPeriod; 68 private String patternLayout; 69 70 private Appender appender; 71 private Timer timer; 72 73 75 78 public LoggingMonitor() 79 { 80 appendToFile = true; 81 rolloverPeriod = new RolloverPeriod("DAY"); 82 patternLayout = DEFAULT_PATTERN_LAYOUT; 83 } 84 85 87 90 public void setFilename(String filename) 91 { 92 if (filename == null || filename.length() == 0) 93 { 94 throw new IllegalArgumentException ("Logging monitor's filename can not be null or empty"); 95 } 96 this.filename = filename; 97 } 98 99 102 public String getFilename() 103 { 104 return filename; 105 } 106 107 110 public void setAppendToFile(boolean appendToFile) 111 { 112 this.appendToFile = appendToFile; 113 } 114 115 118 public boolean getAppendToFile() 119 { 120 return appendToFile; 121 } 122 123 126 public void setRolloverPeriod(String rolloverPeriod) 127 { 128 this.rolloverPeriod = new RolloverPeriod(rolloverPeriod); 129 } 130 131 134 public String getRolloverPeriod() 135 { 136 return rolloverPeriod.toString(); 137 } 138 139 142 public void setMonitorPeriod(long monitorPeriod) 143 { 144 if (monitorPeriod < 1) 145 { 146 throw new IllegalArgumentException ("Logging monitor's monitor period must be a positive, non-zero value"); 147 } 148 this.monitorPeriod = monitorPeriod; 149 } 150 151 154 public long getMonitorPeriod() 155 { 156 return monitorPeriod; 157 } 158 159 162 public void setPatternLayout(String patternLayout) 163 { 164 this.patternLayout = patternLayout; 165 } 166 167 170 public String getPatternLayout() 171 { 172 return patternLayout; 173 } 174 175 178 public String getRolloverFormat() 179 { 180 return rolloverPeriod.getRolloverFormat(); 181 } 182 183 186 public void setMonitoredObjects(Element monitoredObjects) throws MalformedObjectNameException 187 { 188 NodeList monitoredMBeans = monitoredObjects.getElementsByTagName(MONITORED_MBEAN_ELEMENT); 189 190 int mbeanCount = monitoredMBeans.getLength(); 191 if (mbeanCount < 1) 192 { 193 throw createMissingElementException(MONITORED_MBEAN_ELEMENT); 194 } 195 196 this.monitoredObjects = new MonitoredMBean[mbeanCount]; 197 for (int i = 0; i < mbeanCount; ++i) 198 { 199 Node monitoredMBean = monitoredMBeans.item(i); 200 this.monitoredObjects[i] = toMonitoredMBean((Element ) monitoredMBean); 201 } 202 } 203 204 206 protected void startService() 207 { 208 if (monitoredObjects == null) 209 { 210 throw new IllegalStateException ("'MonitoredObjects' attribute not configured"); 211 } 212 DailyRollingFileAppender appender = new DailyRollingFileAppender(); 213 appender.setFile(filename); 214 appender.setAppend(appendToFile); 215 appender.setDatePattern(rolloverPeriod.getRolloverFormat()); 216 appender.setLayout(new PatternLayout(patternLayout)); 217 appender.setThreshold(Level.INFO); 218 appender.activateOptions(); 219 this.appender = appender; 220 221 for (int i = 0; i < monitoredObjects.length; ++i) 222 { 223 monitoredObjects[i].getLogger().addAppender(appender); 224 } 225 226 TimerTask task = new LoggingMonitorTimerTask(monitoredObjects, log); 228 229 timer = new Timer (); 230 timer.schedule(task, 0, monitorPeriod); 231 232 log.debug("Logging monitor started logging to " + filename); 233 } 234 235 protected void stopService() 236 { 237 timer.cancel(); 238 239 for (int i = 0; i < monitoredObjects.length; ++i) 240 { 241 monitoredObjects[i].getLogger().removeAllAppenders(); 242 } 243 244 appender.close(); 245 246 log.debug("Logging monitor stopped logging to " + filename); 247 } 248 249 251 260 private MonitoredMBean toMonitoredMBean(Element element) throws MalformedObjectNameException 261 { 262 String objectName = element.getAttribute(MBEAN_NAME_ATTRIBUTE); 263 264 if ("".equals(objectName)) 265 { 266 throw createAttributeNotFoundException(MBEAN_NAME_ATTRIBUTE); 267 } 268 269 String loggerName = element.getAttribute(MBEAN_LOGGER_ATTRIBUTE); 270 if ("".equals(loggerName)) 271 { 272 throw createAttributeNotFoundException(MBEAN_LOGGER_ATTRIBUTE); 273 } 274 275 Logger logger = Logger.getLogger(loggerName.toLowerCase()); 276 logger.setAdditivity(false); 277 logger.setLevel(Level.INFO); 278 279 String [] attributes = getMonitoredAttributes(element); 280 281 return new MonitoredMBean(objectName, attributes, logger); 282 } 283 284 291 private String [] getMonitoredAttributes(Element monitoredMBean) 292 { 293 NodeList monitoredAttributes = monitoredMBean.getElementsByTagName(MBEAN_ATTRIBUTE_ELEMENT); 294 295 int monitoredAttributesCount = monitoredAttributes.getLength(); 296 if (monitoredAttributesCount < 1) 297 { 298 throw createMissingElementException(MBEAN_ATTRIBUTE_ELEMENT); 299 } 300 301 String [] attributes = new String [monitoredAttributesCount]; 302 for (int i = 0; i < monitoredAttributesCount; ++i) 303 { 304 Node node = monitoredAttributes.item(i); 305 Node attribute = node.getFirstChild(); 306 307 if (attribute.getNodeType() != Node.TEXT_NODE) 308 { 309 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, 310 "Unexpected node type inside <attribute> for monitored MBean."); 311 } 312 attributes[i] = (((Text ) attribute).getData()).trim(); 313 } 314 return attributes; 315 } 316 317 325 private DOMException createMissingElementException(String element) 326 { 327 return new DOMException (DOMException.NOT_FOUND_ERR, 328 "At least one <" + element + "> element is expected"); 329 } 330 331 339 private DOMException createAttributeNotFoundException(String attribute) 340 { 341 return new DOMException (DOMException.NOT_FOUND_ERR, 342 "Missing expected '" + attribute + "' attribute of a <monitoredmbean> element"); 343 } 344 345 } 346 | Popular Tags |