Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.meta.log
Class LogManager

java.lang.Object
  extended by dk.rode.thesis.meta.log.LogManager

public class LogManager
extends Object

A simple log manager to create and deliver logs to use, either globally or per unique class.

Per default, the global default log is enabled, while class specific are disabled.

Its usage and concrete sub-class is hidden as it is accessed through the LogFactory.

Implementation notes:
This manager utilises the Template Method and Factory Method patterns: the create(Class, Class, AtomicInteger) method is the factory method, which is utilised by the getLog(Class) template method.

In a real-life scenario, there should be such a thing as log-timeout to ensure the file logs are closed properly.

Author:
Gunni Rode / rode.dk

Field Summary
private  AtomicInteger lineNumber
          Line number values, if not null.
private  Map<Class<?>,Log> logs
          Created logs per class.
private  Class<?> type
          Class of log to use.
 
Constructor Summary
protected LogManager()
          No-arg constructor.
protected LogManager(Class<T> type, boolean lineNumbers)
          Constructor.
 
Method Summary
protected  Log create(Class<?> clazz, Class<?> type, AtomicInteger lineNumber)
          Factory method creating the types of log to use.
 Log getLog()
          Return the default log.
 Log getLog(Class<?> clazz)
          Returns, and creates if necessary, the log associated with the class supplied as class, if any.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lineNumber

private final AtomicInteger lineNumber
Line number values, if not null.


logs

private final Map<Class<?>,Log> logs
Created logs per class.

Null key represents the global log.

Never null.


type

private final Class<?> type
Class of log to use.

Constructor Detail

LogManager

protected LogManager()
No-arg constructor.


LogManager

protected LogManager(Class<T> type,
                     boolean lineNumbers)
Constructor.

Type Parameters:
T - The type of log created by this manager.
Parameters:
type - The type of log to use; can be null, in which case the default type of log is used.
lineNumbers - True to log line numbers.
Method Detail

create

protected Log create(Class<?> clazz,
                     Class<?> type,
                     AtomicInteger lineNumber)
              throws Exception
Factory method creating the types of log to use.

Override this for different behaviour.

Parameters:
clazz - The class to associate the created log with, if any; can be null, i.e. default log.
type - The type of log to use; never null.
lineNumber - Line number values; can be null.
Returns:
A new Log instance; never null.
Throws:
Exception - In case the creation fails (a default log will be created in its place).

getLog

public final Log getLog()
Return the default log.

Returns:
The default log; never null.

getLog

public final Log getLog(Class<?> clazz)
Returns, and creates if necessary, the log associated with the class supplied as class, if any.

If clazz is null, the default log is used. If clazz is an anonymous class, the first non-anonymous super class is used or the first and only interface implemented (recursively as well).

Parameters:
clazz - The class; can be null, i.e. default log.
Returns:
The log to use; never null.

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.