FileDocCategorySizeDatePackage
MessageLog.javaAPI DocExample2963Thu May 23 09:32:50 BST 2002 sample.utility

MessageLog

public class MessageLog extends Object
This is a utility class that writes messages to a log file that has the format DDMMMYYYY.LOG, where MMM is the month. For example: Mar is March, Apr is April, etc.

Fields Summary
private SimpleDateFormat
_timestampFormatter
private SimpleDateFormat
_filenameFormatter
private DecimalFormat
_decimalFormat
private StringBuffer
_filename
private StringBuffer
_message
private boolean
_includeThreadInfo
public static final boolean
INCLUDE_THREAD_INFO
public static final boolean
NO_INCLUDE_THREAD_INFO
Constructors Summary
public MessageLog()


       
        this(NO_INCLUDE_THREAD_INFO);
    
public MessageLog(boolean includeThreadInfo)

        _includeThreadInfo = includeThreadInfo;
    
Methods Summary
private java.lang.StringcreateFileName(java.util.Date now)

        _filename.setLength(0);
        _filename.append(_filenameFormatter.format(now));
        _filename.append(".LOG");
        return  _filename.toString();
    
private java.lang.StringcreateTimeStamp(java.util.Date now)

        return  _timestampFormatter.format(now);
    
public voidwrite(java.lang.String message)

        Date now = new Date();
        String filename = createFileName(now);
        //
        // Open the file (append output) for output
        //
        try {
            FileWriter outputFile = new FileWriter(filename, true);
            String timestamp = createTimeStamp(now);
            _message.setLength(0);
            _message.append(timestamp);
            _message.append(": ");
            if (_includeThreadInfo) {
                _message.append(Thread.currentThread().toString());
                _message.append(": ");
            }
            _message.append(message);
            _message.append('\n");
            ;
            outputFile.write(_message.toString());
            outputFile.close();
        } catch (IOException e) {
            System.out.println("MessageLog.write(): ERROR: IO Exception occurred.");
            e.printStackTrace();
        }
    
public voidwrite(java.lang.Throwable t)

        write("EXCEPTION: Stack trace follows...");
        try {
            Writer outputFile = new FileWriter(createFileName(new Date()), 
                    true);
            t.printStackTrace(new PrintWriter(outputFile));
            outputFile.flush();
            outputFile.close();
        } catch (IOException e) {
            System.out.println("MessageLog.write(): ERROR: IO Exception occurred.");
            e.printStackTrace();
        }