You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by "Sluis, Minto van der" <Mi...@isc.politie.nl> on 2004/12/23 15:53:31 UTC

XML based socket server

Hi all,
 
Originally I intended to use the Log4cxx. However I run into 
the compatibility issues regarding the wire protocol for the
SocketAppender. To get it working all I need is a XML based
socket server. But I will get to that later.
 
On my current project we want to have multiple windows clients (C++)
to log to a single log server. We also want ChainSaw to be able
to view the messages logged to this server. To prevent changing
the log properties for the server all the time, we intend to 
use the SocketHubExtender. However if the log server is based
on Log4cxx it not possible to get the messages into Chainsaw.
If the log server is based on Log4J the message will get to 
ChainSaw, but the client will not be able to get the message
into the Log server.
 
To circumvent these problems I intend to use a Log4J based log 
server that uses XML based sockets (XMLSocketServer). I guess
log4cxx will be able transfer log messages as it does for
Chainsaw.
 
I thought it would be easy to convert the SimpleSocketServer
to use the XMLSocketNode instead of the SocketNode. By the
way, what is the difference between SimpleSocketServer and 
SocketServer? As I mentioned I derived this new server from 
the SimpleSocketServer. Unfortunately it does not work. I get
the following exception:
 
    Exception constructing XML Socket Receiver
    java.lang.Exception: No receiver or decoder provided.  Cannot process
xml socket events
        at org.apache.log4j.net.XMLSocketNode.run(XMLSocketNode.java:111)
        at java.lang.Thread.run(Thread.java:534)
 
I provided the decoder like this:
 
    logger.info("Starting new xml socket node.");
    new Thread(new XMLSocketNode( XMLDecoder.class.getName(), socket,
LogManager.getLoggerRepository()))
    .start();

I am stuck. I have no clue what I did wrong. Is anyone able 
to help me out here?
 
regards and merry Xmas to you all,
 
Minto van der Sluis
 
PS. The complete SimpleXMLSocketServer is like this
 
 
/*
 * Copyright 1999,2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
<http://www.apache.org/licenses/LICENSE-2.0> 
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.joran.JoranConfigurator;
import org.apache.log4j.net.XMLSocketNode;
import org.apache.log4j.xml.XMLDecoder;
 
import java.net.ServerSocket;
import java.net.Socket;
 

public class SimpleXMLSocketServer {
  final static Logger logger =
Logger.getLogger(SimpleXMLSocketServer.class);
  static int port;
 
  public static void main(String[] argv) {
    if (argv.length == 2) {
      init(argv[0], argv[1]);
    } else {
      usage("Wrong number of arguments.");
    }
 
    try {
      logger.info("Listening on port " + port);
 
      ServerSocket serverSocket = new ServerSocket(port);
 
      while (true) {
        logger.info("Waiting to accept a new client.");
 
        Socket socket = serverSocket.accept();
        logger.info("Connected to client at " + socket.getInetAddress());
        logger.info("Starting new xml socket node.");
        new Thread(new XMLSocketNode( XMLDecoder.class.getName(), socket,
LogManager.getLoggerRepository()))
        .start();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
  static void usage(String msg) {
    System.err.println(msg);
    System.err.println(
      "Usage: java " + SimpleXMLSocketServer.class.getName() + " port
configFile");
    System.exit(1);
  }
 
  static void init(String portStr, String configFile) {
    try {
      port = Integer.parseInt(portStr);
    } catch (java.lang.NumberFormatException e) {
      e.printStackTrace();
      usage("Could not interpret port number [" + portStr + "].");
    }
 
    if (configFile.endsWith(".xml")) {
      JoranConfigurator jc = new JoranConfigurator();
      jc.doConfigure(configFile, LogManager.getLoggerRepository());
    } else {
      PropertyConfigurator.configure(configFile);
    }
  }
}