Class SLogger
- java.lang.Object
-
- com.identityworksllc.iiq.common.logging.SLogger
-
- All Implemented Interfaces:
org.apache.commons.logging.Log
public class SLogger extends Object implements org.apache.commons.logging.Log
A wrapper around the Commons LoggingLogclass that supplements its features with some extras available in other logging libraries.Since this class itself implements
Log, Beanshell should not care if you simply overwrite the ‘log’ variable in your code.Log strings are interpreted as Java
MessageFormatobjects and have the various features of that class in your JDK version.Values passed as arguments are only evaluated when the appropriate log level is active. If the log level is not active, the operation becomes a quick no-op. This prevents a lot of isDebugEnabled() type checks.
You can also pass a
Supplierto any argument, and the supplier’s get() method will be called to derive the value, but only if the log level is active. This allows you to wrap complex operations in a lambda and only have them executed if the log level is active.If you invoke
capture(), all log messages will be captured in a thread-local buffer, which can be retrieved withgetCapturedLogs(). You should always invokereset()in a finally block to ensure that the thread-local buffer is cleared. Capture state is shared at the global level for a particular thread, so all instances of SLogger, regardless of classloader, will share the same capture buffer.In addition to the usual Log levels, this class supports the following levels:
- HERE: A log message indicating that the code has reached a certain point. This is mostly useful for tracing execution.
- ENTER: A log message indicating that the code is entering a certain segment, such as a method. This is logged at DEBUG level.
- EXIT: A log message indicating that the code is exiting a certain segment, such as a method. This is logged at DEBUG level.
The ‘S’ stands for Super. Super Logger.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSLogger.FormatterContainer class to format an object for logging.static classSLogger.LevelAn enumeration of log levels to replace the one in log4j
-
Field Summary
Fields Modifier and Type Field Description protected Stack<String>contextStackThe context stack, which can be used to track nested contexts, such as method calls.static StringCUSTOM_GLOBAL_CAPTURED_LOGS_TOKENprotected org.apache.commons.logging.LogloggerThe underlying logger to use.protected PrintStreamoutThe underlying output stream to use.
-
Constructor Summary
Constructors Modifier Constructor Description SLogger(PrintStream Out)Creates a new logger.SLogger(Class<?> Owner)Creates a new logger.SLogger(String name)Creates a new logger.SLogger(org.apache.commons.logging.Log WrapLog)Wraps the given log4j logger with this loggerprotectedSLogger(org.apache.commons.logging.Log logger, PrintStream out)Creates a new logger with the given logger and print stream.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidappendStandardPrefix()Appends the standard prefix to the captured logs, including timestamp and context stackprotected StringBuilderbuilder()Returns the StringBuilder used to capture logs, initializing it if necessaryvoidcapture()Begins capturing logs.voiddebug(Object arg0)voiddebug(Object arg0, Throwable arg1)voiddebug(String MessageTemplate, Object... Args)Logs an debugging message.voidenter(bsh.This bshThis)Logs an entry message for a Beanshell function.voidenter(String value)Logs an entry message for a segment of code.voiderror(Object arg0)voiderror(Object arg0, Throwable arg1)voiderror(String MessageTemplate, Object... Args)Logs an error message.voidexit(bsh.This bshThis)Logs an exit message for a Beanshell function.voidexit(String value)Logs an exit message for a segment of code.voidfatal(Object arg0)voidfatal(Object arg0, Throwable arg1)voidfatal(String MessageTemplate, Object... Args)Logs a fatal error message.static SLogger.Formatter[]format(Object[] args)Wraps the arguments for future formatting.StringgetCapturedLogs()Returns the captured logs as a Stringprotected static AtomicReference<StringBuilder>getCapturedLogsRef()Gets the captured logs ref for the given thread from the CustomGlobal.protected StringgetTimestamp()Generates a timestamp string for captured log entriesvoidhandleException(Throwable Error)Handles an exception.voidhere(String value)Logs a message “Here”, along with a custom suffix, indicating that the code has reached a certain point.voidinfo(Object arg0)voidinfo(Object arg0, Throwable arg1)voidinfo(String MessageTemplate, Object... Args)Logs an informational message.booleanisCapturing()Returns true if log capturing is currently activebooleanisDebugEnabled()booleanisErrorEnabled()booleanisFatalEnabled()booleanisInfoEnabled()booleanisTraceEnabled()booleanisWarnEnabled()voidlog(SLogger.Level logLevel, String message)Logs the message at the appropriate level according to the Commons Logging APIprotected voidlog(SLogger.Level logLevel, String messageTemplate, Object... args)Logs a message.Stringpop()Pops the top value off the context stackvoidpush(String value)Pushes a value onto the context stack.static StringrenderMessage(String messageTemplate, Object[] args)Renders the MessageTemplate using the given argumentsvoidreset()Resets/clears the captured logsprotected voidsave(SLogger.Level LogLevel, String MessageTemplate, Object[] Args)Saves a log message to the captured logsprotected voidsave(Throwable error)Saves an error message and stack trace to the captured logs.voidtrace(Object arg0)voidtrace(Object arg0, Throwable arg1)voidtrace(String MessageTemplate, Object... Args)Logs a trace message.voidwarn(Object arg0)voidwarn(Object arg0, Throwable arg1)voidwarn(String MessageTemplate, Object... Args)Logs a warning message.
-
-
-
Field Detail
-
CUSTOM_GLOBAL_CAPTURED_LOGS_TOKEN
public static final String CUSTOM_GLOBAL_CAPTURED_LOGS_TOKEN
- See Also:
- Constant Field Values
-
contextStack
protected final Stack<String> contextStack
The context stack, which can be used to track nested contexts, such as method calls.The context stack will be prepended to all log messages if it is not empty.
-
logger
protected final org.apache.commons.logging.Log logger
The underlying logger to use.
-
out
protected final PrintStream out
The underlying output stream to use.
-
-
Constructor Detail
-
SLogger
protected SLogger(org.apache.commons.logging.Log logger, PrintStream out)
Creates a new logger with the given logger and print stream.- Parameters:
logger- the underlying Commons Logging logger to useout- the underlying PrintStream to use
-
SLogger
public SLogger(String name)
Creates a new logger.- Parameters:
name- The name to log messages for. Typically, this is a class name, but may be a rule library, etc
-
SLogger
public SLogger(Class<?> Owner)
Creates a new logger.- Parameters:
Owner- The class to log messages for.
-
SLogger
public SLogger(org.apache.commons.logging.Log WrapLog)
Wraps the given log4j logger with this logger- Parameters:
WrapLog- The logger to wrap
-
SLogger
public SLogger(PrintStream Out)
Creates a new logger.- Parameters:
Out- The output stream to
-
-
Method Detail
-
format
public static SLogger.Formatter[] format(Object[] args)
Wraps the arguments for future formatting.The format string is not resolved at this time, meaning that the toString() is lazily evaluated.
- Parameters:
args- The arguments for any place-holders in the message template.- Returns:
- The formatted arguments.
-
getCapturedLogsRef
protected static AtomicReference<StringBuilder> getCapturedLogsRef()
Gets the captured logs ref for the given thread from the CustomGlobal.If it does not exist, it will be created. Using CustomGlobal and only core JDK classes for this allows this trace to be shared across plugins that may contain an obfuscated instance of this class.
If the ThreadLocal does not already exist, it will be created in a block synchronized on the CustomGlobal class itself, preventing double creation.
The contents of the AtomicReference may be null if capture() has not been invoked yet, or if reset() has been invoked.
- Returns:
- The AtomicReference containing the StringBuilder for captured logs for this thread
-
renderMessage
public static String renderMessage(String messageTemplate, Object[] args)
Renders the MessageTemplate using the given arguments- Parameters:
messageTemplate- The message templateargs- The arguments- Returns:
- The resolved message template
-
appendStandardPrefix
protected void appendStandardPrefix()
Appends the standard prefix to the captured logs, including timestamp and context stack
-
builder
protected StringBuilder builder()
Returns the StringBuilder used to capture logs, initializing it if necessary- Returns:
- The StringBuilder for captured logs
-
capture
public void capture()
Begins capturing logs.This will clear any previously captured logs by replacing the current StringBuilder with a new one.
-
debug
public void debug(Object arg0)
- Specified by:
debugin interfaceorg.apache.commons.logging.Log- Parameters:
arg0- The message to log- See Also:
Log.debug(Object)
-
debug
public void debug(Object arg0, Throwable arg1)
- Specified by:
debugin interfaceorg.apache.commons.logging.Log- Parameters:
arg0- The message to logarg1- The exception to log- See Also:
Log.debug(Object, Throwable)
-
debug
public void debug(String MessageTemplate, Object... Args)
Logs an debugging message.- Parameters:
MessageTemplate- A message template, which can either be a plain string or contain place-holders like {0} and {1}.Args- The arguments for any place-holders in the message template.
-
enter
public void enter(String value)
Logs an entry message for a segment of code.This will log at DEBUG level if the system configuration property IIQCommon.SLogger.EnterExitEnabled is set to true.
- Parameters:
value- The value to log as the ‘location’, e.g., a method name, a chunk of code, etc
-
enter
public void enter(bsh.This bshThis)
Logs an entry message for a Beanshell function.This will log at DEBUG level if * the system configuration property IIQCommon.SLogger.EnterExitEnabled is set to true.
- Parameters:
bshThis- The Beanshell ‘this’ object, which contains the namespace name.
-
error
public void error(Object arg0)
- Specified by:
errorin interfaceorg.apache.commons.logging.Log- See Also:
Log.error(Object)
-
error
public void error(Object arg0, Throwable arg1)
- Specified by:
errorin interfaceorg.apache.commons.logging.Log- See Also:
Log.error(Object, Throwable)
-
error
public void error(String MessageTemplate, Object... Args)
Logs an error message.- Parameters:
MessageTemplate- A message template, which can either be a plain string or contain place-holders like {0} and {1}.Args- The arguments for any place-holders in the message template.
-
exit
public void exit(String value)
Logs an exit message for a segment of code.This will log at DEBUG level if the system configuration property IIQCommon.SLogger.EnterExitEnabled is set to true.
- Parameters:
value- The value to log as the ‘location’, e.g., a method name, a chunk of code, etc
-
exit
public void exit(bsh.This bshThis)
Logs an exit message for a Beanshell function.This will log at DEBUG level if the system configuration property IIQCommon.SLogger.EnterExitEnabled is set to true.
- Parameters:
bshThis- The Beanshell ‘this’ object, which contains the namespace name.
-
fatal
public void fatal(Object arg0)
- Specified by:
fatalin interfaceorg.apache.commons.logging.Log
-
fatal
public void fatal(Object arg0, Throwable arg1)
- Specified by:
fatalin interfaceorg.apache.commons.logging.Log
-
fatal
public void fatal(String MessageTemplate, Object... Args)
Logs a fatal error message.- Parameters:
MessageTemplate- A message template, which can either be a plain string or contain place-holders like {0} and {1}.Args- The arguments for any place-holders in the message template.
-
getCapturedLogs
public String getCapturedLogs()
Returns the captured logs as a String- Returns:
- The captured logs
-
getTimestamp
protected String getTimestamp()
Generates a timestamp string for captured log entries- Returns:
- The timestamp string
-
handleException
public void handleException(Throwable Error)
Handles an exception.- Parameters:
Error- The exception to handle.
-
here
public void here(String value)
Logs a message “Here”, along with a custom suffix, indicating that the code has reached a certain point.This will only be logged (at INFO level) if the system configuration property IIQCommon.SLogger.HereEnabled is set to true.
- Parameters:
value- The value to log as the ‘location’, e.g., a method name, a chunk of code, etc.
-
info
public void info(Object arg0, Throwable arg1)
- Specified by:
infoin interfaceorg.apache.commons.logging.Log
-
info
public void info(String MessageTemplate, Object... Args)
Logs an informational message.- Parameters:
MessageTemplate- A message template, which can either be a plain string or contain place-holders like {0} and {1}.Args- The arguments for any place-holders in the message template.
-
isCapturing
public boolean isCapturing()
Returns true if log capturing is currently active- Returns:
- true if capturing is active
-
isDebugEnabled
public boolean isDebugEnabled()
- Specified by:
isDebugEnabledin interfaceorg.apache.commons.logging.Log- See Also:
Log.isDebugEnabled()
-
isErrorEnabled
public boolean isErrorEnabled()
- Specified by:
isErrorEnabledin interfaceorg.apache.commons.logging.Log- See Also:
Log.isErrorEnabled()
-
isFatalEnabled
public boolean isFatalEnabled()
- Specified by:
isFatalEnabledin interfaceorg.apache.commons.logging.Log- See Also:
Log.isFatalEnabled()
-
isInfoEnabled
public boolean isInfoEnabled()
- Specified by:
isInfoEnabledin interfaceorg.apache.commons.logging.Log- See Also:
Log.isInfoEnabled()
-
isTraceEnabled
public boolean isTraceEnabled()
- Specified by:
isTraceEnabledin interfaceorg.apache.commons.logging.Log- See Also:
Log.isTraceEnabled()
-
isWarnEnabled
public boolean isWarnEnabled()
- Specified by:
isWarnEnabledin interfaceorg.apache.commons.logging.Log- See Also:
Log.isWarnEnabled()
-
log
public void log(SLogger.Level logLevel, String message)
Logs the message at the appropriate level according to the Commons Logging API- Parameters:
logLevel- The log level to log atmessage- The message to log
-
log
protected void log(SLogger.Level logLevel, String messageTemplate, Object... args)
Logs a message.- Parameters:
logLevel- The level to log the message at.messageTemplate- A message template, which can either be a plain string or contain place-holders like {0} and {1}.args- The arguments for any place-holders in the message template.
-
pop
public String pop()
Pops the top value off the context stack- Returns:
- The value popped off the stack, or null if the stack is empty.
-
push
public void push(String value)
Pushes a value onto the context stack.This will be logged with the message
- Parameters:
value- The context value to add
-
reset
public void reset()
Resets/clears the captured logs
-
save
protected void save(Throwable error)
Saves an error message and stack trace to the captured logs.If the error cannot be rendered for whatever reason, a message will be printed to standard error as a last resort.
- Parameters:
error- The exception to handle.
-
save
protected void save(SLogger.Level LogLevel, String MessageTemplate, Object[] Args)
Saves a log message to the captured logs- Parameters:
LogLevel- The level to log the message at.MessageTemplate- A message template, which can either be a plain string or contain place-holders like {0} and {1}.Args- The arguments for any place-holders in the message template.
-
trace
public void trace(Object arg0)
- Specified by:
tracein interfaceorg.apache.commons.logging.Log
-
trace
public void trace(Object arg0, Throwable arg1)
- Specified by:
tracein interfaceorg.apache.commons.logging.Log
-
trace
public void trace(String MessageTemplate, Object... Args)
Logs a trace message.- Parameters:
MessageTemplate- A message template, which can either be a plain string or contain place-holders like {0} and {1}.Args- The arguments for any place-holders in the message template.
-
warn
public void warn(Object arg0)
- Specified by:
warnin interfaceorg.apache.commons.logging.Log- See Also:
Log.warn(Object)
-
warn
public void warn(Object arg0, Throwable arg1)
- Specified by:
warnin interfaceorg.apache.commons.logging.Log- See Also:
Log.warn(Object, Throwable)
-
-