Holger's
Java API

com.antelmann.util.logging
Class Logger

java.lang.Object
  extended by com.antelmann.util.logging.Logger
All Implemented Interfaces:
Filter<LogEntry>

public class Logger
extends Object
implements Filter<LogEntry>

Logger somewhat leans on the functionality of the java.util.logging functionality of J2SE 1.4, but it's not quite the same (and was implemented before J2SE 1.4).

In addition to the J2SE functionality, it provides the full stack trace for the log. Whether or not the logging here is written synchronously or asynchronously, is left to the implementation for each LogWriter that is used with the Logger.

All logging methods may throw LogException - propagated from the LogWriter objects contained in the logger to the calling thread. The writing to the different LogWriters is always performed sequentially in the order the LogWriters were added to the Logger.

It is recommended to always use a log method with the origin parameter and pass the 'this' argument from the calling function; this will provide useful additional information for the log.

Author:
Holger Antelmann
See Also:
LogWriter, LogEntry, LogException

Field Summary
protected  ArrayList<LogWriter> writers
           
 
Constructor Summary
Logger()
          creates an empty Logger that does not write to any log yet
Logger(Logger logger)
          uses the configuration from the given logger (including writers and filter)
Logger(LogWriter... handler)
           
 
Method Summary
 boolean accept(LogEntry entry)
          returns true only if the given entry will be logged by this logger and thus propagated to its handlers
 boolean addWriter(LogWriter writer)
          adds the given handler to the list of handlers this Logger writes to
 void addWritersFromLogger(Logger logger)
          adds all LogWriter handler from the given logger
 void clearLast()
          sets the last logged entry to null
 boolean containsWriter(LogWriter writer)
           
 LogExceptionHandler getExceptionHandler()
          if the LogExceptionHandler is null (which it is by default), any LogException during logging is propagated to the calling application.
 Filter<LogEntry> getFilter()
          returns null by default
 LogEntry getLast()
          returns the last entry logged with this logger (while enabled and filter passed)
 LogWriter[] getWriters()
           
 void ignoreLogExceptions()
          sets a LogExceptionHandler that simply ignores any given LogException, so no Exception will be propagated to the calling application if logging fails.
 boolean includesStack()
          returns true if a stack trace is produced when a Logger method creates a LogEntry (false by default)
 boolean isEnabled()
          the logger only logs if the return value is true (which it is by default)
 void log(Level level, String message, long time, String sourceClass, String sourceString, Throwable thrown, String threadName, StackTraceElement[] stack, Object[] parameters)
           
 void log(Level level, String message, Object... parameters)
           
 void log(LogEntry entry)
          logs the entry by writing to the writers after checking whether the logger is enabled and the filter (if available) accepts the entry.
 void log(Object origin, Level level, String message, Object... parameters)
           
 void log(Object origin, Level level, String message, Throwable thrown, Object... parameters)
           
 void log(Object origin, String message, Object... parameters)
           
 void log(Object origin, Throwable thrown)
           
 void log(String message, Object... parameters)
           
 void log(String message, Throwable thrown, Object... parameters)
           
 void log(Throwable thrown)
           
 void removeAllWriters()
           
 boolean removeWriter(LogWriter writer)
          removes the given writer from the list of log writers
 void setEnabled(boolean flag)
           
 void setExceptionHandler(LogExceptionHandler handler)
           
 void setFilter(Filter<LogEntry> filter)
           
 void setIncludeStack(boolean on)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

writers

protected ArrayList<LogWriter> writers
Constructor Detail

Logger

public Logger()
creates an empty Logger that does not write to any log yet


Logger

public Logger(LogWriter... handler)

Logger

public Logger(Logger logger)
uses the configuration from the given logger (including writers and filter)

Method Detail

getWriters

public LogWriter[] getWriters()

addWritersFromLogger

public void addWritersFromLogger(Logger logger)
adds all LogWriter handler from the given logger


includesStack

public boolean includesStack()
returns true if a stack trace is produced when a Logger method creates a LogEntry (false by default)


setIncludeStack

public void setIncludeStack(boolean on)

addWriter

public boolean addWriter(LogWriter writer)
adds the given handler to the list of handlers this Logger writes to

Returns:
true if the set did not already contain the writer

removeWriter

public boolean removeWriter(LogWriter writer)
removes the given writer from the list of log writers

Returns:
true if the set contained the handler

containsWriter

public boolean containsWriter(LogWriter writer)

removeAllWriters

public void removeAllWriters()

log

public void log(String message,
                Object... parameters)
         throws LogException
Throws:
LogException

log

public void log(Level level,
                String message,
                Object... parameters)
         throws LogException
Throws:
LogException

log

public void log(Throwable thrown)
         throws LogException
Throws:
LogException

log

public void log(String message,
                Throwable thrown,
                Object... parameters)
         throws LogException
Throws:
LogException

log

public void log(Object origin,
                String message,
                Object... parameters)
         throws LogException
Throws:
LogException

log

public void log(Object origin,
                Level level,
                String message,
                Object... parameters)
         throws LogException
Throws:
LogException

log

public void log(Object origin,
                Throwable thrown)
         throws LogException
Throws:
LogException

log

public void log(Object origin,
                Level level,
                String message,
                Throwable thrown,
                Object... parameters)
         throws LogException
Throws:
LogException

log

public void log(Level level,
                String message,
                long time,
                String sourceClass,
                String sourceString,
                Throwable thrown,
                String threadName,
                StackTraceElement[] stack,
                Object[] parameters)
         throws LogException
Throws:
LogException

isEnabled

public boolean isEnabled()
the logger only logs if the return value is true (which it is by default)


setEnabled

public void setEnabled(boolean flag)

getFilter

public Filter<LogEntry> getFilter()
returns null by default


accept

public boolean accept(LogEntry entry)
returns true only if the given entry will be logged by this logger and thus propagated to its handlers

Specified by:
accept in interface Filter<LogEntry>
See Also:
getWriters()

setFilter

public void setFilter(Filter<LogEntry> filter)

setExceptionHandler

public void setExceptionHandler(LogExceptionHandler handler)

getExceptionHandler

public LogExceptionHandler getExceptionHandler()
if the LogExceptionHandler is null (which it is by default), any LogException during logging is propagated to the calling application.

See Also:
ignoreLogExceptions()

ignoreLogExceptions

public void ignoreLogExceptions()
sets a LogExceptionHandler that simply ignores any given LogException, so no Exception will be propagated to the calling application if logging fails. To reverse the effect of calling this method, simply set the LogExceptionHandler back to a null value - or use any other customized LogExceptionHandler.

See Also:
setExceptionHandler(LogExceptionHandler)

log

public void log(LogEntry entry)
         throws LogException
logs the entry by writing to the writers after checking whether the logger is enabled and the filter (if available) accepts the entry. This is the method that ultimately called by all other logging methods. Only after all writers have been called, the last logging entry is set, i.e. if a writer would call getLast() while writing the entry, a previous entry would be returned.

Throws:
LogException - if a LogWriter throws a LogException during logging. The exception handling can be controlled by setting an LogExceptionHandler. If no LogExceptionHandler is configured, the Exception is simply propagated to the calling method.
See Also:
getFilter(), isEnabled(), ignoreLogExceptions(), setExceptionHandler(LogExceptionHandler)

getLast

public LogEntry getLast()
returns the last entry logged with this logger (while enabled and filter passed)


clearLast

public void clearLast()
sets the last logged entry to null



(c) 2001-2006 Holger Antelmann - all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads