|
Simple Log 2.0.1
[grlea] |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.grlea.log.SimpleLogger
Used to create log messages for a single class or instance of a class.
SimpleLogger
is where it all happens, from a client's perspective.
The easiest way to use Simple Log is to create one SimpleLogger
for each class and
then log to it!
1. Create a logger like this:
private static final SimpleLogger log = new SimpleLogger(HelloWorld.class);
2. And then use it like this:
log.entry("main()"); log.debugObject("argv", argv); if (argv.length == 0) { log.info("No arguments. Using defaults."); ... } ... log.exit("main()");
SimpleLogger
provides for four types of log messages:
SimpleLogger.db()
)SimpleLogger.dbo()
)
SimpleLogger.dbe()
)SimpleLogger.entry()
and SimpleLogger.exit()
)
Instance-based SimpleLogger
s can be used to determine, from the log output,
exactly which object a log statement is coming from. This is useful when many objects of the same
class are all performing logging and it is crucial to analysis to know which object logged any
given message.
Here is an example of how an instance-based logger would be used:
public class Test { private final SimpleLogger log; private final String id; public Test(String id) { log = new SimpleLogger(Test.class, id); this.id = id; } }Note the following important features of this pattern and instance-based logs in general:
log
field is not static. There is one per instance.<ClassName>.class
is used rather than getClass()
. Otherwise,
if this class were subclassed, logging statements would appear with the subclass's name as the
source.
Constructor Summary | |
SimpleLogger(java.lang.Class sourceClass)
Creates a new SimpleLogger for the specified Class using the default
SimpleLog . |
|
SimpleLogger(java.lang.Class sourceClass,
boolean useLongName)
Creates a new SimpleLogger for the specified Class using the default
SimpleLog . |
|
SimpleLogger(java.lang.Class sourceClass,
java.lang.Object instanceId)
Creates a new SimpleLogger for the specified Class using the default
SimpleLog . |
|
SimpleLogger(java.lang.Class sourceClass,
java.lang.Object instanceId,
boolean useLongName)
Creates a new SimpleLogger for the specified Class using the default
SimpleLog . |
|
SimpleLogger(SimpleLog log,
java.lang.Class sourceClass)
Creates a new SimpleLogger for the specified Class that will log to, and be
configured by, the provided SimpleLog . |
|
SimpleLogger(SimpleLog log,
java.lang.Class sourceClass,
boolean useLongName)
Creates a new SimpleLogger for the specified Class that will log to, and be
configured by, the provided SimpleLog . |
|
SimpleLogger(SimpleLog log,
java.lang.Class sourceClass,
java.lang.Object instanceId)
Creates a new SimpleLogger for the specified Class that will log to, and be
configured by, the provided SimpleLog . |
|
SimpleLogger(SimpleLog log,
java.lang.Class sourceClass,
java.lang.Object instanceId,
boolean useLongName)
Creates a new SimpleLogger for the specified Class that will log to, and be
configured by, the provided SimpleLog . |
Method Summary | |
void |
db(DebugLevel level,
java.lang.String message)
Logs a simple debug message. |
void |
dbe(DebugLevel level,
java.lang.Throwable t)
Logs a message containing an exception (or throwable). |
void |
dbo(DebugLevel level,
java.lang.String objectName,
boolean val)
Logs a message containing a boolean 's name and value. |
void |
dbo(DebugLevel level,
java.lang.String objectName,
byte val)
Logs a message containing a byte 's name and value. |
void |
dbo(DebugLevel level,
java.lang.String objectName,
byte[] val)
Logs a message containing a byte array's name and value. |
void |
dbo(DebugLevel level,
java.lang.String objectName,
char val)
Logs a message containing a char 's name and value. |
void |
dbo(DebugLevel level,
java.lang.String objectName,
char[] val)
Logs a message containing a char array's name and value. |
void |
dbo(DebugLevel level,
java.lang.String objectName,
double val)
Logs a message containing a double 's name and value. |
void |
dbo(DebugLevel level,
java.lang.String objectName,
float val)
Logs a message containing a float 's name and value. |
void |
dbo(DebugLevel level,
java.lang.String objectName,
int val)
Logs a message containing an int 's name and value. |
void |
dbo(DebugLevel level,
java.lang.String objectName,
long val)
Logs a message containing a long 's name and value. |
void |
dbo(DebugLevel level,
java.lang.String objectName,
java.lang.Object value)
Logs a message containing an object's name and value. |
void |
dbo(DebugLevel level,
java.lang.String objectName,
java.lang.Object[] val)
Logs a message containing an object array's name and value. |
void |
dbo(DebugLevel level,
java.lang.String objectName,
short val)
Logs a message containing a short 's name and value. |
void |
debug(java.lang.String message)
Logs a debug message at the Debug level. |
void |
debugObject(java.lang.String objectName,
boolean val)
Convenience method for logging a boolean 's name and value at the
Debug level. |
void |
debugObject(java.lang.String objectName,
int val)
Convenience method for logging an int 's name and value at the
Debug level. |
void |
debugObject(java.lang.String objectName,
java.lang.Object val)
Convenience method for logging an object's name and value at the Debug level. |
void |
entry(java.lang.String methodName)
Logs an entry message. |
void |
error(java.lang.String message)
Logs a debug message at the Error level. |
void |
errorException(java.lang.Throwable t)
Convenience method for logging an exception (or throwable) at the Error level. |
void |
exit(java.lang.String methodName)
Logs an exit message. |
void |
fatal(java.lang.String message)
Logs a debug message at the Fatal level. |
void |
fatalException(java.lang.Throwable t)
Convenience method for logging an exception (or throwable) at the Fatal level. |
java.lang.String |
getConfigName()
Returns this SimpleLogger 's config name, the name used to specify and look up
its configuration. |
DebugLevel |
getDebugLevel()
Returns the current debug level of this SimpleLogger . |
java.lang.Object |
getInstanceID()
Returns the #instanceId of this SimpleLogger , or null if it
does not have one (i.e. |
java.lang.Class |
getSourceClass()
Returns the source class of this SimpleLogger . |
void |
info(java.lang.String message)
Logs a debug message at the Info level. |
void |
infoObject(java.lang.String objectName,
boolean val)
Convenience method for logging a boolean 's name and value at the Info level. |
void |
infoObject(java.lang.String objectName,
int val)
Convenience method for logging an int 's name and value at the
Info level. |
void |
infoObject(java.lang.String objectName,
java.lang.Object val)
Convenience method for logging an object's name and value at the Info level. |
boolean |
isInstanceDebugger()
Returns true if this SimpleLogger is a logger for one instance of
the source class rather than for the class as a whole. |
boolean |
isTracing()
Returns whether this SimpleLogger is currently reporting tracing or not. |
void |
ludicrous(java.lang.String message)
Logs a debug message at the Ludicrous level. |
void |
ludicrousObject(java.lang.String objectName,
boolean val)
Convenience method for logging a boolean 's name and value at the
Ludicrous level. |
void |
ludicrousObject(java.lang.String objectName,
int val)
Convenience method for logging an int 's name and value at the
Ludicrous level. |
void |
ludicrousObject(java.lang.String objectName,
java.lang.Object val)
Convenience method for logging an object's name and value at the Ludicrous level. |
void |
setDebugLevel(DebugLevel debugLevel)
Sets the current debug level of this SimpleLogger . |
void |
setInstanceID(java.lang.Object instanceId)
Sets the #instanceId of this SimpleLogger instance. |
void |
setTracing(boolean tracing)
Sets this SimpleLogger 's tracing flag. |
void |
verbose(java.lang.String message)
Logs a debug message at the Verbose level. |
void |
verboseObject(java.lang.String objectName,
boolean val)
Convenience method for logging a boolean 's name and value at the
Verbose level. |
void |
verboseObject(java.lang.String objectName,
int val)
Convenience method for logging an int 's name and value at the
Verbose level. |
void |
verboseObject(java.lang.String objectName,
java.lang.Object val)
Convenience method for logging an object's name and value at the Verbose level. |
void |
warn(java.lang.String message)
Logs a debug message at the Warn level. |
void |
warnException(java.lang.Throwable t)
Convenience method for logging an exception (or throwable) at the Warn level. |
boolean |
wouldLog(DebugLevel level)
A convenience method for testing whether a message logged at the specified level would be logged by this SimpleLogger . |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public SimpleLogger(java.lang.Class sourceClass)
Creates a new SimpleLogger
for the specified Class using the default
SimpleLog
.
This constructor is the equivalent of calling:
new SimpleLogger(SimpleLog.defaultInstance(), sourceClass)
sourceClass
- The class that this SimpleLogger object is for, the name of which will be
printed out as part of every message.public SimpleLogger(java.lang.Class sourceClass, java.lang.Object instanceId)
Creates a new SimpleLogger
for the specified Class using the default
SimpleLog
.
This constructor is the equivalent of calling:
new SimpleLogger(SimpleLog.defaultInstance(), sourceClass, instanceId)
sourceClass
- The class that this SimpleLogger object is for, the name of which will be
printed out as part of every message.instanceId
- An object uniquely identifying the instance of sourceClass
that this SimpleLogger
is specifically for. Can be null
if this
SimpleLogger
instance is for all instances of sourceClass
rather than for a particular instance (this is the norm).public SimpleLogger(java.lang.Class sourceClass, boolean useLongName)
Creates a new SimpleLogger
for the specified Class using the default
SimpleLog
.
This constructor is the equivalent of calling:
new SimpleLogger(SimpleLog.defaultInstance(), sourceClass, useLongName)
sourceClass
- The class that this SimpleLogger object is for, the name of which will be
printed out as part of every message.useLongName
- A boolean indicating whether the fully-qualified name of the specified
class should be printed in each message (when set to true
), or just the name of
the class within it's package (ie. the package will not be printed when set to
false
).public SimpleLogger(java.lang.Class sourceClass, java.lang.Object instanceId, boolean useLongName)
Creates a new SimpleLogger
for the specified Class using the default
SimpleLog
.
This constructor is the equivalent of calling:
new SimpleLogger(SimpleLog.defaultInstance(), sourceClass, instanceId, useLongName)
sourceClass
- The class that this SimpleLogger object is for, the name of which will be
printed out as part of every message.instanceId
- An object uniquely identifying the instance of sourceClass
that this SimpleLogger
is specifically for. Can be null
if this
SimpleLogger
instance is for all instances of sourceClass
rather than for a particular instance (this is the norm).useLongName
- A boolean indicating whether the fully-qualified name of the specified
class should be printed in each message (when set to true
), or just the name of
the class within it's package (ie. the package will not be printed when set to
false
).public SimpleLogger(SimpleLog log, java.lang.Class sourceClass)
Creates a new SimpleLogger
for the specified Class that will log to, and be
configured by, the provided SimpleLog
.
This constructor is the equivalent of calling:
new SimpleLogger(log, sourceClass, false)
log
- the SimpleLog
instance that this SimpleLogger
should log
to and be configured by.sourceClass
- The class that this SimpleLogger object is for, the name of which will be
printed out as part of every message.public SimpleLogger(SimpleLog log, java.lang.Class sourceClass, java.lang.Object instanceId)
Creates a new SimpleLogger
for the specified Class that will log to, and be
configured by, the provided SimpleLog
.
This constructor is the equivalent of calling:
new SimpleLogger(SimpleLog.defaultInstance(), sourceClass, instanceId, false)
log
- the SimpleLog
instance that this SimpleLogger
should log
to and be configured by.sourceClass
- The class that this SimpleLogger object is for, the name of which will be
printed out as part of every message.instanceId
- An object uniquely identifying the instance of sourceClass
that this SimpleLogger
is specifically for. Can be null
if this
SimpleLogger
instance is for all instances of sourceClass
rather than for a particular instance (this is the norm).public SimpleLogger(SimpleLog log, java.lang.Class sourceClass, boolean useLongName)
Creates a new SimpleLogger
for the specified Class that will log to, and be
configured by, the provided SimpleLog
.
This constructor is the equivalent of calling:
new SimpleLogger(SimpleLog.defaultInstance(), sourceClass, null, useLongName)
log
- the SimpleLog
instance that this SimpleLogger
should log
to and be configured by.sourceClass
- The class that this SimpleLogger object is for, the name of which will be
printed out as part of every message.useLongName
- A boolean indicating whether the fully-qualified name of the specified
class should be printed in each message (when set to true
), or just the name of
the class within it's package (ie. the package will not be printed when set to
false
).public SimpleLogger(SimpleLog log, java.lang.Class sourceClass, java.lang.Object instanceId, boolean useLongName)
Creates a new SimpleLogger
for the specified Class that will log to, and be
configured by, the provided SimpleLog
.
log
- the SimpleLog
instance that this SimpleLogger
should log
to and be configured by.sourceClass
- The class that this SimpleLogger object is for, the name of which will be
printed out as part of every message.instanceId
- An object uniquely identifying the instance of sourceClass
that this SimpleLogger
is specifically for. Can be null
if this
SimpleLogger
instance is for all instances of sourceClass
rather than for a particular instance (this is the norm).useLongName
- A boolean indicating whether the fully-qualified name of the specified
class should be printed in each message (when set to true
), or just the name of
the class within it's package (ie. the package will not be printed when set to
false
).Method Detail |
public boolean isInstanceDebugger()
true
if this SimpleLogger
is a logger for one instance of
the source class rather than for the class as a whole.
public java.lang.Object getInstanceID()
#instanceId
of this SimpleLogger
, or null
if it
does not have one (i.e. it is a class logger, not an instance logger).
public void setInstanceID(java.lang.Object instanceId)
#instanceId
of this SimpleLogger
instance.
java.lang.IllegalArgumentException
- if instanceId
is null
java.lang.IllegalStateException
- if this SimpleLogger
is for a class, not an
instance. (See the javadoc for the class for more information.)public java.lang.String getConfigName()
Returns this SimpleLogger
's config name, the name used to specify and look up
its configuration.
Currently, the config name is the fully-qualified name of the source class of the logger,
plus, if the logger is an instance logger, a period ('.') followed by the string
representation (toString()
) of the logger's instance ID.
Additionally, any dollar signs appearing in the name are changed to periods.
public DebugLevel getDebugLevel()
SimpleLogger
.
public boolean wouldLog(DebugLevel level)
SimpleLogger
.
level
- the level of the hypothetical message being logged
true
if a message at the specified level would be logged given this
SimpleLogger
's current log level, false
if it would not.getDebugLevel()
,
DebugLevel#shouldLog
public void setDebugLevel(DebugLevel debugLevel)
SimpleLogger
.
debugLevel
- the new debug level
java.lang.IllegalArgumentException
- if debugLevel
is null
.public boolean isTracing()
SimpleLogger
is currently reporting tracing or not.
true
if this logger is reporting tracing, false otherwise.public void setTracing(boolean tracing)
SimpleLogger
's tracing flag.
tracing
- true
if this logger should report tracing, false if it should not.public java.lang.Class getSourceClass()
SimpleLogger
.
public void fatal(java.lang.String message)
Fatal
level.
message
- the message to log.db(org.grlea.log.DebugLevel, java.lang.String)
public void error(java.lang.String message)
Error
level.
message
- the message to log.db(org.grlea.log.DebugLevel, java.lang.String)
public void warn(java.lang.String message)
Warn
level.
message
- the message to log.db(org.grlea.log.DebugLevel, java.lang.String)
public void info(java.lang.String message)
Info
level.
message
- the message to log.db(org.grlea.log.DebugLevel, java.lang.String)
public void debug(java.lang.String message)
Debug
level.
message
- the message to log.db(org.grlea.log.DebugLevel, java.lang.String)
public void verbose(java.lang.String message)
Verbose
level.
message
- the message to log.db(org.grlea.log.DebugLevel, java.lang.String)
public void ludicrous(java.lang.String message)
Ludicrous
level.
message
- the message to log.db(org.grlea.log.DebugLevel, java.lang.String)
public void db(DebugLevel level, java.lang.String message)
Logs a simple debug message.
The message will be printed if the given debug level is less than or equal to the current
debug level of this SimpleLogger
.
message
- The debug message to print.fatal(java.lang.String)
,
error(java.lang.String)
,
warn(java.lang.String)
,
info(java.lang.String)
,
debug(java.lang.String)
,
verbose(java.lang.String)
public void dbo(DebugLevel level, java.lang.String objectName, java.lang.Object value)
Logs a message containing an object's name and value.
The object name and value will be printed only if the given debug level is less than or
equal to the current debug level of this SimpleLogger
.
Note that, if the given object is an instance of Object[]
, byte[]
or char[]
, this method will route the call to the corresponding variation of
dbo()
(each of which performs special formatting for the particualr type),
eliminating the need to perform pre-logging type checks and casts.
Note that passing a Throwable
to this method does not behave in the same way as
dbe(org.grlea.log.DebugLevel, java.lang.Throwable)
. I.e. passing an exception in as the object value will not result in a stack
trace being printed.
objectName
- The name of the object whose value is being given.value
- The value of the object.public void dbo(DebugLevel level, java.lang.String objectName, java.lang.Object[] val)
Logs a message containing an object array's name and value.
The array name and value will be printed only if the given debug level is less than or
equal to the current debug level of this SimpleLogger
.
objectName
- The name of the array whose value is being given.val
- The array.public void dbo(DebugLevel level, java.lang.String objectName, short val)
Logs a message containing a short
's name and value.
dbo(DebugLevel,String,Object)
public void dbo(DebugLevel level, java.lang.String objectName, int val)
Logs a message containing an int
's name and value.
dbo(DebugLevel,String,Object)
public void dbo(DebugLevel level, java.lang.String objectName, long val)
Logs a message containing a long
's name and value.
dbo(DebugLevel,String,Object)
public void dbo(DebugLevel level, java.lang.String objectName, boolean val)
Logs a message containing a boolean
's name and value.
dbo(DebugLevel,String,Object)
public void dbo(DebugLevel level, java.lang.String objectName, float val)
Logs a message containing a float
's name and value.
dbo(DebugLevel,String,Object)
public void dbo(DebugLevel level, java.lang.String objectName, double val)
Logs a message containing a double
's name and value.
dbo(DebugLevel,String,Object)
public void dbo(DebugLevel level, java.lang.String objectName, byte val)
Logs a message containing a byte
's name and value.
dbo(DebugLevel,String,Object)
public void dbo(DebugLevel level, java.lang.String objectName, byte[] val)
Logs a message containing a byte
array's name and value.
dbo(DebugLevel,String,Object)
public void dbo(DebugLevel level, java.lang.String objectName, char val)
Logs a message containing a char
's name and value.
dbo(DebugLevel,String,Object)
public void dbo(DebugLevel level, java.lang.String objectName, char[] val)
Logs a message containing a char
array's name and value.
dbo(DebugLevel,String,Object)
public void infoObject(java.lang.String objectName, java.lang.Object val)
Info
level.
dbo(DebugLevel,String,Object)
public void infoObject(java.lang.String objectName, boolean val)
boolean
's name and value at the Info
level.
dbo(DebugLevel,String,boolean)
public void infoObject(java.lang.String objectName, int val)
int
's name and value at the
Info
level.
dbo(DebugLevel,String,int)
public void debugObject(java.lang.String objectName, java.lang.Object val)
Debug
level.
dbo(DebugLevel,String,Object)
public void debugObject(java.lang.String objectName, boolean val)
boolean
's name and value at the
Debug
level.
dbo(DebugLevel,String,boolean)
public void debugObject(java.lang.String objectName, int val)
int
's name and value at the
Debug
level.
dbo(DebugLevel,String,int)
public void verboseObject(java.lang.String objectName, java.lang.Object val)
Verbose
level.
dbo(DebugLevel,String,Object)
public void verboseObject(java.lang.String objectName, boolean val)
boolean
's name and value at the
Verbose
level.
dbo(DebugLevel,String,boolean)
public void verboseObject(java.lang.String objectName, int val)
int
's name and value at the
Verbose
level.
dbo(DebugLevel,String,int)
public void ludicrousObject(java.lang.String objectName, java.lang.Object val)
Ludicrous
level.
dbo(DebugLevel,String,Object)
public void ludicrousObject(java.lang.String objectName, boolean val)
boolean
's name and value at the
Ludicrous
level.
dbo(DebugLevel,String,boolean)
public void ludicrousObject(java.lang.String objectName, int val)
int
's name and value at the
Ludicrous
level.
dbo(DebugLevel,String,int)
public void dbe(DebugLevel level, java.lang.Throwable t)
Logs a message containing an exception (or throwable).
The exception will be printed only if the given debug level is less than or
equal to the current debug level of this SimpleLogger
. This method will result in
the stack trace of the exception being printed if this option is turned on in the properties
(which it is by default).
t
- the throwable to log.public void fatalException(java.lang.Throwable t)
Fatal
level.
dbe(DebugLevel,Throwable)
public void errorException(java.lang.Throwable t)
Error
level.
dbe(DebugLevel,Throwable)
public void warnException(java.lang.Throwable t)
Warn
level.
dbe(DebugLevel,Throwable)
public void entry(java.lang.String methodName)
Logs an entry message.
The message will be printed only if the this SimpleLogger
's tracing flag is
set to true
.
methodName
- The method name to include in the entry message.public void exit(java.lang.String methodName)
Logs an exit message.
The message will be printed only if the this SimpleLogger
's tracing flag is
set to true
.
|
Copyright (c) 2004-2006, Graham Lea. All rights reserved. |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |