bb.util.logging
Class HandlerConsole

java.lang.Object
  extended by java.util.logging.Handler
      extended by bb.util.logging.HandlerAbstract
          extended by bb.util.logging.HandlerConsole

public class HandlerConsole
extends HandlerAbstract

Same as ConsoleHandler except that instead of always logging to System.err, this class only logs serious issues to System.err while non-serious issues get logged to System.out. The problem with ConsoleHandler always logging to System.err is that there may be monitoring code which looks at System.err and concludes that if anything appears, then it must signal a problem. This is obviously a mistake for many log records (typically Level.INFO and below logs are not problems).

It can be useful to attach an instance to the root Logger, and have it publish all logs.

It is unclear whether or not this class is multithread safe. There may be issues with its superclass; see HandlerAbstract for more discussion. Concerning the state added by this class, every method is synchronized.

Author:
Brent Boyer

Nested Class Summary
static class HandlerConsole.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Field Summary
private  StreamHandler errHandler
          Used to write serious logs.
private  Level levelSerious
          All LogRecords with this or a higher Level are considered to be "serious" log events by isSerious.
private  StreamHandler outHandler
          Used to write non-serious logs.
 
Constructor Summary
HandlerConsole()
          Convenience constructor.
HandlerConsole(Level levelSerious)
          Fundamental constructor.
 
Method Summary
 void close()
          If isAlive returns true, then calls super.close before calling setAlive(false).
 void flush()
           
 boolean isSerious(LogRecord record)
          Returns true if record's Level has at the same or higher value than levelSerious.
 void publish(LogRecord record)
          If isAlive or isLoggable(record) returns false, then immediately returns.
 void setEncoding(String encoding)
           
 void setErrorManager(ErrorManager errorManager)
           
 void setFilter(Filter filter)
           
 void setFormatter(Formatter formatter)
           
 void setLevel(Level level)
           
 
Methods inherited from class bb.util.logging.HandlerAbstract
configure, getEncodingDefault, getFilterDefault, getFilterProperty, getFormatterDefault, getFormatterProperty, getIntProperty, getLevelDefault, getLevelProperty, getStringProperty, isAlive, isLoggable, setAlive
 
Methods inherited from class java.util.logging.Handler
getEncoding, getErrorManager, getFilter, getFormatter, getLevel, reportError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

levelSerious

private Level levelSerious
All LogRecords with this or a higher Level are considered to be "serious" log events by isSerious.

If this field has the value Level.ALL (which is the lowest valued Level), then all LogRecords will be logged to System.err. Else if this field has the value Level.OFF (which is the highest valued Level), then all LogRecords will be logged to System.out. Else if this field has some other value, then the "high value" LogRecords will be logged to System.err and the "low value" LogRecords will be logged to System.out. For example, if this value is Level.WARNING, then (ignoring OFF LogRecords) only Level.SEVERE and WARNING LogRecords will be logged to System.err, while all others go to System.out).

Contract: is never null after construction, but is nulled out once and for all when close is first called.


outHandler

private StreamHandler outHandler
Used to write non-serious logs.

Contract: is never null after construction, but is nulled out once and for all when close is first called.


errHandler

private StreamHandler errHandler
Used to write serious logs.

Contract: is never null after construction, but is nulled out once and for all when close is first called.

Constructor Detail

HandlerConsole

public HandlerConsole()
Convenience constructor.

All this instance's initial configuration comes from the LogManager properties described in HandlerAbstract.configure, except for the levelSerious field which is assigned from calling getLevelProperty(getClass().getName() + ".levelSerious", Level.SEVERE). In other words, levelSerious is set to whatever is defined for it in the logging properties file, else it defaults to Level.SEVERE if not defined there.


HandlerConsole

public HandlerConsole(Level levelSerious)
               throws IllegalArgumentException
Fundamental constructor.

All this instance's initial configuration comes from the LogManager properties described in HandlerAbstract.configure, except for the levelSerious field which is assigned from the param.

Throws:
IllegalArgumentException - if levelSerious is null
Method Detail

close

public void close()
           throws SecurityException
Description copied from class: HandlerAbstract
If isAlive returns true, then calls super.close before calling setAlive(false).

Overrides:
close in class HandlerAbstract
Throws:
SecurityException

flush

public void flush()
Specified by:
flush in class Handler

publish

public void publish(LogRecord record)
If isAlive or isLoggable(record) returns false, then immediately returns. Else publishes record to System.err if isSerious(record) returns true, else publishes record to System.out.

Specified by:
publish in class Handler

setFormatter

public void setFormatter(Formatter formatter)
                  throws SecurityException
Overrides:
setFormatter in class Handler
Throws:
SecurityException

setEncoding

public void setEncoding(String encoding)
                 throws SecurityException,
                        UnsupportedEncodingException
Overrides:
setEncoding in class Handler
Throws:
SecurityException
UnsupportedEncodingException

setFilter

public void setFilter(Filter filter)
               throws SecurityException
Overrides:
setFilter in class Handler
Throws:
SecurityException

setErrorManager

public void setErrorManager(ErrorManager errorManager)
Overrides:
setErrorManager in class Handler

setLevel

public void setLevel(Level level)
              throws SecurityException
Overrides:
setLevel in class Handler
Throws:
SecurityException

isSerious

public boolean isSerious(LogRecord record)
Returns true if record's Level has at the same or higher value than levelSerious.