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.err
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.
|
int |
getLevel()
Get the current level of this logger.
|
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 |
setLevel(int level)
Set the level of this logger.
|
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 ILoggingTool
public void setStackLength(int length)
debug(Throwable)
.
The default value is DEFAULT_STACK_LENGTH.setStackLength
in interface ILoggingTool
length
- the new stack lengthDEFAULT_STACK_LENGTH
public void dumpClasspath()
dumpClasspath
in interface ILoggingTool
public void debug(Object object)
debug
in interface ILoggingTool
object
- Object to apply toString() too and outputpublic void debug(Object object, Object... objects)
debug
in interface ILoggingTool
object
- Object to apply toString() too and outputobjects
- Object[] to apply toString() too and outputpublic void error(Object object)
error
in interface ILoggingTool
object
- Object to apply toString() too and outputpublic void error(Object object, Object... objects)
error
in interface ILoggingTool
object
- Object to apply toString() too and outputobjects
- Object[] to apply toString() too and outputpublic void fatal(Object object)
fatal
in interface ILoggingTool
object
- Object to apply toString() too and outputpublic void info(Object object)
info
in interface ILoggingTool
object
- Object to apply toString() too and outputpublic void info(Object object, Object... objects)
info
in interface ILoggingTool
object
- Object to apply toString() too and outputobjects
- Object[] to apply toString() too and outputpublic void warn(Object object)
warn
in interface ILoggingTool
object
- Object to apply toString() too and outputpublic void warn(Object object, Object... objects)
warn
in interface ILoggingTool
object
- 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 ILoggingTool
public static ILoggingTool create(Class<?> sourceClass)
LoggingTool
for the given class.sourceClass
- Class for which logging messages are recorded.LoggingTool
.public void setLevel(int level)
ILoggingTool.TRACE
, ILoggingTool.DEBUG
, ILoggingTool.INFO
, ILoggingTool.WARN
, ILoggingTool.ERROR
, ILoggingTool.FATAL
. After setting the level only messages
above the specified level will be emitted.setLevel
in interface ILoggingTool
level
- the levelpublic int getLevel()
ILoggingTool.TRACE
, ILoggingTool.DEBUG
, ILoggingTool.INFO
, ILoggingTool.WARN
, ILoggingTool.ERROR
, ILoggingTool.FATAL
.getLevel
in interface ILoggingTool
Copyright © 2021. All rights reserved.