[logback-dev] svn commit: r1641 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic/spi test/java/ch/qos/logback/classic/net test/java/ch/qos/logback/classic/spi
noreply.ceki at qos.ch
noreply.ceki at qos.ch
Tue Mar 11 10:43:47 CET 2008
Author: ceki
Date: Tue Mar 11 10:43:47 2008
New Revision: 1641
Added:
logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/LoggingEventSerializationTest.java
- copied, changed from r1638, /logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/LoggingEventSerializationTest.java
logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/LuckyCharms.java
Removed:
logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/LoggingEventSerializationTest.java
Modified:
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java
Log:
- ongoing work for fixing bug 100
http://bugzilla.qos.ch/show_bug.cgi?id=100
Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java (original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java Tue Mar 11 10:43:47 2008
@@ -47,6 +47,9 @@
*/
private static final long serialVersionUID = 3022264832697160750L;
+ private static final int NULL_ARGUMENT_ARRAY = -1;
+ private static final String NULL_ARGUMENT_ARRAY_ELEMENT = "NULL_ARGUMENT_ARRAY_ELEMENT";
+
/**
*
*/
@@ -80,7 +83,7 @@
private String message;
private String formattedMessage;
- private Object[] argumentArray;
+ private transient Object[] argumentArray;
private ThrowableInformation throwableInfo;
@@ -113,7 +116,7 @@
// bug 85 (we previously failed to set this.argumentArray)
this.argumentArray = argArray;
-
+
if (argArray != null) {
formattedMessage = MessageFormatter.arrayFormat(message, argArray);
} else {
@@ -122,7 +125,8 @@
timeStamp = System.currentTimeMillis();
// the case is ugly but under the circumstances acceptable
- LogbackMDCAdapter logbackMDCAdapter = (LogbackMDCAdapter) MDC.getMDCAdapter();
+ LogbackMDCAdapter logbackMDCAdapter = (LogbackMDCAdapter) MDC
+ .getMDCAdapter();
mdcPropertyMap = logbackMDCAdapter.getPropertyMap();
}
@@ -185,13 +189,13 @@
* This method should be called prior to serializing an event. It should also
* be called when using asynchronous logging.
*
- * <p>Note that due to performance concerns, this method does NOT extract
- * caller data. It is the responsability of the calller to extract caller
+ * <p>
+ * Note that due to performance concerns, this method does NOT extract caller
+ * data. It is the responsability of the calller to extract caller
* information.
*/
public void prepareForDeferredProcessing() {
this.getThreadName();
-
}
public LoggerRemoteView getLoggerRemoteView() {
@@ -286,6 +290,20 @@
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeInt(level.levelInt);
+ if (argumentArray != null) {
+ int len = argumentArray.length;
+ out.writeInt(len);
+ for (int i = 0; i < argumentArray.length; i++) {
+ if (argumentArray[i] != null) {
+ out.writeUTF(argumentArray[i].toString());
+ } else {
+ out.writeUTF(NULL_ARGUMENT_ARRAY_ELEMENT);
+ }
+ }
+ } else {
+ out.writeInt(NULL_ARGUMENT_ARRAY);
+ }
+
}
private void readObject(ObjectInputStream in) throws IOException,
@@ -293,8 +311,19 @@
in.defaultReadObject();
int levelInt = in.readInt();
level = Level.toLevel(levelInt);
+
+ int argArrayLen = in.readInt();
+ if (argArrayLen != NULL_ARGUMENT_ARRAY) {
+ argumentArray = new String[argArrayLen];
+ for (int i = 0; i < argArrayLen; i++) {
+ String val = in.readUTF();
+ if (!NULL_ARGUMENT_ARRAY_ELEMENT.equals(val)) {
+ argumentArray[i] = val;
+ }
+ }
+ }
}
-
+
@Override
public String toString() {
StringBuffer sb = new StringBuffer('[');
@@ -303,5 +332,4 @@
sb.append("\n");
return sb.toString();
}
-
}
Copied: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/LoggingEventSerializationTest.java (from r1638, /logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/LoggingEventSerializationTest.java)
==============================================================================
--- /logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/LoggingEventSerializationTest.java (original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/LoggingEventSerializationTest.java Tue Mar 11 10:43:47 2008
@@ -1,4 +1,8 @@
-package ch.qos.logback.classic.net;
+package ch.qos.logback.classic.spi;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -6,18 +10,16 @@
import java.io.ObjectOutputStream;
import java.util.Map;
-import junit.framework.TestCase;
-
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
import org.slf4j.MDC;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.spi.LoggerContextRemoteView;
-import ch.qos.logback.classic.spi.LoggerRemoteView;
-import ch.qos.logback.classic.spi.LoggingEvent;
-public class LoggingEventSerializationTest extends TestCase {
+public class LoggingEventSerializationTest {
LoggerContext lc;
Logger logger;
@@ -26,24 +28,26 @@
ObjectOutputStream oos;
ObjectInputStream inputStream;
+ @Before
public void setUp() throws Exception {
- super.setUp();
lc = new LoggerContext();
lc.setName("testContext");
logger = lc.getLogger(LoggerContext.ROOT_NAME);
+ // create the byte output stream
+ bos = new ByteArrayOutputStream();
+ oos = new ObjectOutputStream(bos);
}
+
+ @After
public void tearDown() throws Exception {
- super.tearDown();
+
lc = null;
logger = null;
}
+ @Test
public void testBasic() throws Exception {
- // create the byte output stream
- bos = new ByteArrayOutputStream();
- oos = new ObjectOutputStream(bos);
-
LoggingEvent event = createLoggingEvent();
oos.writeObject(event);
@@ -57,11 +61,8 @@
assertEquals(Level.DEBUG, remoteEvent.getLevel());
}
+ @Test
public void testContext() throws Exception {
- // create the byte output stream
- bos = new ByteArrayOutputStream();
- oos = new ObjectOutputStream(bos);
-
lc.putProperty("testKey", "testValue");
LoggingEvent event = createLoggingEvent();
oos.writeObject(event);
@@ -85,11 +86,8 @@
assertEquals("testValue", props.get("testKey"));
}
+ @Test
public void testMDC() throws Exception {
- // create the byte output stream
- bos = new ByteArrayOutputStream();
- oos = new ObjectOutputStream(bos);
-
MDC.put("key", "testValue");
LoggingEvent event = createLoggingEvent();
oos.writeObject(event);
@@ -104,11 +102,8 @@
assertEquals("testValue", MDCPropertyMap.get("key"));
}
+ @Test
public void testUpdatedMDC() throws Exception {
- // create the byte output stream
- bos = new ByteArrayOutputStream();
- oos = new ObjectOutputStream(bos);
-
MDC.put("key", "testValue");
LoggingEvent event1 = createLoggingEvent();
oos.writeObject(event1);
@@ -131,6 +126,28 @@
assertEquals("updatedTestValue", MDCPropertyMap.get("key"));
}
+ @Test
+ public void nonSerializableParameters() throws Exception {
+ LoggingEvent event = createLoggingEvent();
+ LuckyCharms lucky0 = new LuckyCharms(0);
+ event.setArgumentArray(new Object[] {lucky0, null});
+ oos.writeObject(event);
+
+ // create the input stream based on the ouput stream
+ ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
+ inputStream = new ObjectInputStream(bis);
+
+ LoggingEvent remoteEvent = (LoggingEvent) inputStream.readObject();
+
+ Object[] aa = remoteEvent.getArgumentArray();
+ assertNotNull(aa);
+ assertEquals(2, aa.length);
+ assertEquals("LC(0)", aa[0]);
+ assertNull(aa[1]);
+ }
+
+
+
private LoggingEvent createLoggingEvent() {
LoggingEvent le = new LoggingEvent(this.getClass().getName(), logger,
Level.DEBUG, "test message", null, null);
Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/LuckyCharms.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/LuckyCharms.java Tue Mar 11 10:43:47 2008
@@ -0,0 +1,15 @@
+package ch.qos.logback.classic.spi;
+
+// non serializable object
+public class LuckyCharms {
+ int id;
+
+ LuckyCharms(int id) {
+ this.id= id;
+ }
+
+ @Override
+ public String toString() {
+ return "LC("+id+")";
+ }
+}
More information about the logback-dev
mailing list