public class LoggingTool extends Object implements ILoggingTool
public class SomeClass {
private static ILoggingTool logger =
LoggingToolFactory.createLoggingTool(SomeClass.class);
}
There is no special reason not to make the logger private and static, as the logging
information is closely bound to one specific Class, not subclasses and not instances.
The logger has five logging levels:
Consider that the debugging will not always be turned on. Therefore, it is better not to concatenate string in the logger.debug() call, but have the LoggingTool do this when appropriate. In other words, use:
logger.debug("The String X has this value: ", someString);
logger.debug("The int Y has this value: ", y);
instead of:
logger.debug("The String X has this value: " + someString);
logger.debug("The int Y has this value: " + y);
For logging calls that require even more computation you can use the
isDebugEnabled() method:
if (logger.isDebugEnabled()) {
logger.info("The 1056389822th prime that is used is: ",
calculatePrime(1056389822));
}
The class uses log4j as a backend if available, and System.out otherwise.
| Modifier and Type | Field and Description |
|---|---|
int |
DEFAULT_STACK_LENGTH
Default number of StackTraceElements to be printed by debug(Exception).
|
| Constructor and Description |
|---|
LoggingTool()
Constructs a LoggingTool which produces log lines without any special
indication which class the message originates from.
|
LoggingTool(Class<?> classInst)
Constructs a LoggingTool which produces log lines indicating them to be
for the given Class.
|
LoggingTool(Object object)
Constructs a LoggingTool which produces log lines indicating them to be
for the Class of the
Object. |
| Modifier and Type | Method and Description |
|---|---|
static void |
configureLog4j()
Forces the
LoggingTool to configure the Log4J toolkit. |
static ILoggingTool |
create(Class<?> sourceClass)
Creates a new
LoggingTool for the given class. |
void |
debug(Object object)
Shows DEBUG output for the Object.
|
void |
debug(Object object,
Object... objects)
Shows DEBUG output for the given Object's.
|
void |
dumpClasspath()
Outputs the system property for java.class.path.
|
void |
dumpSystemProperties()
Outputs system properties for the operating system and the java
version.
|
void |
error(Object object)
Shows ERROR output for the Object.
|
void |
error(Object object,
Object... objects)
Shows ERROR output for the given Object's.
|
void |
fatal(Object object)
Shows FATAL output for the Object.
|
void |
info(Object object)
Shows INFO output for the Object.
|
void |
info(Object object,
Object... objects)
Shows INFO output for the given Object's.
|
boolean |
isDebugEnabled()
Use this method for computational demanding debug info.
|
void |
setStackLength(int length)
Sets the number of StackTraceElements to be printed in DEBUG mode when
calling
debug(Throwable). |
void |
warn(Object object)
Shows WARN output for the Object.
|
void |
warn(Object object,
Object... objects)
Shows WARN output for the given Object's.
|
public final int DEFAULT_STACK_LENGTH
public LoggingTool()
public LoggingTool(Object object)
Object.object - Object from which the log messages originatepublic LoggingTool(Class<?> classInst)
classInst - Class from which the log messages originatepublic static void configureLog4j()
LoggingTool to configure the Log4J toolkit.
Normally this should be done by the application that uses the CDK library,
but is available for convenience.public void dumpSystemProperties()
dumpSystemProperties in interface ILoggingToolpublic void setStackLength(int length)
debug(Throwable).
The default value is DEFAULT_STACK_LENGTH.setStackLength in interface ILoggingToollength - the new stack lengthDEFAULT_STACK_LENGTHpublic void dumpClasspath()
dumpClasspath in interface ILoggingToolpublic void debug(Object object)
debug in interface ILoggingToolobject - Object to apply toString() too and outputpublic void debug(Object object, Object... objects)
debug in interface ILoggingToolobject - Object to apply toString() too and outputobjects - Object[] to apply toString() too and outputpublic void error(Object object)
error in interface ILoggingToolobject - Object to apply toString() too and outputpublic void error(Object object, Object... objects)
error in interface ILoggingToolobject - Object to apply toString() too and outputobjects - Object[] to apply toString() too and outputpublic void fatal(Object object)
fatal in interface ILoggingToolobject - Object to apply toString() too and outputpublic void info(Object object)
info in interface ILoggingToolobject - Object to apply toString() too and outputpublic void info(Object object, Object... objects)
info in interface ILoggingToolobject - Object to apply toString() too and outputobjects - Object[] to apply toString() too and outputpublic void warn(Object object)
warn in interface ILoggingToolobject - Object to apply toString() too and outputpublic void warn(Object object, Object... objects)
warn in interface ILoggingToolobject - Object to apply toString() too and outputobjects - Object[] to apply toString() too and outputpublic boolean isDebugEnabled()
if (logger.isDebugEnabled()) {
logger.info("The 1056389822th prime that is used is: ",
calculatePrime(1056389822));
}
isDebugEnabled in interface ILoggingToolpublic static ILoggingTool create(Class<?> sourceClass)
LoggingTool for the given class.sourceClass - Class for which logging messages are recorded.LoggingTool.Copyright © 2017. All Rights Reserved.