Class Log
- Direct Known Subclasses:
SystemLog
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(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.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionfinal voidfinal voidfinal voidfinal voidfinal voidfinal voidfinal voidfinal voidbug(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7, Object... args) booleanReturns true if the log's current level will allow logging messages at the specified logging level.abstract voidclose()static voidCloses and removes the log with the specified name if it existsfinal voidfinal voidfinal voidfinal voidfinal voidfinal voidfinal voidfinal voiddebug(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7, Object... args) final voidfinal voidfinal voidfinal voidfinal voidfinal voidfinal voidfinal voiderror(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7, Object... args) static final LogConvenience method that returns the log named after the package that the specified class belong to.static final LogConvenience method that returns a log named after the package that the specified object's class belong to.static LogReturns aSystemLogwith the specified name.final StringgetDebugContext(LogLevel level, int framenum) Returns the method name, file number and line number of the calling method.static SystemLogReturns the default system log.getLevel()Returns the current level set for this log.Returns an iteration containing level names for this log.static LogAn alias for methodget(String).getName()Returns the name of this log.final voidfinal voidfinal voidfinal voidfinal voidfinal voidfinal voidfinal voidinfo(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7, Object... args) abstract voidabstract voidabstract voidabstract voidabstract voidabstract voidabstract voidlog(LogLevel level, Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7) abstract voidlog(LogLevel level, Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7, Object str8) abstract voidlog(LogLevel level, Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7, Object str8, Object... args) voidvoidprintLevelName(boolean printName) If set to true, will print the level name before the logging message.voidprintMethodInfoAtLevel(LogLevel level) By default, method, line and thread information is printed wich each logging statement at the DEBUG level.voidprintRelativeTimestamp(boolean val) Prints a relative time stamp with every message.voidprintTimestamp(boolean val) Prints a time stamp with every message.static voidsetDefaultLevel(LogLevel level) Sets the new default logging level for all new instances of loggers (that are created after this method is invoked).static voidsetDefaultLevel(String level) Sets the new default logging level for all new instances of loggers (created after this method is invoked).voidSets the current logging level for this logger.voidSets the level of this log based on a level description.static voidsetLevelForAll(String name, LogLevel level) Sets the level for all logs whose name contain the specified name.toString()final voidfinal voidfinal voidfinal voidfinal voidfinal voidfinal voidfinal voidwarn(Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7, Object... args)
-
Field Details
-
OFF
-
ERROR
-
WARN
-
INFO
-
DEBUG
-
DEFAULT_LEVEL
The default level used for new logs. Subclasses should (by default) create logs using this level.
-
-
Method Details
-
get
Returns aSystemLogwith 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.
-
getLog
An alias for methodget(String).Returns a
SystemLogwith 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.
-
get
Convenience method that returns the log named after the package that the specified class belong to. If 2 classesa.b.Class1
anda.b.Class2
call this method, they will get the same logger (named a.b).If the log does not already exist, creates and returns a new
SystemLogwith that name.- Parameters:
c- a non-null class
-
get
Convenience method that returns a log named after the package that the specified object's class belong to. If 2 objects of classa.b.Class1
anda.b.Class2
call this method, they will get the same logger (named a.b).If the log does not already exist, creates and returns a new
SystemLogwith that name.- Parameters:
obj- a non-null object
-
getDefault
Returns the default system log. This system log writes to System.err and has it's level set toINFO. This level can be changed to some other level if desired by invokingon the returned object.invalid reference
setLevel() -
getLevelNames
Returns an iteration containing level names for this log. The names can be in any order. -
closeLog
-
getDebugContext
Returns the method name, file number and line number of the calling method. Useful for logging/code tracing.- Parameters:
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.
-
printLevelName
If set to true, will print the level name before the logging message. For example, if the level isINFO, the message isfoo, thenINFO foo
will be printed.This is set to true by default.
-
printTimestamp
Prints a time stamp with every message. By default this is false- Parameters:
val- specify true to print time stamps, false to not
-
printRelativeTimestamp
Prints a relative time stamp with every message. By default, printing any timestamp is false. Timestamps must first be enabled via theprintTimestampmethod before this method can have any effect.- Parameters:
val- if true, prints a relative time stamp. An initial timestamp is printed and all succeeding timestamps are second increments from the initial timestamp
-
printMethodInfoAtLevel
By default, method, line and thread information is printed wich each logging statement at the DEBUG level. Other levels print only the log message but skip the method/stack information.This method allows method information to be printed at all levels greater than or equal to the specified level.
-
setLevel
Sets the current logging level for this logger. Each log has a logging level. A message is printed only if the message level is equal to or lower than the current maximum level for that log.Typically, classes that implement a log will define a bunch of static variables of type
LogLevelthat list the available levels for that implementation. Clients of a particular log class should use levels defined within only that class. -
setLevel
Sets the level of this log based on a level description. This is convenient for when levels are specified in a configuration file. If the specified name cannot be converted into a level, then no change is made.- Parameters:
levelname- the level name. For example, "info" would set the level of this log to INFO. The name is case-insensitive.
-
setLevelForAll
Sets the level for all logs whose name contain the specified name. This is convenient when changing log levels for package heirarchies. A empty string (non-null) "" sets the level for all logs.- Parameters:
levelname- the level name. For example, "info" would set the level of this log to INFO. The name is case-insensitive.
-
setDefaultLevel
Sets the new default logging level for all new instances of loggers (that are created after this method is invoked). -
setDefaultLevel
Sets the new default logging level for all new instances of loggers (created after this method is invoked). -
canLog
Returns true if the log's current level will allow logging messages at the specified logging 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.
- Parameters:
level- the specified logging level
-
getName
-
getLevel
-
logSystemInfo
-
toString
-
error
-
error
-
error
-
error
-
error
-
error
-
error
-
error
-
warn
-
warn
-
warn
-
warn
-
warn
-
warn
-
warn
-
warn
-
info
-
info
-
info
-
info
-
info
-
info
-
info
-
info
-
debug
-
debug
-
debug
-
debug
-
debug
-
debug
-
debug
-
debug
-
bug
-
bug
-
bug
-
bug
-
bug
-
bug
-
bug
-
bug
-
close
-
log
- Parameters:
level- the current log level. This can be logged as well.str1- unless overridden in a subclass, this is the value returned bygetDebugContext(fc.io.LogLevel,int)and is generated automatically by the warn(), info(), debug() etc., methods
-
log
- Parameters:
level- the current log level. This can be logged as well.str1- unless overridden in a subclass, this is the value returned bygetDebugContext(fc.io.LogLevel,int)and is generated automatically by the warn(), info(), debug() etc., methodsstr2_onwards- some arbitrary object
-
log
- Parameters:
level- the current log level. This can be logged as well.str1- unless overridden in a subclass, this is the value returned bygetDebugContext(fc.io.LogLevel,int)and is generated automatically by the warn(), info(), debug() etc., methodsstr2_onwards- some arbitrary object
-
log
- Parameters:
level- the current log level. This can be logged as well.str1- unless overridden in a subclass, this is the value returned bygetDebugContext(fc.io.LogLevel,int)and is generated automatically by the warn(), info(), debug() etc., methodsstr2_onwards- some arbitrary object
-
log
public abstract void log(LogLevel level, Object str1, Object str2, Object str3, Object str4, Object str5) - Parameters:
level- the current log level. This can be logged as well.str1- unless overridden in a subclass, this is the value returned bygetDebugContext(fc.io.LogLevel,int)and is generated automatically by the warn(), info(), debug() etc., methodsstr2_onwards- some arbitrary object
-
log
public abstract void log(LogLevel level, Object str1, Object str2, Object str3, Object str4, Object str5, Object str6) - Parameters:
level- the current log level. This can be logged as well.str1- unless overridden in a subclass, this is the value returned bygetDebugContext(fc.io.LogLevel,int)and is generated automatically by the warn(), info(), debug() etc., methodsstr2_onwards- some arbitrary object
-
log
public abstract void log(LogLevel level, Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7) - Parameters:
level- the current log level. This can be logged as well.str1- unless overridden in a subclass, this is the value returned bygetDebugContext(fc.io.LogLevel,int)and is generated automatically by the warn(), info(), debug() etc., methodsstr2_onwards- some arbitrary object
-
log
public abstract void log(LogLevel level, Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7, Object str8) - Parameters:
level- the current log level. This can be logged as well.str1- unless overridden in a subclass, this is the value returned bygetDebugContext(fc.io.LogLevel,int)and is generated automatically by the warn(), info(), debug() etc., methodsstr2_onwards- some arbitrary object
-
log
public abstract void log(LogLevel level, Object str1, Object str2, Object str3, Object str4, Object str5, Object str6, Object str7, Object str8, Object... args) - Parameters:
level- the current log level. This can be logged as well.str1- unless overridden in a subclass, this is the value returned bygetDebugContext(fc.io.LogLevel,int)and is generated automatically by the warn(), info(), debug() etc., methodsstr2_onwards- some arbitrary object
-