andyLib v0.9.1

it.andynaz.log
Class Logger

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

public class Logger
extends Object

Base logger. A logger is used to log events. This class is the root of all the logger jerarchy, and it logs on the System.out channel.

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

Each logger has a set of attributes related to how it logs the events (for instance, FileLogger has a File object where to write logs), which are set by the contructors (see the documentation to know which parameter it takes).
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 particular log).

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 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, other methods can be overridden.

Version:
2014/04/29
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 useDate
          Indicates if the date has to be used in the prefix of the log.
protected  boolean useLevel
          Indicates if the level code has to be used in the prefix of the log.
 
Constructor Summary
Logger()
          Creates a logger.
Logger(Properties props)
          Creates a logger with a configuration.
Logger(String name)
          Creates a logger with a name.
Logger(String name, Properties props)
          Creates a Logger with a name and a specified configuration.
 
Method Summary
protected  String getDate()
          Returns a string with the date.
protected  String getPrefix(Level level)
          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 (useful ti know by which component of the program the log has been done).


useDate

protected boolean useDate
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"


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".


level

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

Only the logs with a level compatible (according to the Level.hasToLog(Level) method) with this value will be logged.


useLevel

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


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,
              Properties props)
Creates a Logger with a name and a specified configuration. From the configuration, this method takes from the Properties object only the propertyes used. If a property is not found in the configuration, default value is 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 configuration (the Properties object passed as an argument) are:

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

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

Logger

public Logger(String name)
Creates a logger with a name. It uses the configuration of the application (taken from ConfigMgr.getConfigParams() method).

Parameters:
name - name of the logger

Logger

public Logger(Properties props)
Creates a logger with a configuration.

Parameters:
props - configuration of the logger
See Also:
Logger(String, Properties)

Logger

public Logger()
Creates a logger. It uses the configuration of the application (taken from ConfigMgr.getConfigParams() method).

See Also:
Logger(String, Properties)
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 INFO.

Parameters:
log - log to be written

log

public void log(Exception e)
Writes the log of an exception. The level used is 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(Level level)
Returns the prefix to be written before the log.

The prefix is

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

Parameters:
level - the level from which take the code
Returns:
a prefix in the form [date+sep][level+sep][name+sep] (it can be an empty string)

getDate

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

Returns:
a string with the date

andyLib v0.9.1

© 2013 andynaz - http://andynaz.altervista.org