Class 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 Logging Log class 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 MessageFormat objects 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 Supplier to 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 with getCapturedLogs(). You should always invoke reset() 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.

    • 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 use
        out - 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<StringBuildergetCapturedLogsRef()
        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 template
        args - 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:
        debug in interface org.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:
        debug in interface org.apache.commons.logging.Log
        Parameters:
        arg0 - The message to log
        arg1 - 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:
        error in interface org.apache.commons.logging.Log
        See Also:
        Log.error(Object)
      • error

        public void error​(Object arg0,
                          Throwable arg1)
        Specified by:
        error in interface org.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:
        fatal in interface org.apache.commons.logging.Log
      • fatal

        public void fatal​(Object arg0,
                          Throwable arg1)
        Specified by:
        fatal in interface org.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)
        Specified by:
        info in interface org.apache.commons.logging.Log
      • info

        public void info​(Object arg0,
                         Throwable arg1)
        Specified by:
        info in interface org.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:
        isDebugEnabled in interface org.apache.commons.logging.Log
        See Also:
        Log.isDebugEnabled()
      • isErrorEnabled

        public boolean isErrorEnabled()
        Specified by:
        isErrorEnabled in interface org.apache.commons.logging.Log
        See Also:
        Log.isErrorEnabled()
      • isFatalEnabled

        public boolean isFatalEnabled()
        Specified by:
        isFatalEnabled in interface org.apache.commons.logging.Log
        See Also:
        Log.isFatalEnabled()
      • isInfoEnabled

        public boolean isInfoEnabled()
        Specified by:
        isInfoEnabled in interface org.apache.commons.logging.Log
        See Also:
        Log.isInfoEnabled()
      • isTraceEnabled

        public boolean isTraceEnabled()
        Specified by:
        isTraceEnabled in interface org.apache.commons.logging.Log
        See Also:
        Log.isTraceEnabled()
      • isWarnEnabled

        public boolean isWarnEnabled()
        Specified by:
        isWarnEnabled in interface org.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 at
        message - 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:
        trace in interface org.apache.commons.logging.Log
      • trace

        public void trace​(Object arg0,
                          Throwable arg1)
        Specified by:
        trace in interface org.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:
        warn in interface org.apache.commons.logging.Log
        See Also:
        Log.warn(Object)
      • warn

        public void warn​(Object arg0,
                         Throwable arg1)
        Specified by:
        warn in interface org.apache.commons.logging.Log
        See Also:
        Log.warn(Object, Throwable)
      • warn

        public void warn​(String MessageTemplate,
                         Object... Args)
        Logs a warning 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.