SOURCECODE

How to... log the opening and closing of a log file


Description:
It might be useful to log events like the opening or closing of a log file.
To be able to log these events, Log already has the two methods protected void logOpenLog() and protected void logCloseLog(), which by default are empty. In order to have the two events logged, you have to make these methods put the right LogEntry into the desired Log.
This might sound familiar to "Howto log the opening and closing of a SalesPoint", which it in deed is, so this example also shows how to extend Log, implement the Loggable and extend the LogEntry in one class, so everything is there when needed. For basic information on this topic, please refer to the Howto mentioned before.

ToDo's:
  1. Subclass Log
  2. Don't forget to hand over the Outputstream to the super class in order to create or append to the log.
  3. Implement the method logOpenLog() to log the opening of the logfile
  4. Catch the two exceptions thrown by log(Loggable l)
  5. Implement the needed Loggable within the log method
  6. Subclass the LogEntry within the implementation of the getLogData() method
  7. Overwrite the toString() method of the LogEntry subclass

To log the closing of a log file, do the same on the logCloseLog() method.



SourceCode

import log.*;
import java.io.*;

 1
//to get the all-in-one wonder, subclass Log
public class LogLog extends Log {

     2
    //hand the OutputStream to the super class
    public LogLog(OutputStream os) {
        super(os);
    }

     3
    //implement the logOpenLog() method
    protected void logOpenLog() {

         4
        //catch the two Exceptions thrown by log(Loggable l)
        try {

                 5
                //here an implementation of Loggable is being needed
            log(new Loggable() {
    
                 6
                //implement the method for Loggable
                public LogEntry getLogData() {

                    //and for that, extend the LogEntry class
                    return new LogEntry() {
    
                         7
                        //to suit our log Description
                        public String toString() {
                            return "Log file opened";
                        }
                    };
                }
            });

        } catch (LogNoOutputStreamException lnose) {
            System.out.println(lnose);

        } catch (IOException ie) {
            System.out.println(ie);
        }
    }
}