001: package com.jamonapi.log4j;
002:
003: import org.apache.log4j.spi.LoggingEvent;
004: import com.jamonapi.JAMonBufferListener;
005: import com.jamonapi.JAMonListener;
006: import com.jamonapi.Monitor;
007: import com.jamonapi.MonKey;
008: import com.jamonapi.utils.BufferList;
009: import com.jamonapi.utils.Misc;
010:
011: /**
012: * <p>
013: * This class can act as a standard JAMonBufferListener/FIFOBuffer or more
014: * interestingly if used with log4j it will put in the Buffer data that although
015: * designed to work as a Buffer that displays details unique to log4j, if the
016: * monitor does not have a Log4jMonKey, it will fallback to Standard
017: * JAMonBufferListener behaviour. This makes it so no problems exist if someone
018: * inadvertently assigns a Log4jBufferListener to a non log4j entity.
019: * </p>
020: *
021: *
022: * @author steve souza
023: *
024: */
025:
026: public class Log4jBufferListener extends JAMonBufferListener {
027:
028: private boolean isLog4jMonKey = false;// looks at data to determine if a
029: // key is log4j. if so
030:
031: // header used to display details in the JAMonBufferListener.
032: private static final String[] LOG4J_HEADER = new String[] {
033: "LoggerName", "Level", "ThreadName", "FormattedMessage",
034: "Date", "Exception" };
035:
036: //{"Label","LastValue","Active","Date"};more data
037:
038: /**
039: * Constructor that creaates this object with its default name (the class
040: * name)
041: */
042: public Log4jBufferListener() {
043: super ("Log4jBufferListener");
044: }
045:
046: /** Pass in the jamonListener name */
047: public Log4jBufferListener(String name) {
048: super (name);
049: }
050:
051: /** Name the listener and pass in the jamon BufferList to use */
052: public Log4jBufferListener(String name, BufferList list) {
053: super (name, list);
054: }
055:
056: private Log4jBufferListener(String name, BufferList list,
057: boolean isLog4jMonKey) {
058: this (name, list);
059: this .isLog4jMonKey = isLog4jMonKey;
060: }
061:
062: /**
063: * When this event is fired the monitor will be added to the rolling buffer.
064: * If it is a log4j monitor the buffer will be specific to log4j fields
065: * (i.e.LoggingEvent info such as threadname, formattedmessage, exception
066: * stack trace and a few others. If it is not then the super class's
067: * processEvent is called.
068: *
069: */
070: public void processEvent(Monitor mon) {
071: MonKey monKey = mon.getMonKey();
072: // If the key is a log4j key it has the LoggingEvent in it and we can
073: // put that data into the buffer. If the first record is passed and it is a Log4JMonKey then use
074: // more specific log4j detail buffer array. Note this variable is also used in the header method.
075: if (monKey instanceof Log4jMonKey && !isLog4jMonKey)
076: isLog4jMonKey = true;
077:
078: if (isLog4jMonKey) {
079: Log4jMonKey key = (Log4jMonKey) monKey;
080: getBufferList().addRow(toArray(key.getLoggingEvent(), mon));
081: } else
082: super .processEvent(mon);
083:
084: }
085:
086: /**
087: * method that returns an array to use in the Buffer. It can return any
088: * sortable objects as long as they match what is returned in the
089: * getHeader() method.
090: *
091: * @param event
092: * @param mon
093: * @return
094: */
095: protected Object[] toArray(LoggingEvent event, Monitor mon) {
096: return new Object[] {
097: event.getLoggerName(),
098: event.getLevel().toString(),
099: event.getThreadName(),
100: mon.getMonKey().getDetails(),
101: mon.getLastAccess(),
102: (event.getThrowableInformation() == null || event
103: .getThrowableInformation().getThrowable() == null) ? ""
104: : Misc.getExceptionTrace(event
105: .getThrowableInformation()
106: .getThrowable()) };
107:
108: }
109:
110: protected String[] getDefaultHeader() {
111: return LOG4J_HEADER;
112: }
113:
114: /** Makes a usable copy of this BufferListener */
115: public JAMonListener copy() {
116: return new Log4jBufferListener(getName(), getBufferList()
117: .copy(), isLog4jMonKey);
118: }
119:
120: /** Returns the valid header for display of this buffer */
121: public String[] getHeader() {
122: if (isLog4jMonKey)
123: return getDefaultHeader();
124: else
125: return getBufferList().getHeader();
126: }
127:
128: }
|