FileDocCategorySizeDatePackage
SocketHubAppender.javaAPI DocApache log4j 1.2.1512097Sat Aug 25 00:09:40 BST 2007org.apache.log4j.net

SocketHubAppender

public class SocketHubAppender extends AppenderSkeleton
Sends {@link LoggingEvent} objects to a set of remote log servers, usually a {@link SocketNode SocketNodes}.

Acts just like {@link SocketAppender} except that instead of connecting to a given remote log server, SocketHubAppender accepts connections from the remote log servers as clients. It can accept more than one connection. When a log event is received, the event is sent to the set of currently connected remote log servers. Implemented this way it does not require any update to the configuration file to send data to another remote log server. The remote log server simply connects to the host and port the SocketHubAppender is running on.

The SocketHubAppender does not store events such that the remote side will events that arrived after the establishment of its connection. Once connected, events arrive in order as guaranteed by the TCP protocol.

This implementation borrows heavily from the {@link SocketAppender}.

The SocketHubAppender has the following characteristics:

  • If sent to a {@link SocketNode}, logging is non-intrusive as far as the log event is concerned. In other words, the event will be logged with the same time stamp, {@link org.apache.log4j.NDC}, location info as if it were logged locally.

  • SocketHubAppender does not use a layout. It ships a serialized {@link LoggingEvent} object to the remote side.

  • SocketHubAppender relies on the TCP protocol. Consequently, if the remote side is reachable, then log events will eventually arrive at remote client.

  • If no remote clients are attached, the logging requests are simply dropped.

  • Logging events are automatically buffered by the native TCP implementation. This means that if the link to remote client is slow but still faster than the rate of (log) event production, the application will not be affected by the slow network connection. However, if the network connection is slower then the rate of event production, then the local application can only progress at the network rate. In particular, if the network link to the the remote client is down, the application will be blocked.

    On the other hand, if the network link is up, but the remote client is down, the client will not be blocked when making log requests but the log events will be lost due to client unavailability.

    The single remote client case extends to multiple clients connections. The rate of logging will be determined by the slowest link.

  • If the JVM hosting the SocketHubAppender exits before the SocketHubAppender is closed either explicitly or subsequent to garbage collection, then there might be untransmitted data in the pipe which might be lost. This is a common problem on Windows based systems.

    To avoid lost data, it is usually sufficient to {@link #close} the SocketHubAppender either explicitly or by calling the {@link org.apache.log4j.LogManager#shutdown} method before exiting the application.

author
Mark Womack

Fields Summary
static final int
DEFAULT_PORT
The default port number of the ServerSocket will be created on.
private int
port
private Vector
oosList
private ServerMonitor
serverMonitor
private boolean
locationInfo
Constructors Summary
public SocketHubAppender()

  
     
public SocketHubAppender(int _port)
Connects to remote server at address and port.

    port = _port;
    startServer();
  
Methods Summary
public voidactivateOptions()
Set up the socket server on the specified port.

    startServer();
  
public voidappend(org.apache.log4j.spi.LoggingEvent event)
Append an event to all of current connections.

	// if no event or no open connections, exit now
    if(event == null || oosList.size() == 0)
      return;

    // set up location info if requested
    if (locationInfo) {
    	event.getLocationInformation();	
    } 

	// loop through the current set of open connections, appending the event to each
    for (int streamCount = 0; streamCount < oosList.size(); streamCount++) {    	

      ObjectOutputStream oos = null;
      try {
        oos = (ObjectOutputStream)oosList.elementAt(streamCount);
      }
      catch (ArrayIndexOutOfBoundsException e) {
        // catch this, but just don't assign a value
        // this should not really occur as this method is
        // the only one that can remove oos's (besides cleanUp).
      }
      
      // list size changed unexpectedly? Just exit the append.
      if (oos == null)
        break;
        
      try {
      	oos.writeObject(event);
      	oos.flush();
    	// Failing to reset the object output stream every now and
    	// then creates a serious memory leak.
    	// right now we always reset. TODO - set up frequency counter per oos?
    	oos.reset();
      }
      catch(IOException e) {
      	// there was an io exception so just drop the connection
      	oosList.removeElementAt(streamCount);
      	LogLog.debug("dropped connection");
      	
      	// decrement to keep the counter in place (for loop always increments)
      	streamCount--;
      }
    }
  
public voidcleanUp()
Release the underlying ServerMonitor thread, and drop the connections to all connected remote servers.

    // stop the monitor thread
	LogLog.debug("stopping ServerSocket");
    serverMonitor.stopMonitor();
    serverMonitor = null;
    
    // close all of the connections
	LogLog.debug("closing client connections");
    while (oosList.size() != 0) {
      ObjectOutputStream oos = (ObjectOutputStream)oosList.elementAt(0);
      if(oos != null) {
        try {
        	oos.close();
        }
        catch(IOException e) {
        	LogLog.error("could not close oos.", e);
        }
        
        oosList.removeElementAt(0);     
      }
    }
  
public synchronized voidclose()
Close this appender.

This will mark the appender as closed and call then {@link #cleanUp} method.

    if(closed)
      return;

	LogLog.debug("closing SocketHubAppender " + getName());
    this.closed = true;
    cleanUp();
	LogLog.debug("SocketHubAppender " + getName() + " closed");
  
public booleangetLocationInfo()
Returns value of the LocationInfo option.

    return locationInfo;
  
public intgetPort()
Returns value of the Port option.

    return port;
  
public booleanrequiresLayout()
The SocketHubAppender does not use a layout. Hence, this method returns false.

    return false;
  
public voidsetLocationInfo(boolean _locationInfo)
The LocationInfo option takes a boolean value. If true, the information sent to the remote host will include location information. By default no location information is sent to the server.

    locationInfo = _locationInfo;
  
public voidsetPort(int _port)
The Port option takes a positive integer representing the port where the server is waiting for connections.

    port = _port;
  
private voidstartServer()
Start the ServerMonitor thread.

    serverMonitor = new ServerMonitor(port, oosList);