You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avalon.apache.org by da...@hic.gov.au on 2001/10/16 02:18:47 UTC

LogKit: Support for fail-safe operations

I have been looking for a logging framework which will notify the client
application
when logging fails (fail-safe). I am seeking confirmation that Logkit can
achieve this.
Having looked at the source code, Logkit defaults to using
DefaultErrorHandler
which does not, by itself, ensure fail-safe operations.

However, if I subclass DefaultErrorHandler and make the error method throw
a
run-time exception, this should then allow any client class to detect the
failure
and take appropriate action. Naturally, the client will need to instantiate
a Hierarchy object that sets the ErrorHandler.

My question is - this seems very easy to do but will this work?

==== Source Code follows FailSafeErrorHandler.java
====

import org.apache.log.util.DefaultErrorHandler;
import org.apache.log.LogEvent;
/**
 * Insert the type's description here.
 * Creation date: (11/10/2001 17:19:59)
 * @author: David Gray
 */
public class FailSafeErrorHandler extends DefaultErrorHandler {
public FailSafeErrorHandler () {
     super();
}
public void error(
   final String message,
   final Throwable throwable,
   final LogEvent event) {
   super.error(message, throwable, event);
   throw new LoggingException(message);
}
}

===== I have defined the class LoggingException so that clients can specifically catch it.


/**
 * Insert the type's description here.
 * Creation date: (12/10/2001 9:03:18)
 * @author: David Gray
 */
public class LoggingException extends RuntimeException {
/**
 * LoggingException constructor comment.
 */
public LoggingException() {
     super();
}
/**
 * LoggingException constructor comment.
 * @param s java.lang.String
 */
public LoggingException(String s) {
     super(s);
}
}

===== I Tested this setup with the following code (under jUnit hence the call to fail() ). Note, that the code
creates an instance of the Hierarchy Class:

public void testHICErrorHandler() {
   FailSafeErrorHandler hicErrorHandler = new FailSafeErrorHandler();
   Hierarchy h = new Hierarchy();
   h.setErrorHandler(hicErrorHandler);
   Logger logger = h.getLoggerFor("mySafeCategory");
   logger.setPriority(Priority.DEBUG);
   PriorityFilter filter = new PriorityFilter(Priority.ERROR);
   final String pattern =
      "%7.7{priority} %5.5{time}   [%8.8{category}] "
         + "(%{context}): %{message}\\n%{throwable}";
   final PatternFormatter formatter = new PatternFormatter(pattern);
   final File file = new File("C:/temp/log.txt");
   try {
      // deleting the file ensures that the new file is
      // write enabled.
     file.delete();
     // Note - using SafeFileTarget ensures that file is opened and closed for
     // each log movement hence the write-protect can actually force an error
    SafeFileTarget target = new SafeFileTarget(file, true, formatter);
      //Set log targets of logger
      logger.setLogTargets(new LogTarget[] { target, filter });
      try {
         // The following will work
         logger.debug("This is a debug message to " + this.getName());
         file.setReadOnly();
         // If here we write-protect the file the following throws
         // the LoggingException message.
         logger.debug("This is a info message to " + this.getName());
         fail("Was able to write a message to a write-only file");
      }
      catch (LoggingException le) {
         // This should occur
      }
   }
   catch (IOException e) {
      fail("Unable to set a log file - check if read-only " + e.getMessage());
   }
}



****************************************************************
NOTICE - This message is intended only for the use of the 
addressee named above and may contain privileged and 
confidential information.  If you are not the intended recipient
of this message you are hereby notified that you must not 
disseminate, copy or take any action based upon it.  If you 
received this message in error please notify HIC immediately.
Any views expressed in this message are those of the individual
sender, except where the sender specifically states them to be
the views of HIC.
****************************************************************

---------------------------------------------------------------------
To unsubscribe, e-mail: avalon-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: avalon-dev-help@jakarta.apache.org


Re: LogKit: Support for fail-safe operations

Posted by Peter Donald <do...@apache.org>.
On Tue, 16 Oct 2001 10:18, david.gray@hic.gov.au wrote:
> I have been looking for a logging framework which will notify the client
> application
> when logging fails (fail-safe). I am seeking confirmation that Logkit can
> achieve this.
> Having looked at the source code, Logkit defaults to using
> DefaultErrorHandler
> which does not, by itself, ensure fail-safe operations.

yep.

> However, if I subclass DefaultErrorHandler and make the error method throw
> a
> run-time exception, this should then allow any client class to detect the
> failure
> and take appropriate action. Naturally, the client will need to instantiate
> a Hierarchy object that sets the ErrorHandler.
>
> My question is - this seems very easy to do but will this work?

It *should* in all (most?) cases. The only times where it will not work is 
when a LogTarget is not written correctly or has bugs. I think that all the 
Targets in current CVS should work fine in your situation though it is not 
something I have tried before.

-- 
Cheers,

Pete

----------------------------------------------------------
Which is worse: Ignorance or Apathy? Who knows? Who cares?
----------------------------------------------------------

---------------------------------------------------------------------
To unsubscribe, e-mail: avalon-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: avalon-dev-help@jakarta.apache.org