it.andynaz.log
Class Logger

java.lang.Object
  extended by it.andynaz.log.Logger
Direct Known Subclasses:
FileLogger

public class Logger
extends Object

Base logger. This class is the root of all the logger jerarchy. A logger actually logs all the events.

Each logger has a set of attributes related to haw it logs the events (for instance, FileLogger has a File object to where write the logs).
A logger should have also methods related to this set of attributes.
In addition to this members, each logger has also a name, used in the log prefix (useful to distinguish which logger do a log).

Every log is written with a prefix, which is returned by the getPrefix() method.

Logs are written using the various log() methods: every method, before writing the log, check if the log level if enough to write the log.

Sub-classes of this logger can change the way a log is written (for instance, in a file or on a database), only changing the log(String, Level) method. For other reasons, like efficiency, aother methods can be overridden.

This logger uses the System.out

.

Version:
2013/01/14
Author:
andynaz

Field Summary
protected  SimpleDateFormat dateFormat
          Indicates the format of the date.
protected  Level level
          Indicates the level of the logging.
protected  String name
          Name of the logger.
protected  String separator
          Indicates the separator to use in the prefix of the logs.
protected  boolean withDate
          Indicates if the date has to be used in the prefix of the log.
protected  boolean withLevel
          Indicates if the level code has to be used in the profix of the log.
 
Constructor Summary
Logger()
          Creates a logger.
Logger(Config conf)
          Creates a logger with a configuration.
Logger(String name)
          Creates a logger with a name.
Logger(String name, Config conf)
          Creates a Logger with a name and a specified configuration.
 
Method Summary
protected  String getDate()
          Returns a string with the date.
protected  String getPrefix()
          Returns the prefix to be written before the log.
 void log(Exception e)
          Writes the log of an exception.
 void log(Exception e, Level level)
          Writes the log of an exception with a specificied level.
 void log(String log)
          Writes a log.
 void log(String log, Level level)
          Writes a log with a specified level.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

name

protected String name
Name of the logger. If present, it is used in the prefix of the log.


withDate

protected boolean withDate
Indicates if the date has to be used in the prefix of the log. Dafaule value is 'false'.

If the value is 'true', the prefix starts with "date+separator"


withLevel

protected boolean withLevel
Indicates if the level code has to be used in the profix of the log. Dafaule value is 'false'.


level

protected Level level
Indicates the level of the logging. Dafaule value is Level.INFO.

Only the logs with a level higher than this value will be logged.


dateFormat

protected SimpleDateFormat dateFormat
Indicates the format of the date. The format is the one used by SimpleDateFormat. Dafaule value is "yy-MM-dd HH:mm:ss".


separator

protected String separator
Indicates the separator to use in the prefix of the logs. Dafaule value is "::".

Constructor Detail

Logger

public Logger(String name,
              Config conf)
Creates a Logger with a name and a specified configuration. From the configuration are taken only the propertyes used. For the properties not found in the configuration, default values are used.

Properties are used to construct the attributes of the logger. If error occour (for instance, the format for the date is not correct), no exception is thrown and the default value is used.

The properties taken from the Config object are:

dateFormat
format for the date (suitable for a SimpleDateFormat object
level
log level: has to be one of the Level enum constants
withDate
a boolean to indicates if the date has to be used in the log prefix
withLevel
a boolean to indicates if the level code has to be used in the log prefix
separator
a string indicating the separator of the variuos section of the log prefix

Parameters:
name - name of the logger
conf - logger configuration

Logger

public Logger(String name)
Creates a logger with a name. It uses the shared configuration.

Parameters:
name - name of the logger

Logger

public Logger(Config conf)
Creates a logger with a configuration.

Parameters:
conf - configuration of the logger

Logger

public Logger()
Creates a logger. It uses the shared configuration.

Method Detail

log

public void log(String log,
                Level level)
Writes a log with a specified level. Depending on the log configuration, this method include the level code and a separator before the log.

Parameters:
log - log to be written
level - log level

log

public void log(String log)
Writes a log. The level used is Level.INFO.

Parameters:
log - log to be written

log

public void log(Exception e)
Writes the log of an exception. The level used is Level.INFO.

Parameters:
e - exception to be logged

log

public void log(Exception e,
                Level level)
Writes the log of an exception with a specificied level.

It is written the exception type (with the message of the exception, if present) and the stack trace

Parameters:
e - exception to be logged
level - log level

getPrefix

protected String getPrefix()
Returns the prefix to be written before the log.

The prefix is

[date+sep][name+sep]
where each part is optional (depends on the logger configuration).

Note: the log prefix returned does not include the level code, which has to be included in the log methods.

Returns:
the prefix, based on the configuration of the logger

getDate

protected String getDate()
Returns a string with the date. The format of the sctring is the one specified by the dateFormat values.

Returns:
a string with the date


(c) 2013 andynaz - http://andynaz.altervista.org