public abstract class Log extends Object
Logs messages to some user specified destination(s). Each log has a name and a log level.
The names are arbitrary (hopefully descriptive) names, per your liking. Many different logs can be created and then later retrieved by their name as needed.
The levels provided by this class have the order:
OFF < ERROR < WARN < INFO < DEBUGThe above levels are self-explanatory.
Convenience methods with names equal to a level name are provided. So for example, instead of saying:
log(LogLevel.warn, "the message");one can say:
log.warn("the message");
log.debug("the message"); --or-- log.bug("the message");
A default logger of type SystemLog
is provided for
convenience in this class and can be retrieved by calling the getDefault()
method.
The closeLog(String)
method is called on all logs
at JVM shutdown.
Note:To log a full stack traces, pass the string obtained by the
IOUtil.throwableToString(java.lang.Throwable)
method. (by default, an exception simply
prints its message but not the full stack trace)
Implementation Note: subclasses should implement static getLog(..) type methods. These that create, as needed, and return a new log object of that subclass type. These getter methods should be static for convenience. Also, the implementation of the subclass.getLog(...) methods in subclasses is expected by convention.
Thread Safety: This class is Threadsafe and all its methods can be used concurrently.
Modifier and Type | Field and Description |
---|---|
static LogLevel |
DEBUG |
static LogLevel |
DEFAULT_LEVEL
The default level used for new logs.
|
static LogLevel |
ERROR |
static LogLevel |
INFO |
static LogLevel |
OFF |
static LogLevel |
WARN |
Modifier and Type | Method and Description |
---|---|
void |
bug(Object str1) |
void |
bug(Object str1,
Object str2) |
void |
bug(Object str1,
Object str2,
Object str3) |
void |
bug(Object str1,
Object str2,
Object str3,
Object str4) |
void |
bug(Object str1,
Object str2,
Object str3,
Object str4,
Object str5) |
void |
bug(Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6) |
void |
bug(Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6,
Object str7) |
void |
bug(Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6,
Object str7,
Object... args) |
boolean |
canLog(LogLevel level)
Returns true if the log's current level will allow logging
messages at the specified logging level.
|
abstract void |
close() |
static void |
closeLog(String name)
Closes and removes the log with the specified name if it exists
|
void |
debug(Object str1) |
void |
debug(Object str1,
Object str2) |
void |
debug(Object str1,
Object str2,
Object str3) |
void |
debug(Object str1,
Object str2,
Object str3,
Object str4) |
void |
debug(Object str1,
Object str2,
Object str3,
Object str4,
Object str5) |
void |
debug(Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6) |
void |
debug(Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6,
Object str7) |
void |
debug(Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6,
Object str7,
Object... args) |
void |
error(Object str1) |
void |
error(Object str1,
Object str2) |
void |
error(Object str1,
Object str2,
Object str3) |
void |
error(Object str1,
Object str2,
Object str3,
Object str4) |
void |
error(Object str1,
Object str2,
Object str3,
Object str4,
Object str5) |
void |
error(Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6) |
void |
error(Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6,
Object str7) |
void |
error(Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6,
Object str7,
Object... args) |
static Log |
get(Class c)
Convenience method that returns the log named after the package that
the specified class belong to.
|
static Log |
get(Object obj)
Convenience method that returns a log named after the package that
the specified object's class belong to.
|
static Log |
get(String name)
Returns a
SystemLog with the specified name. |
String |
getDebugContext(LogLevel level,
int framenum)
Returns the method name, file number and line number
of the calling method.
|
static SystemLog |
getDefault()
Returns the default system log.
|
LogLevel |
getLevel()
Returns the current level set for this log.
|
Iterator |
getLevelNames()
Returns an iteration containing level names for this log.
|
static Log |
getLog(String name)
An alias for method
get(String) . |
String |
getName()
Returns the name of this log.
|
void |
info(Object str1) |
void |
info(Object str1,
Object str2) |
void |
info(Object str1,
Object str2,
Object str3) |
void |
info(Object str1,
Object str2,
Object str3,
Object str4) |
void |
info(Object str1,
Object str2,
Object str3,
Object str4,
Object str5) |
void |
info(Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6) |
void |
info(Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6,
Object str7) |
void |
info(Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6,
Object str7,
Object... args) |
abstract void |
log(LogLevel level,
Object str1) |
abstract void |
log(LogLevel level,
Object str1,
Object str2) |
abstract void |
log(LogLevel level,
Object str1,
Object str2,
Object str3) |
abstract void |
log(LogLevel level,
Object str1,
Object str2,
Object str3,
Object str4) |
abstract void |
log(LogLevel level,
Object str1,
Object str2,
Object str3,
Object str4,
Object str5) |
abstract void |
log(LogLevel level,
Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6) |
abstract void |
log(LogLevel level,
Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6,
Object str7) |
abstract void |
log(LogLevel level,
Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6,
Object str7,
Object str8) |
abstract void |
log(LogLevel level,
Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6,
Object str7,
Object str8,
Object... args) |
void |
logSystemInfo() |
void |
printLevelName(boolean printName)
If set to true, will print the level name before the logging
message.
|
void |
printMethodInfoAtLevel(LogLevel level)
By default, method, line and thread information is printed wich each
logging statement at the DEBUG level.
|
void |
printRelativeTimestamp(boolean val)
Prints a relative time stamp with every message.
|
void |
printTimestamp(boolean val)
Prints a time stamp with every message.
|
static void |
setDefaultLevel(LogLevel level)
Sets the new default logging level for all new instances of loggers
(that are created after this method is invoked).
|
static void |
setDefaultLevel(String level)
Sets the new default logging level for all new instances of loggers
(created after this method is invoked).
|
void |
setLevel(LogLevel level)
Sets the current logging level for this logger.
|
void |
setLevel(String levelname)
Sets the level of this log based on a level description.
|
static void |
setLevelForAll(String name,
LogLevel level)
Sets the level for all logs whose name contain the specified name.
|
String |
toString() |
void |
warn(Object str1) |
void |
warn(Object str1,
Object str2) |
void |
warn(Object str1,
Object str2,
Object str3) |
void |
warn(Object str1,
Object str2,
Object str3,
Object str4) |
void |
warn(Object str1,
Object str2,
Object str3,
Object str4,
Object str5) |
void |
warn(Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6) |
void |
warn(Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6,
Object str7) |
void |
warn(Object str1,
Object str2,
Object str3,
Object str4,
Object str5,
Object str6,
Object str7,
Object... args) |
public static LogLevel DEFAULT_LEVEL
public static Log get(String name)
SystemLog
with the specified name. If the log does not already
exist, creates and returns a new SystemLog with that name.
The system log created will have a default destination of System.err
and a default level of INFO
.
To obtain logs with a different destination, create a SystemLog directly.
public static Log getLog(String name)
get(String)
.
Returns a SystemLog
with the specified name. If the log does not already
exist, creates and returns a new SystemLog with that name.
The system log created will have a default destination of System.err
and a default level of INFO
.
To obtain logs with a different destination, create a SystemLog directly.
public static final Log get(Class c)
a.b.Class1and
a.b.Class2call this method, they will get the same logger (named a.b).
If the log does not already exist, creates and returns a new SystemLog
with that name.
c
- a non-null classpublic static final Log get(Object obj)
a.b.Class1and
a.b.Class2call this method, they will get the same logger (named a.b).
If the log does not already exist, creates and returns a new SystemLog
with that name.
obj
- a non-null objectpublic static SystemLog getDefault()
INFO
. This
level can be changed to some other level if desired by invoking setLevel()
on the returned object.public Iterator getLevelNames()
public static void closeLog(String name)
public final String getDebugContext(LogLevel level, int framenum)
level
- the level for which this logging call was invoked.framenum
- the method to examine. This method itself
has frame number 0, the calling method
has frame number 1, it's parent 2 and so on.public void printLevelName(boolean printName)
INFO
, the
message is foo
, then
INFO foowill be printed.
This is set to true by default.
public void printTimestamp(boolean val)
val
- specify true to print time stamps, false
to notpublic void printRelativeTimestamp(boolean val)
printTimestamp
method before this method can have any effect.
val
- if true, prints a relative time
stamp. An initial timestamp is printed and
all succeeding timestamps are second
increments from the initial timestamppublic void printMethodInfoAtLevel(LogLevel level)
This method allows method information to be printed at all levels greater than or equal to the specified level.
public void setLevel(LogLevel level)
Typically, classes that implement a log will define a bunch of static
variables of type LogLevel
that list the available levels for that
implementation. Clients of a particular log class should use levels
defined within only that class.
public void setLevel(String levelname)
levelname
- the level name. For example,
"info" would set the level of
this log to INFO. The name
is case-insensitive.public static void setLevelForAll(String name, LogLevel level)
levelname
- the level name. For example,
"info" would set the level of
this log to INFO. The name
is case-insensitive.public static void setDefaultLevel(LogLevel level)
public static void setDefaultLevel(String level)
public boolean canLog(LogLevel level)
Implementation Note: If the currentLevel is lesser or equal to the specified level returns true, else false. Subclasses can override this method if needed.
level
- the specified logging levelpublic LogLevel getLevel()
public void logSystemInfo()
public final void error(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6)
public final void error(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7)
public final void error(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7, Object... args)
public final void warn(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6)
public final void warn(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7)
public final void warn(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7, Object... args)
public final void info(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6)
public final void info(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7)
public final void info(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7, Object... args)
public final void debug(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6)
public final void debug(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7)
public final void debug(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7, Object... args)
public final void bug(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6)
public final void bug(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7)
public final void bug(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7, Object... args)
public abstract void close()
public abstract void log(LogLevel level, Object str1)
level
- the current log level. This can be logged
as well.str1
- unless overridden in a subclass, this is the
value returned by getDebugContext
and
is generated automatically by the warn(),
info(), debug() etc., methodspublic abstract void log(LogLevel level, Object str1, Object str2)
level
- the current log level. This can be logged
as well.str1
- unless overridden in a subclass, this is the
value returned by getDebugContext
and
is generated automatically by the warn(),
info(), debug() etc., methodsstr2_onwards
- some arbitrary objectpublic abstract void log(LogLevel level, Object str1, Object str2, Object str3)
level
- the current log level. This can be logged
as well.str1
- unless overridden in a subclass, this is the
value returned by getDebugContext
and
is generated automatically by the warn(),
info(), debug() etc., methodsstr2_onwards
- some arbitrary objectpublic abstract void log(LogLevel level, Object str1, Object str2, Object str3, Object str4)
level
- the current log level. This can be logged
as well.str1
- unless overridden in a subclass, this is the
value returned by getDebugContext
and
is generated automatically by the warn(),
info(), debug() etc., methodsstr2_onwards
- some arbitrary objectpublic abstract void log(LogLevel level, Object str1, Object str2, Object str3, Object str4, Object str5)
level
- the current log level. This can be logged
as well.str1
- unless overridden in a subclass, this is the
value returned by getDebugContext
and
is generated automatically by the warn(),
info(), debug() etc., methodsstr2_onwards
- some arbitrary objectpublic abstract void log(LogLevel level, Object str1, Object str2, Object str3, Object str4, Object str5, Object str6)
level
- the current log level. This can be logged
as well.str1
- unless overridden in a subclass, this is the
value returned by getDebugContext
and
is generated automatically by the warn(),
info(), debug() etc., methodsstr2_onwards
- some arbitrary objectpublic abstract void log(LogLevel level, Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7)
level
- the current log level. This can be logged
as well.str1
- unless overridden in a subclass, this is the
value returned by getDebugContext
and
is generated automatically by the warn(),
info(), debug() etc., methodsstr2_onwards
- some arbitrary objectpublic abstract void log(LogLevel level, Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7, Object str8)
level
- the current log level. This can be logged
as well.str1
- unless overridden in a subclass, this is the
value returned by getDebugContext
and
is generated automatically by the warn(),
info(), debug() etc., methodsstr2_onwards
- some arbitrary objectpublic abstract void log(LogLevel level, Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7, Object str8, Object... args)
level
- the current log level. This can be logged
as well.str1
- unless overridden in a subclass, this is the
value returned by getDebugContext
and
is generated automatically by the warn(),
info(), debug() etc., methodsstr2_onwards
- some arbitrary object