Return-Path: Delivered-To: apmail-jakarta-avalon-dev-archive@jakarta.apache.org Received: (qmail 25702 invoked by uid 500); 16 Oct 2001 00:23:28 -0000 Mailing-List: contact avalon-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Reply-To: "Avalon Development" Delivered-To: mailing list avalon-dev@jakarta.apache.org Received: (qmail 25690 invoked from network); 16 Oct 2001 00:23:28 -0000 From: Subject: LogKit: Support for fail-safe operations To: avalon-dev@jakarta.apache.org Date: Tue, 16 Oct 2001 10:18:47 +1000 Message-ID: X-MIMETrack: Serialize by Router on HIC-CO-SMTP-1/SRV/HIC(Release 5.0.5 |September 22, 2000) at 16/10/2001 10:20:50 MIME-Version: 1.0 Content-type: text/plain; charset=us-ascii X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N 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