logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Edoardo Comar" <edoardo.co...@capeclear.com>
Subject SocketAppender proposed change to avoid losing events
Date Mon, 05 Nov 2001 14:24:40 GMT
Hi
I need that if a SocketAppender encounters a problem during
  public
  void append(LoggingEvent event) {

I could at least emit the event to the local console rather than losing
the event.


My suggestion is that the method

  public
  void append(LoggingEvent event) {
    if(event == null)
      return;

    if(address==null) {
      errorHandler.error("No remote host is set for SocketAppender named
\""+
			this.name+"\".");
      return;
    }

    if(oos != null) {
      try {
	if(locationInfo) {
	   event.getLocationInformation();	
	} 
	oos.writeObject(event);
	//LogLog.debug("=========Flushing.");
	oos.flush();
	if(++counter >= RESET_FREQUENCY) {
	  counter = 0;
	  // Failing to reset the object output stream every now and
	  // then creates a serious memory leak.
	  //System.err.println("Doing oos.reset()");
	  oos.reset();
	}
      }
      catch(IOException e) {
	oos = null;
	LogLog.warn("Detected problem with connection: "+e);
	if(reconnectionDelay > 0) {
	  fireConnector();
	}
      }
    }
  }

Could be changed as 

  public
  void append(LoggingEvent event) {
    if(event == null)
      return;

    if(address==null) {
      errorHandler.error("No remote host is set for SocketAppender named
\""+
			this.name+"\".");
      return;
    }

    if(oos != null) {
      try {
	if(locationInfo) {
	   event.getLocationInformation();	
	} 
	oos.writeObject(event);
	//LogLog.debug("=========Flushing.");
	oos.flush();
	if(++counter >= RESET_FREQUENCY) {
	  counter = 0;
	  // Failing to reset the object output stream every now and
	  // then creates a serious memory leak.
	  //System.err.println("Doing oos.reset()");
	  oos.reset();
	}
      }
      catch(IOException e) {
	oos = null;
	//LogLog.warn("Detected problem with connection: "+e);
      errorHandler.error("Detected problem with connection", e, 0);
//ADDED
	if(reconnectionDelay > 0) {
	  fireConnector();
	}
      }
    } else { //ADDED else block
        errorHandler.error("No output stream available for
SocketAppender named \""+
              this.name+"\".");
        return;
    }
  }


This way I could easily extend the SocketAppender which 
Instantiates an error handler which could be checked for errors

and in SockeAppenderExtended
append(LoggingEvent ) can be overridden 
1- synchronized
2 - calls super.append(event)
3 - then error handler is checked and if it has any errors  it is reset
and the fallback appender is called to handle the event


Regards,
Edo


--
To unsubscribe, e-mail:   <mailto:log4j-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:log4j-dev-help@jakarta.apache.org>


Mime
View raw message