[slf4j-dev] svn commit: r1282 - in slf4j/trunk/slf4j-ext/src: main/java/org/slf4j/ext test/java/org/slf4j/dummyExt
rgoers at slf4j.org
rgoers at slf4j.org
Wed Feb 25 10:01:42 CET 2009
Author: rgoers
Date: Wed Feb 25 10:01:41 2009
New Revision: 1282
Added:
slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/ext/EventData.java (contents, props changed)
slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/ext/EventException.java
slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/ext/EventLogger.java (contents, props changed)
slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/EventLoggerTest.java (contents, props changed)
- copied, changed from r1281, /slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/XLoggerTest.java
Modified:
slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/PackageTest.java
Log:
Add EventLogger
Added: slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/ext/EventData.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/ext/EventData.java Wed Feb 25 10:01:41 2009
@@ -0,0 +1,193 @@
+package org.slf4j.ext;
+
+
+import java.io.Serializable;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.beans.XMLDecoder;
+import java.beans.XMLEncoder;
+import java.beans.ExceptionListener;
+
+/**
+ * Base class for Event Data. Event Data contains data to be logged about an event. Users may
+ * extend this class for each EventType they want to log.
+ *
+ * @author Ralph Goers
+ */
+public class EventData implements Serializable
+{
+ private Map<String, Object> eventData = new HashMap<String,Object>();
+ public static final String EVENT_MESSAGE = "EventMessage";
+ public static final String EVENT_TYPE = "EventType";
+ public static final String EVENT_DATETIME = "EventDateTime";
+ public static final String EVENT_ID = "EventId";
+
+ /**
+ * Default Constructor
+ */
+ public EventData()
+ {
+ }
+
+ /**
+ * Constructor to create event data from a Map.
+ * @param map The event data.
+ */
+ public EventData(Map<String, Object> map)
+ {
+ eventData.putAll(map);
+ }
+
+ /**
+ * Construct from a serialized form of the Map containing the RequestInfo elements
+ * @param xml The serialized form of the RequestInfo Map.
+ */
+ public EventData(String xml)
+ {
+ ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
+ try
+ {
+ XMLDecoder decoder = new XMLDecoder(bais);
+ this.eventData = (Map<String, Object>)decoder.readObject();
+ }
+ catch (Exception e)
+ {
+ throw new EventException("Error decoding " + xml, e);
+ }
+ }
+
+ /**
+ * Serialize all the EventData items into an XML representation.
+ * @return an XML String containing all the EventDAta items.
+ */
+ public String toXML()
+ {
+ return toXML(eventData);
+ }
+
+ /**
+ * Serialize all the EventData items into an XML representation.
+ * @return an XML String containing all the EventDAta items.
+ */
+ public static String toXML(Map<String, Object>map)
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try
+ {
+ XMLEncoder encoder = new XMLEncoder(baos);
+ encoder.setExceptionListener(new ExceptionListener()
+ {
+ public void exceptionThrown(Exception exception)
+ {
+ exception.printStackTrace();
+ }
+ });
+ encoder.writeObject(map);
+ encoder.close();
+ return baos.toString();
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ public String getEventId()
+ {
+ return (String)this.eventData.get(EVENT_ID);
+ }
+
+ public void setEventId(String eventId)
+ {
+ if (eventId == null)
+ {
+ throw new IllegalArgumentException("eventId cannot be null");
+ }
+ this.eventData.put(EVENT_ID, eventId);
+ }
+
+ public String getMessage()
+ {
+ return (String)this.eventData.get(EVENT_MESSAGE);
+ }
+
+ public void setMessage(String message)
+ {
+ this.eventData.put(EVENT_MESSAGE, message);
+ }
+
+ public Date getEventDateTime()
+ {
+ return (Date)this.eventData.get(EVENT_DATETIME);
+ }
+
+ public void setEventDateTime(Date eventDateTime)
+ {
+ this.eventData.put(EVENT_DATETIME, eventDateTime);
+ }
+
+ public void setEventType(String eventType)
+ {
+ this.eventData.put(EVENT_TYPE, eventType);
+ }
+
+ public String getEventType()
+ {
+ return (String)this.eventData.get(EVENT_TYPE);
+ }
+
+
+ public void put(String name, Serializable obj)
+ {
+ this.eventData.put(name, obj);
+ }
+
+ public Serializable get(String name)
+ {
+ return (Serializable) this.eventData.get(name);
+ }
+
+ public void putAll(Map data)
+ {
+ this.eventData.putAll(data);
+ }
+
+ public int getSize()
+ {
+ return this.eventData.size();
+ }
+
+ public Iterator getEntrySetIterator()
+ {
+ return this.eventData.entrySet().iterator();
+ }
+
+ public Map getEventMap()
+ {
+ return this.eventData;
+ }
+
+ public String toString()
+ {
+ return toXML();
+ }
+
+ public boolean equals(Object o)
+ {
+ if (this == o) return true;
+ if (!(o instanceof EventData || o instanceof Map)) return false;
+ Map<String, Object>map = (o instanceof EventData) ? ((EventData)o).getEventMap() : (Map<String, Object>)o;
+
+ return this.eventData.equals(map);
+ }
+
+ public int hashCode()
+ {
+ return this.eventData.hashCode();
+ }
+}
\ No newline at end of file
Added: slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/ext/EventException.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/ext/EventException.java Wed Feb 25 10:01:41 2009
@@ -0,0 +1,27 @@
+package org.slf4j.ext;
+
+/**
+ *
+ */
+public class EventException extends RuntimeException
+{
+ public EventException()
+ {
+ super();
+ }
+
+ public EventException(String exceptionMessage)
+ {
+ super(exceptionMessage);
+ }
+
+ public EventException(Throwable originalException)
+ {
+ super(originalException);
+ }
+
+ public EventException(String exceptionMessage, Throwable originalException)
+ {
+ super(exceptionMessage, originalException);
+ }
+}
Added: slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/ext/EventLogger.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/ext/EventLogger.java Wed Feb 25 10:01:41 2009
@@ -0,0 +1,44 @@
+package org.slf4j.ext;
+
+import org.slf4j.LoggerFactory;
+import org.slf4j.Marker;
+import org.slf4j.MarkerFactory;
+import org.slf4j.spi.LocationAwareLogger;
+
+/**
+ * Simple Logger used to log events. All events are directed to a logger named "EventLogger"
+ * with a level of ERROR (the closest to "always" that SLF4J gets) and with an Event marker.
+ *
+ * @author Ralph Goers
+ */
+public class EventLogger {
+
+ private static final String FQCN = EventLogger.class.getName();
+
+ static Marker EVENT_MARKER = MarkerFactory.getMarker("EVENT");
+
+ private static LoggerWrapper eventLogger =
+ eventLogger = new LoggerWrapper(LoggerFactory.getLogger("EventLogger"), FQCN);
+
+
+
+ /**
+ * There can only be a single EventLogger.
+ */
+ private EventLogger() {
+ }
+
+ /**
+ * Logs the event.
+ * @param data The EventData.
+ */
+ public static void logEvent(EventData data)
+ {
+ if (eventLogger.instanceofLAL) {
+ ((LocationAwareLogger) eventLogger.logger).log(EVENT_MARKER, FQCN,
+ LocationAwareLogger.ERROR_INT, data.toXML(), null);
+ } else {
+ eventLogger.logger.error(EVENT_MARKER, data.toXML(), data);
+ }
+ }
+}
Copied: slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/EventLoggerTest.java (from r1281, /slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/XLoggerTest.java)
==============================================================================
--- /slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/XLoggerTest.java (original)
+++ slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/EventLoggerTest.java Wed Feb 25 10:01:41 2009
@@ -30,15 +30,22 @@
import org.apache.log4j.spi.LoggingEvent;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;
+import org.slf4j.ext.EventData;
+import org.slf4j.ext.EventLogger;
+import org.slf4j.MDC;
+
+import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
-public class XLoggerTest extends TestCase {
+public class EventLoggerTest extends TestCase {
ListAppender listAppender;
- org.apache.log4j.Logger log4jRoot;
+ org.apache.log4j.Logger log4;
- final static String EXPECTED_FILE_NAME = "XLoggerTest.java";
+ final static String EXPECTED_FILE_NAME = "EventLoggerTest.java";
- public XLoggerTest(String name) {
+ public EventLoggerTest(String name) {
super(name);
}
@@ -49,13 +56,24 @@
listAppender = new ListAppender();
listAppender.extractLocationInfo = true;
- log4jRoot = org.apache.log4j.Logger.getRootLogger();
- log4jRoot.addAppender(listAppender);
- log4jRoot.setLevel(org.apache.log4j.Level.TRACE);
+ org.apache.log4j.Logger eventLogger =
+ org.apache.log4j.Logger.getLogger("EventLogger");
+ eventLogger.addAppender(listAppender);
+ eventLogger.setLevel(org.apache.log4j.Level.TRACE);
+ eventLogger.setAdditivity(false);
+ // Items that apply to any activity
+ MDC.put("ipAddress", "192.168.1.110");
+ MDC.put("login", "TestUSer");
+ MDC.put("hostname", "localhost");
+ MDC.put("productName", "SLF4J");
+ MDC.put("locale", Locale.getDefault().getDisplayName());
+ MDC.put("timezone", TimeZone.getDefault().getDisplayName());
+
}
public void tearDown() throws Exception {
super.tearDown();
+ MDC.clear();
}
void verify(LoggingEvent le, String expectedMsg) {
@@ -63,79 +81,30 @@
assertEquals(EXPECTED_FILE_NAME, le.getLocationInformation().getFileName());
}
- void verifyWithException(LoggingEvent le, String expectedMsg, Throwable t) {
- verify(le, expectedMsg);
- assertEquals(t.toString(), le.getThrowableStrRep()[0]);
- }
-
- public void testEntering() {
- XLogger logger = XLoggerFactory.getXLogger("UnitTest");
- logger.entry();
- logger.entry(1);
- logger.entry("test");
-
- assertEquals(3, listAppender.list.size());
- verify((LoggingEvent) listAppender.list.get(0), "entry");
- verify((LoggingEvent) listAppender.list.get(1), "entry with (1)");
- verify((LoggingEvent) listAppender.list.get(2), "entry with (test)");
- }
-
- public void testExiting() {
- XLogger logger = XLoggerFactory.getXLogger("UnitTest");
- logger.exit();
- logger.exit(0);
- logger.exit(false);
-
- assertEquals(3, listAppender.list.size());
- verify((LoggingEvent) listAppender.list.get(0), "exit");
- verify((LoggingEvent) listAppender.list.get(1), "exit with (0)");
- verify((LoggingEvent) listAppender.list.get(2), "exit with (false)");
- }
- public void testThrowing() {
- XLogger logger = XLoggerFactory.getXLogger("UnitTest");
- Throwable t = new UnsupportedOperationException("Test");
- logger.throwing(t);
- assertEquals(1, listAppender.list.size());
- verifyWithException((LoggingEvent) listAppender.list.get(0), "throwing", t);
- }
-
- public void testCaught() {
- XLogger logger = XLoggerFactory.getXLogger("UnitTest");
- long x = 5;
- Throwable t = null;
- try {
- @SuppressWarnings("unused")
- long y = x / 0;
- } catch (Exception ex) {
- t = ex;
- logger.catching(ex);
- }
- verifyWithException((LoggingEvent) listAppender.list.get(0), "catching", t);
- }
-
- // See http://bugzilla.slf4j.org/show_bug.cgi?id=114
- public void testLocationExtraction_Bug114() {
- XLogger logger = XLoggerFactory.getXLogger("UnitTest");
- int line = 121; // next line is line number 121
- logger.exit();
- logger.debug("hello");
+ public void testEventLogger() {
+ EventData data[] = new EventData[2];
+ data[0] = new EventData();
+ data[0].setEventType("Login");
+ data[0].setEventId("1");
+ data[0].setEventDateTime(new Date());
+ data[0].put("Userid", "TestUser");
+ EventLogger.logEvent(data[0]);
+
+ data[1] = new EventData();
+ data[1].setEventType("Update");
+ data[1].setEventId("2");
+ data[1].setEventDateTime(new Date());
+ data[1].put("FileName", "/etc/hosts");
+ EventLogger.logEvent(data[1]);
assertEquals(2, listAppender.list.size());
-
- {
- LoggingEvent e = listAppender.list.get(0);
- LocationInfo li = e.getLocationInformation();
+ for (int i=0; i < 2; ++i) {
+ LoggingEvent event = listAppender.list.get(i);
+ verify(event, data[i].toXML());
+ LocationInfo li = event.getLocationInformation();
assertEquals(this.getClass().getName(), li.getClassName());
- assertEquals(""+line, li.getLineNumber());
+ assertEquals(event.getMDC("hostname"), "localhost");
}
-
- {
- LoggingEvent e = listAppender.list.get(1);
- LocationInfo li = e.getLocationInformation();
- assertEquals(this.getClass().getName(), li.getClassName());
- assertEquals(""+(line+1), li.getLineNumber());
- }
-
}
-}
+}
\ No newline at end of file
Modified: slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/PackageTest.java
==============================================================================
--- slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/PackageTest.java (original)
+++ slf4j/trunk/slf4j-ext/src/test/java/org/slf4j/dummyExt/PackageTest.java Wed Feb 25 10:01:41 2009
@@ -8,6 +8,7 @@
TestSuite suite = new TestSuite();
suite.addTestSuite(MDCStrLookupTest.class);
suite.addTestSuite(XLoggerTest.class);
+ suite.addTestSuite(EventLoggerTest.class);
return suite;
}
}
More information about the slf4j-dev
mailing list