it.andynaz.log
public class Logger extends Object
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.
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
Logger()
Creates a logger.
|
Logger(Config conf)
Deprecated.
Use
Logger(Properties) instead; this method will be
removed in future release. |
Logger(Properties props)
Creates a logger with a configuration.
|
Logger(String name)
Creates a logger with a name.
|
Logger(String name,
Config conf)
Deprecated.
Use
Logger(String, Properties) instead; this method
will be removed in future release. |
Logger(String name,
Properties props)
Creates a Logger with a name and a specified configuration.
|
Modifier and Type | Method and Description |
---|---|
protected String |
getDate()
Returns a string with the date.
|
protected String |
getPrefix()
Deprecated.
this method will be joined int the
getPrefix(Level)
method in future release |
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.
|
protected String name
protected boolean useDate
If the value is 'true', the prefix starts with "date+separator"
protected SimpleDateFormat dateFormat
SimpleDateFormat
. Dafaule value is
"yy-MM-dd HH:mm:ss".protected Level level
Level.INFO
.
Only the logs with a level compatible (according to the Level.hasToLog(Level)
method) with this value will be logged.
protected boolean useLevel
protected String separator
@Deprecated public Logger(String name, Config conf)
Logger(String, Properties)
instead; this method
will be removed in future release.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:
SimpleDateFormat
objectLevel
enum constantsname
- name of the loggerconf
- logger configurationpublic Logger(String name, Properties props)
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:
SimpleDateFormat
object)Level
enum constantsLogger(String, Config)
method.name
- name of the loggerprops
- logger configurationpublic Logger(String name)
ConfigMgr.getConfigParams()
method).name
- name of the logger@Deprecated public Logger(Config conf)
Logger(Properties)
instead; this method will be
removed in future release.conf
- configuration of the loggerpublic Logger(Properties props)
props
- configuration of the loggerLogger(String, Properties)
public Logger()
ConfigMgr.getConfigParams()
method).Logger(String, Properties)
public void log(String log, Level level)
log
- log to be writtenlevel
- log levelpublic void log(String log)
INFO
.log
- log to be writtenpublic void log(Exception e)
INFO
.e
- exception to be loggedpublic void log(Exception e, Level level)
It is written the exception type (with the message of the exception, if present) and the stack trace.
e
- exception to be loggedlevel
- log level@Deprecated protected String getPrefix()
getPrefix(Level)
method in future releaseThe 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.
protected String getPrefix(Level level)
getPrefix()
method and add the level
code, according to the configuration of this logger.level
- the level from which take the code[date+sep][name+sep][level+sep]
protected String getDate()
dateFormat
value.© 2013 andynaz - http://andynaz.altervista.org