FileDocCategorySizeDatePackage
StreamHandler.javaAPI DocJava SE 5 API9038Fri Aug 26 14:57:28 BST 2005java.util.logging

StreamHandler

public class StreamHandler extends Handler
Stream based logging Handler.

This is primarily intended as a base class or support class to be used in implementing other logging Handlers.

LogRecords are published to a given java.io.OutputStream.

Configuration: By default each StreamHandler is initialized using the following LogManager configuration properties. If properties are not defined (or have invalid values) then the specified default values are used.

  • java.util.logging.StreamHandler.level specifies the default level for the Handler (defaults to Level.INFO).
  • java.util.logging.StreamHandler.filter specifies the name of a Filter class to use (defaults to no Filter).
  • java.util.logging.StreamHandler.formatter specifies the name of a Formatter class to use (defaults to java.util.logging.SimpleFormatter).
  • java.util.logging.StreamHandler.encoding the name of the character set encoding to use (defaults to the default platform encoding).
version
1.18, 12/19/03
since
1.4

Fields Summary
private LogManager
manager
private OutputStream
output
private boolean
doneHeader
private Writer
writer
Constructors Summary
public StreamHandler()
Create a StreamHandler, with no current output stream.

	sealed = false;
	configure();
	sealed = true;
    
public StreamHandler(OutputStream out, Formatter formatter)
Create a StreamHandler with a given Formatter and output stream.

param
out the target output stream
param
formatter Formatter to be used to format output

	sealed = false;
	configure();
	setFormatter(formatter);
	setOutputStream(out);
	sealed = true;
    
Methods Summary
public synchronized voidclose()
Close the current output stream.

The Formatter's "tail" string is written to the stream before it is closed. In addition, if the Formatter's "head" string has not yet been written to the stream, it will be written before the "tail" string.

exception
SecurityException if a security manager exists and if the caller does not have LoggingPermission("control").
exception
SecurityException if a security manager exists and if the caller does not have LoggingPermission("control").

	flushAndClose();
    
private voidconfigure()


    // Private method to configure a StreamHandler from LogManager
    // properties and/or default values as specified in the class
    // javadoc.
       
        LogManager manager = LogManager.getLogManager();
	String cname = getClass().getName();

	setLevel(manager.getLevelProperty(cname +".level", Level.INFO));
	setFilter(manager.getFilterProperty(cname +".filter", null));
	setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()));
	try {
	    setEncoding(manager.getStringProperty(cname +".encoding", null));
	} catch (Exception ex) {
	    try {
	        setEncoding(null);
	    } catch (Exception ex2) {
		// doing a setEncoding with null should always work.
		// assert false;
	    }
	}
    
public synchronized voidflush()
Flush any buffered messages.

	if (writer != null) {
	    try {
	        writer.flush();
	    } catch (Exception ex) {	
	        // We don't want to throw an exception here, but we
	        // report the exception to any registered ErrorManager.
	        reportError(null, ex, ErrorManager.FLUSH_FAILURE);
	    }
	}
    
private synchronized voidflushAndClose()

        checkAccess();
	if (writer != null) {
	    try {
	        if (!doneHeader) {
	            writer.write(getFormatter().getHead(this));
		    doneHeader = true;
	        }
		writer.write(getFormatter().getTail(this));
		writer.flush();
	        writer.close();
	    } catch (Exception ex) {
	        // We don't want to throw an exception here, but we
	        // report the exception to any registered ErrorManager.
	        reportError(null, ex, ErrorManager.CLOSE_FAILURE);
	    }
	    writer = null;
	    output = null;
	}
    
public booleanisLoggable(java.util.logging.LogRecord record)
Check if this Handler would actually log a given LogRecord.

This method checks if the LogRecord has an appropriate level and whether it satisfies any Filter. It will also return false if no output stream has been assigned yet or the LogRecord is Null.

param
record a LogRecord
return
true if the LogRecord would be logged.

	if (writer == null || record == null) {
	    return false;
	}
	return super.isLoggable(record);
    
public synchronized voidpublish(java.util.logging.LogRecord record)
Format and publish a LogRecord.

The StreamHandler first checks if there is an OutputStream and if the given LogRecord has at least the required log level. If not it silently returns. If so, it calls any associated Filter to check if the record should be published. If so, it calls its Formatter to format the record and then writes the result to the current output stream.

If this is the first LogRecord to be written to a given OutputStream, the Formatter's "head" string is written to the stream before the LogRecord is written.

param
record description of the log event. A null record is silently ignored and is not published

	if (!isLoggable(record)) {
	    return;
	}
	String msg;
	try {
 	    msg = getFormatter().format(record);
	} catch (Exception ex) {
	    // We don't want to throw an exception here, but we
	    // report the exception to any registered ErrorManager.
	    reportError(null, ex, ErrorManager.FORMAT_FAILURE);
	    return;
	}

	try {
	    if (!doneHeader) {
	        writer.write(getFormatter().getHead(this));
		doneHeader = true;
	    }
	    writer.write(msg);
	} catch (Exception ex) {
	    // We don't want to throw an exception here, but we
	    // report the exception to any registered ErrorManager.
	    reportError(null, ex, ErrorManager.WRITE_FAILURE);
	}
    
public voidsetEncoding(java.lang.String encoding)
Set (or change) the character encoding used by this Handler.

The encoding should be set before any LogRecords are written to the Handler.

param
encoding The name of a supported character encoding. May be null, to indicate the default platform encoding.
exception
SecurityException if a security manager exists and if the caller does not have LoggingPermission("control").
exception
UnsupportedEncodingException if the named encoding is not supported.

	super.setEncoding(encoding);
	if (output == null) {
	    return;
	}
	// Replace the current writer with a writer for the new encoding.
	flush();
	if (encoding == null) {
	    writer = new OutputStreamWriter(output);
	} else {
	    writer = new OutputStreamWriter(output, encoding);
	}
    
protected synchronized voidsetOutputStream(java.io.OutputStream out)
Change the output stream.

If there is a current output stream then the Formatter's tail string is written and the stream is flushed and closed. Then the output stream is replaced with the new output stream.

param
out New output stream. May not be null.
exception
SecurityException if a security manager exists and if the caller does not have LoggingPermission("control").

	if (out == null) {
	    throw new NullPointerException();
	}
	flushAndClose();
	output = out;
	doneHeader = false;
	String encoding = getEncoding();
	if (encoding == null) {
	    writer = new OutputStreamWriter(output);
	} else {
	    try {
	        writer = new OutputStreamWriter(output, encoding);
	    } catch (UnsupportedEncodingException ex) {
		// This shouldn't happen.  The setEncoding method
		// should have validated that the encoding is OK.
		throw new Error("Unexpected exception " + ex);
	    }
	}