Package com.bw.jtools
Class Log
- java.lang.Object
-
- com.bw.jtools.Log
-
public final class Log extends java.lang.ObjectSimple log handling.
In most projects at some early point you have to decide which logging framework should be used.
Often this decision is forced because of libraries you need and that already use some logging framework.
I personally don't like to bind my source sp deeply to such stuff.
The used logging framework is mostly not determined by functional requirements, so why bind a huge part of the sources to it?
This class is a lightweight layer to keep the impact low.
All methods are static!
Pro: There is no need to store a "logger"-Instance in your class or to call singleton-getter each time.
Contra: No way to specify different settings for different classes by configuration.
I personally never needed to configure the logger differently for different parts of my software, so I can live with this restriction. And noise from other libraries can turned off by the used framework.
If you use the other parts of this library that use this log, you can simply delegate the output to what-ever you like or turn it off.
Current implementation tries to initialize Log4J2 as back-end. If Log4J2 is not found in class-path, a simple console-logger is used.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classLog.LoggerFacadeAdapter for logging back-ends.
-
Field Summary
Fields Modifier and Type Field Description protected static CollectorLoggercollectorLogstatic intDEBUGThis logging level enables all output.static intERRORThis logging level enables only errors.static intINFOThis logging level enables errors,warnings and information.static Log.LoggerFacadelogstatic intNONEThis logging level turns all output off.static intWARNThis logging level enables errors and warnings.
-
Constructor Summary
Constructors Constructor Description Log()
-
Method Summary
Modifier and Type Method Description static voidaddLogger(Log.LoggerFacade logger)Adds a application specific logger.static voiddebug(java.lang.CharSequence msg)Logs a debug message.static voiddebug(java.lang.CharSequence msg, java.lang.Throwable t)Logs a debug message plus stack-trace.static voiderror(java.lang.CharSequence msg)Logs an error.static voiderror(java.lang.CharSequence msg, java.lang.Throwable t)Logs an error plus stack-trace.static java.lang.StringgetRestrictedStackTrace(java.lang.Throwable t, java.lang.String prefix, int max_lines)Get a stack-trace with restricted length.static voidinfo(java.lang.CharSequence msg)Logs an informational message.static booleanisDebugEnabled()Checks if debug log-output is enabled.static booleanisErrorEnabled()Checks if error log-output is enabled.static booleanisInfoEnabled()Checks if informational log-output is enabled.static booleanisWarnEnabled()Checks if warning log-output is enabled.static voidremoveLogger(Log.LoggerFacade logger)Removes a application specific logger.static voidsetLevel(int level)Sets system log level.
The value can be one of the constants NONE ERROR WARN INFO DEBUGstatic voidstartCollectMessages(int level, boolean includeStackTraces)Starts collecting messages for the current thread.
From this point untilstopCollectMessagesis called, all output in this thread that match the log.level is collected.
This function was designed to provide "internal detail" data for some short-term operation.static voidstopCollectMessages(java.util.List<java.lang.String> messages)Stops collecting messages.
All messages that were collected from the last call tostartCollectMessagesare returned.static voidwarn(java.lang.CharSequence msg)Logs a warning.static voidwarn(java.lang.CharSequence msg, java.lang.Throwable t)Logs an warning plus stack-trace.
-
-
-
Field Detail
-
NONE
public static final int NONE
This logging level turns all output off.- See Also:
- Constant Field Values
-
ERROR
public static final int ERROR
This logging level enables only errors.- See Also:
- Constant Field Values
-
WARN
public static final int WARN
This logging level enables errors and warnings.- See Also:
- Constant Field Values
-
INFO
public static final int INFO
This logging level enables errors,warnings and information.- See Also:
- Constant Field Values
-
DEBUG
public static final int DEBUG
This logging level enables all output.- See Also:
- Constant Field Values
-
log
public static Log.LoggerFacade log
-
collectorLog
protected static final CollectorLogger collectorLog
-
-
Method Detail
-
getRestrictedStackTrace
public static java.lang.String getRestrictedStackTrace(java.lang.Throwable t, java.lang.String prefix, int max_lines)Get a stack-trace with restricted length.- Parameters:
t- The Throwable for the stack-trace.prefix- Prefix for each lines, e.g. to add some indentation. Can be null.max_lines- Maximum number of lines to print.- Returns:
- The stack-trace.
-
startCollectMessages
public static void startCollectMessages(int level, boolean includeStackTraces)Starts collecting messages for the current thread.
From this point untilstopCollectMessagesis called, all output in this thread that match the log.level is collected.
This function was designed to provide "internal detail" data for some short-term operation.- Parameters:
level- The level to collect for.includeStackTraces- If Stack-Traces should be included.
-
stopCollectMessages
public static void stopCollectMessages(java.util.List<java.lang.String> messages)
Stops collecting messages.
All messages that were collected from the last call tostartCollectMessagesare returned.- Parameters:
messages- List to add the messages to.
-
addLogger
public static void addLogger(Log.LoggerFacade logger)
Adds a application specific logger.- Parameters:
logger- The logger to add.
-
removeLogger
public static void removeLogger(Log.LoggerFacade logger)
Removes a application specific logger.- Parameters:
logger- The logger to remove.
-
setLevel
public static void setLevel(int level)
Sets system log level.
The value can be one of the constants- NONE
- ERROR
- WARN
- INFO
- DEBUG
- Parameters:
level- The new system log level.
-
error
public static void error(java.lang.CharSequence msg)
Logs an error.- Parameters:
msg- The message to log.
-
error
public static void error(java.lang.CharSequence msg, java.lang.Throwable t)Logs an error plus stack-trace.- Parameters:
msg- The message to log.t- The Throwable to trace.
-
warn
public static void warn(java.lang.CharSequence msg)
Logs a warning.- Parameters:
msg- The message to log.
-
warn
public static void warn(java.lang.CharSequence msg, java.lang.Throwable t)Logs an warning plus stack-trace.- Parameters:
msg- The message to log.t- The Throwable to trace.
-
info
public static void info(java.lang.CharSequence msg)
Logs an informational message.- Parameters:
msg- The message to log.
-
debug
public static void debug(java.lang.CharSequence msg)
Logs a debug message.- Parameters:
msg- The message to log.
-
debug
public static void debug(java.lang.CharSequence msg, java.lang.Throwable t)Logs a debug message plus stack-trace.- Parameters:
msg- The message to log.t- The Throwable to trace.
-
isDebugEnabled
public static boolean isDebugEnabled()
Checks if debug log-output is enabled.- Returns:
- true if current log-level is equal to or higher than DEBUG.
-
isInfoEnabled
public static boolean isInfoEnabled()
Checks if informational log-output is enabled.- Returns:
- true if current log-level is equal to or higher than INFO.
-
isWarnEnabled
public static boolean isWarnEnabled()
Checks if warning log-output is enabled.- Returns:
- true if current log-level is equal to or higher than WARN.
-
isErrorEnabled
public static boolean isErrorEnabled()
Checks if error log-output is enabled.- Returns:
- true if current log-level is equal to or higher than WARN.
-
-