[logback-dev] svn commit: r1005 - in logback/trunk: logback-access/src/main/java/ch/qos/logback/access/jetty logback-access/src/main/java/ch/qos/logback/access/net logback-access/src/main/java/ch/qos/logback/access/spi logback-classic/src/main/java/ch/qos/logback/classic/net
noreply.seb at qos.ch
noreply.seb at qos.ch
Mon Nov 27 15:18:03 CET 2006
Author: seb
Date: Mon Nov 27 15:18:03 2006
New Revision: 1005
Added:
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SimpleSocketServer.java
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SocketNode.java
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/BasicContext.java
Modified:
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/jetty/RequestLogImpl.java
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SocketAppender.java
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/AccessEvent.java
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SocketServer.java
Log:
Working version of access's SocketAppender and SimpleSocketServer
Work in progress
Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/jetty/RequestLogImpl.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/jetty/RequestLogImpl.java (original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/jetty/RequestLogImpl.java Mon Nov 27 15:18:03 2006
@@ -164,7 +164,7 @@
}
RequestLogRegistry.register(this);
getStatusManager().add(
- new InfoStatus("RequestLog added to RequestLogMapper with name: "
+ new InfoStatus("RequestLog added to RequestLogRegistry with name: "
+ getName(), this));
} catch (JoranException e) {
Added: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SimpleSocketServer.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SimpleSocketServer.java Mon Nov 27 15:18:03 2006
@@ -0,0 +1,97 @@
+/**
+ * Logback: the reliable, generic, fast and flexible logging framework.
+ *
+ * Copyright (C) 1999-2006, QOS.ch
+ *
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.access.net;
+
+import java.net.ServerSocket;
+import java.net.Socket;
+
+import ch.qos.logback.access.joran.JoranConfigurator;
+import ch.qos.logback.access.spi.BasicContext;
+import ch.qos.logback.core.joran.spi.JoranException;
+import ch.qos.logback.core.util.StatusPrinter;
+
+/**
+ * A simple {@link SocketNode} based server.
+ *
+ * <pre>
+ * <b>Usage:</b> java ch.qos.logback.access.net.SimpleSocketServer port configFile
+ *
+ * where
+ * <em>
+ * port
+ * </em>
+ * is a part number where the server listens and
+ * <em>
+ * configFile
+ * </em>
+ * is an xml configuration file fed to {@link JoranConfigurator}.
+ * </pre>
+ *
+ * @author Ceki Gülcü
+ * @author Sébastien Pennec
+ *
+ * @since 0.8.4
+ */
+public class SimpleSocketServer {
+
+ static int port;
+
+ private static BasicContext basicContext;
+
+ public static void main(String argv[]) throws Exception {
+ if (argv.length == 2) {
+ init(argv[0], argv[1]);
+ } else {
+ usage("Wrong number of arguments.");
+ }
+
+ runServer();
+ }
+
+ static void runServer() {
+ try {
+ System.out.println("Listening on port " + port);
+ ServerSocket serverSocket = new ServerSocket(port);
+ while (true) {
+ System.out.println("Waiting to accept a new client.");
+ Socket socket = serverSocket.accept();
+ System.out.println("Connected to client at " + socket.getInetAddress());
+ System.out.println("Starting new socket node.");
+ new Thread(new SocketNode(socket, basicContext)).start();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ static void usage(String msg) {
+ System.err.println(msg);
+ System.err.println("Usage: java " + SimpleSocketServer.class.getName()
+ + " port configFile");
+ System.exit(1);
+ }
+
+ static void init(String portStr, String configFile) throws JoranException {
+ try {
+ port = Integer.parseInt(portStr);
+ } catch (java.lang.NumberFormatException e) {
+ e.printStackTrace();
+ usage("Could not interpret port number [" + portStr + "].");
+ }
+
+ basicContext = new BasicContext();
+ if (configFile.endsWith(".xml")) {
+ JoranConfigurator configurator = new JoranConfigurator();
+ configurator.setContext(basicContext);
+ configurator.doConfigure(configFile);
+ StatusPrinter.print(basicContext);
+ }
+ }
+}
Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SocketAppender.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SocketAppender.java (original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SocketAppender.java Mon Nov 27 15:18:03 2006
@@ -13,6 +13,7 @@
import java.net.InetAddress;
+import ch.qos.logback.access.spi.AccessEvent;
import ch.qos.logback.core.net.SocketAppenderBase;
/**
@@ -52,6 +53,8 @@
@Override
protected void postProcessEvent(Object event) {
+ AccessEvent ae = (AccessEvent)event;
+ ae.prepareForSerialization();
}
}
Added: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SocketNode.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SocketNode.java Mon Nov 27 15:18:03 2006
@@ -0,0 +1,86 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ *
+ * Copyright (C) 1999-2006, QOS.ch
+ *
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
+package ch.qos.logback.access.net;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.net.Socket;
+
+import ch.qos.logback.access.spi.AccessEvent;
+import ch.qos.logback.access.spi.BasicContext;
+import ch.qos.logback.core.spi.FilterReply;
+
+// Contributors: Moses Hohman <mmhohman at rainbow.uchicago.edu>
+
+/**
+ * Read {@link AccessEvent} objects sent from a remote client using Sockets
+ * (TCP). These logging events are logged according to local policy, as if they
+ * were generated locally.
+ *
+ * <p>
+ * For example, the socket node might decide to log events to a local file and
+ * also resent them to a second socket node.
+ *
+ * @author Ceki Gülcü
+ * @author Sébastien Pennec
+ *
+ * @since 0.8.4
+ */
+public class SocketNode implements Runnable {
+
+ Socket socket;
+ BasicContext context;
+ ObjectInputStream ois;
+
+ public SocketNode(Socket socket, BasicContext context) {
+ this.socket = socket;
+ this.context = context;
+ try {
+ ois = new ObjectInputStream(new BufferedInputStream(socket
+ .getInputStream()));
+ } catch (Exception e) {
+ System.out.println("Could not open ObjectInputStream to " + socket + e);
+ }
+ }
+
+ public void run() {
+ AccessEvent event;
+
+ try {
+ while (true) {
+ // read an event from the wire
+ event = (AccessEvent) ois.readObject();
+ //check that the event should be logged
+ if (context.getFilterChainDecision(event) == FilterReply.DENY) {
+ break;
+ }
+ //send it to the appenders
+ context.callAppenders(event);
+ }
+ } catch (java.io.EOFException e) {
+ System.out.println("Caught java.io.EOFException closing connection.");
+ } catch (java.net.SocketException e) {
+ System.out.println("Caught java.net.SocketException closing connection.");
+ } catch (IOException e) {
+ System.out.println("Caught java.io.IOException: " + e);
+ System.out.println("Closing connection.");
+ } catch (Exception e) {
+ System.out.println("Unexpected exception. Closing connection." + e);
+ }
+
+ try {
+ ois.close();
+ } catch (Exception e) {
+ System.out.println("Could not close connection." + e);
+ }
+ }
+}
Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/AccessEvent.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/AccessEvent.java (original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/AccessEvent.java Mon Nov 27 15:18:03 2006
@@ -1,6 +1,5 @@
package ch.qos.logback.access.spi;
-import java.io.InputStream;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
@@ -48,7 +47,7 @@
int statusCode = SENTINEL;
int localPort = SENTINEL;
- ServerAdapter serverAdapter;
+ transient ServerAdapter serverAdapter;
/**
* The number of milliseconds elapsed from 1/1/1970 until logging event was
@@ -293,12 +292,12 @@
return postContent;
}
- try {
- InputStream in = httpRequest.getInputStream();
- postContent = Util.readToString(in);
- } catch (Exception ex) {
- // do nothing
- }
+// try {
+// InputStream in = httpRequest.getInputStream();
+// postContent = Util.readToString(in);
+// } catch (Exception ex) {
+// // do nothing
+// }
if (postContent == null || postContent.length() == 0) {
postContent = NA;
}
@@ -319,4 +318,10 @@
public ServerAdapter getServerAdapter() {
return serverAdapter;
}
+
+ public void prepareForSerialization() {
+ getStatusCode();
+ getContentLength();
+ //getPostContent();
+ }
}
\ No newline at end of file
Added: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/BasicContext.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/BasicContext.java Mon Nov 27 15:18:03 2006
@@ -0,0 +1,73 @@
+package ch.qos.logback.access.spi;
+
+import java.util.Iterator;
+
+import ch.qos.logback.core.Appender;
+import ch.qos.logback.core.ContextBase;
+import ch.qos.logback.core.filter.Filter;
+import ch.qos.logback.core.spi.AppenderAttachable;
+import ch.qos.logback.core.spi.AppenderAttachableImpl;
+import ch.qos.logback.core.spi.FilterAttachable;
+import ch.qos.logback.core.spi.FilterAttachableImpl;
+import ch.qos.logback.core.spi.FilterReply;
+
+/**
+ * This class is a context that can be used
+ * by access to provide the basic functionnalities of a context
+ * to its components, mainly SocketServer.
+ *
+ * @author Sébastien Pennec
+ */
+public class BasicContext extends ContextBase implements AppenderAttachable, FilterAttachable {
+
+ AppenderAttachableImpl aai = new AppenderAttachableImpl();
+ FilterAttachableImpl fai = new FilterAttachableImpl();
+
+ public void callAppenders(AccessEvent event) {
+ aai.appendLoopOnAppenders(event);
+ }
+
+ public void addAppender(Appender newAppender) {
+ aai.addAppender(newAppender);
+ }
+
+ public void detachAndStopAllAppenders() {
+ aai.detachAndStopAllAppenders();
+ }
+
+ public boolean detachAppender(Appender appender) {
+ return aai.detachAppender(appender);
+ }
+
+ public Appender detachAppender(String name) {
+ return aai.detachAppender(name);
+ }
+
+ public Appender getAppender(String name) {
+ return aai.getAppender(name);
+ }
+
+ public boolean isAttached(Appender appender) {
+ return aai.isAttached(appender);
+ }
+
+ public Iterator iteratorForAppenders() {
+ return aai.iteratorForAppenders();
+ }
+
+ public void addFilter(Filter newFilter) {
+ fai.addFilter(newFilter);
+ }
+
+ public void clearAllFilters() {
+ fai.clearAllFilters();
+ }
+
+ public FilterReply getFilterChainDecision(Object event) {
+ return fai.getFilterChainDecision(event);
+ }
+
+ public Filter getFirstFilter() {
+ return fai.getFirstFilter();
+ }
+}
Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SocketServer.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SocketServer.java (original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SocketServer.java Mon Nov 27 15:18:03 2006
@@ -29,16 +29,16 @@
* client.
*
* <pre>
- * <b>Usage:</b> java ch.qos.logback.classic.net.SocketServer port configFile configDir
+ * <b>Usage:</b> java ch.qos.logback.classic.net.SocketServer port configFile configDir
*
- * where <b>port</b> is a part number where the server listens,
- * <b>configFile</b> is an xml configuration file fed to the {@link JoranConfigurator} and
- * <b>configDir</b> is a path to a directory containing configuration files, possibly one for each client host.
+ * where <b>port</b> is a part number where the server listens,
+ * <b>configFile</b> is an xml configuration file fed to the {@link JoranConfigurator} and
+ * <b>configDir</b> is a path to a directory containing configuration files, possibly one for each client host.
* </pre>
*
* <p>
- * The <code>configFile</code> is used to configure the log4j default
- * hierarchy that the <code>SocketServer</code> will use to report on its
+ * The <code>configFile</code> is used to configure the logback default
+ * context that the <code>SocketServer</code> will use to report on its
* actions.
*
* <p>
More information about the logback-dev
mailing list