[LOGBack-dev] svn commit: r486 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic/net main/java/ch/qos/logback/classic/spi test/java/ch/qos/logback/classic/net
noreply.seb at qos.ch
noreply.seb at qos.ch
Tue Aug 29 15:00:12 CEST 2006
Author: seb
Date: Tue Aug 29 15:00:10 2006
New Revision: 486
Modified:
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SimpleSocketServer.java
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SocketAppender.java
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SocketNode.java
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java
logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SocketAppenderTestApp.java
Log:
on going work on SocketAppender
Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SimpleSocketServer.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SimpleSocketServer.java (original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SimpleSocketServer.java Tue Aug 29 15:00:10 2006
@@ -22,17 +22,17 @@
* A simple {@link SocketNode} based server.
*
* <pre>
- * <b>Usage:</b> java ch.qos.logback.classic.net.SimpleSocketServer port configFile
- *
- * where
+ * <b>Usage:</b> java ch.qos.logback.classic.net.SimpleSocketServer port configFile
+ *
+ * where
* <em>
* port
* </em>
- * is a part number where the server listens and
+ * is a part number where the server listens and
* <em>
* configFile
* </em>
- * is an xml configuration file fed to {@link JoranConfigurator}.
+ * is an xml configuration file fed to {@link JoranConfigurator}.
* </pre>
*
* @author Ceki Gülcü
@@ -53,6 +53,10 @@
usage("Wrong number of arguments.");
}
+ runServer();
+ }
+
+ static void runServer() {
try {
logger.info("Listening on port " + port);
ServerSocket serverSocket = new ServerSocket(port);
@@ -61,9 +65,8 @@
Socket socket = serverSocket.accept();
logger.info("Connected to client at " + socket.getInetAddress());
logger.info("Starting new socket node.");
- LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
- new Thread(new SocketNode(socket, lc))
- .start();
+ LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
+ new Thread(new SocketNode(socket, lc)).start();
}
} catch (Exception e) {
e.printStackTrace();
@@ -86,7 +89,7 @@
}
if (configFile.endsWith(".xml")) {
- LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
+ LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(lc);
configurator.doConfigure(configFile);
Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SocketAppender.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SocketAppender.java (original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SocketAppender.java Tue Aug 29 15:00:10 2006
@@ -15,7 +15,6 @@
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
-import java.util.Calendar;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.AppenderBase;
@@ -139,7 +138,7 @@
this.address = address;
this.remoteHost = address.getHostName();
this.port = port;
- //connect(address, port);
+ // connect(address, port);
}
/**
@@ -149,15 +148,15 @@
this.port = port;
this.address = getAddressByName(host);
this.remoteHost = host;
- //connect(address, port);
+ // connect(address, port);
}
- // /**
- // * Connect to the specified <b>RemoteHost</b> and <b>Port</b>.
- // */
- // public void activateOptions() {
- // connect(address, port);
- // }
+ // /**
+ // * Connect to the specified <b>RemoteHost</b> and <b>Port</b>.
+ // */
+ // public void activateOptions() {
+ // connect(address, port);
+ // }
/**
* Start this appender.
@@ -226,7 +225,7 @@
oos = new ObjectOutputStream(new Socket(address, port).getOutputStream());
} catch (IOException e) {
- String msg = "Could not connect to remote log4j server at ["
+ String msg = "Could not connect to remote logback server at ["
+ address.getHostName() + "].";
if (reconnectionDelay > 0) {
msg += " We will try again later.";
@@ -236,6 +235,9 @@
}
}
+
+ int count = 0;
+ long total = 0;
@Override
protected void append(Object event) {
@@ -250,11 +252,13 @@
if (oos != null) {
try {
- Long t1 = Calendar.getInstance().getTimeInMillis();
+ Long t1 = System.nanoTime();
oos.writeObject(event);
- Long t2 = Calendar.getInstance().getTimeInMillis();
- addInfo("=========Writing time: " + Long.toString(t2-t1));
- addInfo("=========Flushing.");
+ Long t2 = System.nanoTime();
+ long delta = t2-t1;
+ total += delta;
+ addInfo("** Writing time: " + Long.toString(delta) + " total: " + ++count + " median: " + total/count);
+ //addInfo("=========Flushing.");
oos.flush();
if (++counter >= RESET_FREQUENCY) {
counter = 0;
@@ -293,14 +297,6 @@
}
/**
- * The SocketAppender does not use a layout. Hence, this method returns
- * <code>false</code>.
- */
- public boolean requiresLayout() {
- return false;
- }
-
- /**
* The <b>RemoteHost</b> option takes a string value which should be the host
* name of the server where a {@link SocketNode} is running.
*/
Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SocketNode.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SocketNode.java (original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SocketNode.java Tue Aug 29 15:00:10 2006
@@ -80,14 +80,14 @@
}
}
} catch (java.io.EOFException e) {
- logger.info("Caught java.io.EOFException closing conneciton.");
+ logger.info("Caught java.io.EOFException closing connection.");
} catch (java.net.SocketException e) {
- logger.info("Caught java.net.SocketException closing conneciton.");
+ logger.info("Caught java.net.SocketException closing connection.");
} catch (IOException e) {
logger.info("Caught java.io.IOException: " + e);
logger.info("Closing connection.");
} catch (Exception e) {
- logger.error("Unexpected exception. Closing conneciton.", e);
+ logger.error("Unexpected exception. Closing connection.", e);
}
try {
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 Aug 29 15:00:10 2006
@@ -10,10 +10,13 @@
package ch.qos.logback.classic.spi;
-
-import java.io.Serializable;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import ch.qos.logback.classic.Level;
@@ -22,219 +25,253 @@
* The internal representation of logging events. When an affirmative decision
* is made to log then a <code>LoggingEvent</code> instance is created. This
* instance is passed around to the different Logback components.
- *
- * <p>Writers of Logback components such as appenders should be
- * aware of that some of the LoggingEvent fields are initialized lazily.
- * Therefore, an appender wishing to output data to be later correctly read
- * by a receiver, must initialize "lazy" fields prior to writing them out.
- * See the {@link #prepareForDeferredProcessing()} method for the exact list.</p>
+ *
+ * <p>
+ * Writers of Logback components such as appenders should be aware of that some
+ * of the LoggingEvent fields are initialized lazily. Therefore, an appender
+ * wishing to output data to be later correctly read by a receiver, must
+ * initialize "lazy" fields prior to writing them out. See the
+ * {@link #prepareForDeferredProcessing()} method for the exact list.
+ * </p>
*
* @author Ceki Gülcü
*/
-public class LoggingEvent implements Serializable {
+public class LoggingEvent implements Externalizable {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 3022264832697160750L;
+
+ /**
+ *
+ */
+ private static long startTime = System.currentTimeMillis();
+
+ /**
+ * Fully qualified name of the calling Logger class. This field does not
+ * survive serialization.
+ *
+ * <p>
+ * Note that the getCallerInformation() method relies on this fact.
+ */
+ transient String fqnOfLoggerClass;
+
+ /**
+ * The name of thread in which this logging event was generated.
+ */
+ private String threadName;
-
- /**
+ /**
+ * Level of logging event.
+ *
+ * <p>
+ * This field should not be accessed directly. You shoud use the {@link
+ * #getLevel} method instead.
+ * </p>
*
*/
- private static final long serialVersionUID = -7298433437463204531L;
+ private Level level;
+
+ private String message;
+ private Object[] argumentArray;
+
+ private Logger logger;
+
+ private ThrowableInformation throwableInfo;
+
+ private CallerData[] callerDataArray;
+
+ private Marker marker;
/**
- *
- */
- private static long startTime = System.currentTimeMillis();
-
- /**
- * Fully qualified name of the calling Logger class. This field does not
- * survive serialization.
- *
- * <p>Note that the getCallerInformation() method relies on this fact.
- */
- transient String fqnOfLoggerClass;
-
-
- /**
- * The name of thread in which this logging event was generated.
- */
- private String threadName;
-
- /**
- * Level of logging event.
- *
- * <p>
- * This field should not be accessed directly. You shoud use the {@link
- * #getLevel} method instead.
- * </p>
- *
- */
- private Level level;
-
- private String message;
- private Object[] argumentArray;
-
- private Logger logger;
-
- private ThrowableInformation throwableInfo;
-
- private CallerData[] callerDataArray;
-
- private Marker marker;
-
- /**
- * The number of milliseconds elapsed from 1/1/1970 until logging event was
- * created.
- */
- private long timeStamp;
-
- public LoggingEvent(String fqcn, Logger logger, Level level, String message, Throwable throwable) {
- this.fqnOfLoggerClass = fqcn;
- this.logger = logger;
- this.level = level;
- this.message = message;
-
- if (throwable != null) {
- this.throwableInfo = new ThrowableInformation(throwable);
- }
- timeStamp = System.currentTimeMillis();
- }
-
- public void setArgumentArray(Object[] argArray) {
- if (this.argumentArray != null) {
- throw new IllegalStateException("argArray has been already set");
- }
- this.argumentArray = argArray;
- }
-
- public Object[] getArgumentArray() {
- return this.argumentArray;
- }
-
- public Level getLevel() {
- return level;
- }
-
- public String getThreadName() {
- if (threadName == null) {
- threadName = (Thread.currentThread()).getName();
- }
- return threadName;
- }
-
- /**
- * @param threadName The threadName to set.
- * @throws IllegalStateException If threadName has been already set.
- */
- public void setThreadName(String threadName)
- throws IllegalStateException {
- if (this.threadName != null) {
- throw new IllegalStateException("threadName has been already set");
- }
- this.threadName = threadName;
- }
-
- /**
- * Returns the throwable information contained within this event. May be
- * <code>null</code> if there is no such information.
- */
- public ThrowableInformation getThrowableInformation() {
- return throwableInfo;
- }
-
- /**
- * Set this event's throwable information.
- */
- public void setThrowableInformation(ThrowableInformation ti) {
- if (throwableInfo != null) {
- throw new IllegalStateException("ThrowableInformation has been already set.");
- } else {
- throwableInfo = ti;
- }
- }
-
- /**
- * This method should be called prior to serializing an event. It should also
- * be called when using asynchronous logging.
- */
- public void prepareForDeferredProcessing() {
- this.getThreadName();
- }
-
- public Logger getLogger() {
- return logger;
- }
-
- public void setLogger(Logger logger) {
- this.logger = logger;
- }
-
- public String getMessage() {
- return message;
- }
-
- public void setMessage(String message) {
- if (this.message != null) {
- throw new IllegalStateException("The message for this event has been set already.");
- }
- this.message = message;
- }
-
- public long getTimeStamp() {
- return timeStamp;
- }
-
- public void setTimeStamp(long timeStamp) {
- this.timeStamp = timeStamp;
- }
-
- public void setLevel(Level level) {
- if (this.level != null) {
- throw new IllegalStateException("The level has been already set for this event.");
- }
- this.level = level;
- }
-
- /**
- * The time at which this class was loaded into memory, expressed in millisecond
- * elapsed since the epoch (1.1.1970).
- *
- * @return The time as measured when this class was loaded into memory.
- */
- public static final long getStartTime() {
- return startTime;
- }
-
- /**
- * Get the caller information for this logging event. If caller
- * information is null at the time of its invocation, this method extracts
- * location information. The collected information is cached for future use.
- *
- * <p>Note that after serialization it is impossible to correctly extract
- * caller information. </p>
- */
- public CallerData[] getCallerData() {
- // we rely on the fact that fqnOfLoggerClass does not survive
- // serialization
- if (callerDataArray == null && fqnOfLoggerClass != null) {
- callerDataArray = CallerData.extract(new Throwable(), fqnOfLoggerClass);
- }
- return callerDataArray;
- }
-
- public void setCallerInformation(CallerData[] callerDataArray) {
- this.callerDataArray = callerDataArray;
- }
-
- public Marker getMarker() {
- return marker;
- }
-
- public void setMarker(Marker marker) {
- if (this.marker != null) {
- throw new IllegalStateException("The marker has been already set for this event.");
- }
- this.marker = marker;
- }
-
-
+ * The number of milliseconds elapsed from 1/1/1970 until logging event was
+ * created.
+ */
+ private long timeStamp;
+
+ public LoggingEvent() {
+ }
+
+ public LoggingEvent(String fqcn, Logger logger, Level level, String message,
+ Throwable throwable) {
+ this.fqnOfLoggerClass = fqcn;
+ this.logger = logger;
+ this.level = level;
+ this.message = message;
+
+ if (throwable != null) {
+ this.throwableInfo = new ThrowableInformation(throwable);
+ }
+ timeStamp = System.currentTimeMillis();
+ }
+
+ public void setArgumentArray(Object[] argArray) {
+ if (this.argumentArray != null) {
+ throw new IllegalStateException("argArray has been already set");
+ }
+ this.argumentArray = argArray;
+ }
+
+ public Object[] getArgumentArray() {
+ return this.argumentArray;
+ }
+
+ public Level getLevel() {
+ return level;
+ }
+
+ public String getThreadName() {
+ if (threadName == null) {
+ threadName = (Thread.currentThread()).getName();
+ }
+ return threadName;
+ }
+
+ /**
+ * @param threadName
+ * The threadName to set.
+ * @throws IllegalStateException
+ * If threadName has been already set.
+ */
+ public void setThreadName(String threadName) throws IllegalStateException {
+ if (this.threadName != null) {
+ throw new IllegalStateException("threadName has been already set");
+ }
+ this.threadName = threadName;
+ }
+
+ /**
+ * Returns the throwable information contained within this event. May be
+ * <code>null</code> if there is no such information.
+ */
+ public ThrowableInformation getThrowableInformation() {
+ return throwableInfo;
+ }
+
+ /**
+ * Set this event's throwable information.
+ */
+ public void setThrowableInformation(ThrowableInformation ti) {
+ if (throwableInfo != null) {
+ throw new IllegalStateException(
+ "ThrowableInformation has been already set.");
+ } else {
+ throwableInfo = ti;
+ }
+ }
+
+ /**
+ * This method should be called prior to serializing an event. It should also
+ * be called when using asynchronous logging.
+ */
+ public void prepareForDeferredProcessing() {
+ this.getThreadName();
+ }
+
+ public Logger getLogger() {
+ return logger;
+ }
+
+ public void setLogger(Logger logger) {
+ this.logger = logger;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public void setMessage(String message) {
+ if (this.message != null) {
+ throw new IllegalStateException(
+ "The message for this event has been set already.");
+ }
+ this.message = message;
+ }
+
+ public long getTimeStamp() {
+ return timeStamp;
+ }
+
+ public void setTimeStamp(long timeStamp) {
+ this.timeStamp = timeStamp;
+ }
+
+ public void setLevel(Level level) {
+ if (this.level != null) {
+ throw new IllegalStateException(
+ "The level has been already set for this event.");
+ }
+ this.level = level;
+ }
+
+ /**
+ * The time at which this class was loaded into memory, expressed in
+ * millisecond elapsed since the epoch (1.1.1970).
+ *
+ * @return The time as measured when this class was loaded into memory.
+ */
+ public static final long getStartTime() {
+ return startTime;
+ }
+
+ /**
+ * Get the caller information for this logging event. If caller information is
+ * null at the time of its invocation, this method extracts location
+ * information. The collected information is cached for future use.
+ *
+ * <p>
+ * Note that after serialization it is impossible to correctly extract caller
+ * information.
+ * </p>
+ */
+ public CallerData[] getCallerData() {
+ // we rely on the fact that fqnOfLoggerClass does not survive
+ // serialization
+ if (callerDataArray == null && fqnOfLoggerClass != null) {
+ callerDataArray = CallerData.extract(new Throwable(), fqnOfLoggerClass);
+ }
+ return callerDataArray;
+ }
+
+ public void setCallerInformation(CallerData[] callerDataArray) {
+ this.callerDataArray = callerDataArray;
+ }
+
+ public Marker getMarker() {
+ return marker;
+ }
+
+ public void setMarker(Marker marker) {
+ if (this.marker != null) {
+ throw new IllegalStateException(
+ "The marker has been already set for this event.");
+ }
+ this.marker = marker;
+ }
+
+ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ threadName = (String)in.readObject();
+ message = (String) in.readObject();
+ int levelInt = in.readInt();
+ level = Level.toLevel(levelInt);
+ String loggerName = (String)in.readObject();
+ logger = LoggerFactory.getLogger(loggerName);
+ }
+
+ public void writeExternal(ObjectOutput out) throws IOException {
+ if (threadName != null) {
+ out.writeObject(threadName);
+ } else {
+ out.writeObject("noThreadName");
+ }
+ out.writeObject(message);
+ out.writeInt(level.levelInt);
+ out.writeObject(logger.getName());
+
+ // out.writeObject(throwableInfo);
+ // out.writeObject(callerDataArray);
+ // out.writeObject(marker);
+ }
}
Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SocketAppenderTestApp.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SocketAppenderTestApp.java (original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SocketAppenderTestApp.java Tue Aug 29 15:00:10 2006
@@ -23,8 +23,7 @@
for (int i = 0; i <= 1000; i++) {
logger.debug("** Hello world. n=" + i);
}
-
-
+
StatusPrinter.print(lc.getStatusManager());
}
More information about the logback-dev
mailing list