Class SmartInspectHandler


  • public final class SmartInspectHandler
    extends Handler
    Directs logging output to a SmartInspect session.

    This class can be used as handler in the java.util.logging.Logger class. Therefore, it is especially useful if you want to add the SmartInspect capabilities to existing code which uses the j2se logging framework.

    This class uses a session which receives the logging output. Depending on the called java.util.logging.Logger method, the publish method formats a record and chooses a suitable Session method to send this message.

    Note: This class is fully threadsafe.

     import java.util.logging.*;
     import com.gurock.smartinspect.*;
     import com.gurock.smartinspect.jdk.*;
    
     public class HandlerExample
     {
            public static void main(String[] args)
        {
                    // Enable SmartInspect logging.
                    SiAuto.si.setEnabled(true);
    
                    // Remove the default handlers.
                    LogManager.getLogManager().reset();
    
                    // Add a new SmartInspect handler which uses SiAuto.main
                    // as session and then set the log level to Level.ALL.
                    Logger.global.addHandler(new SmartInspectHandler());
                    Logger.global.setLevel(Level.ALL);
    
                    // Write messages to the session using indentation.
                    Logger.global.fine("Test Message");
                    Logger.global.entering("HandlerExample", "main");
                    Logger.global.fine("Test Message");
                    Logger.global.fine("Test Message");
                    Logger.global.exiting("HandlerExample", "main");
                    Logger.global.fine("Test Message");
    
                    // Write test warnings and failures.
                    Logger.global.warning("Test Warning");
                    Logger.global.severe("Test Error");
                    Logger.global.throwing("HandlerExample", "main", new Exception("Test"));
        }
     }
     
    • Constructor Detail

      • SmartInspectHandler

        public SmartInspectHandler()
        Overloaded. Creates and initializes a new SmartInspectHandler instance with SiAuto.main as session.
      • SmartInspectHandler

        public SmartInspectHandler​(String sessionName)
        Overloaded. Creates and initializes a new SmartInspectHandler instance using a new session with SiAuto.si as parent.

        This constructor creates a new session with the supplied sessionName parameter as name and the default background color. The parent of this session is SiAuto.si.

        Parameters:
        sessionName - The name for the new session
      • SmartInspectHandler

        public SmartInspectHandler​(Session session)
        Overloaded. Creates and initializes a new SmartInspectHandler instance with an existing session.
        Parameters:
        session - The session to use
    • Method Detail

      • getFormatter

        public Formatter getFormatter()
        Overridden. Returns the formatter used to format LogRecords.
        Overrides:
        getFormatter in class Handler
        Returns:
        The formatter used to format LogRecords
      • setFormatter

        public void setFormatter​(Formatter formatter)
                          throws SecurityException
        Overridden. Sets the formatter used to format LogRecords.

        Please note that this SmartInspectHandler only accepts instances of the SmartInspectFormatter class. Other formatters will silently be ignored.

        Overrides:
        setFormatter in class Handler
        Parameters:
        formatter - The formatter used to format LogRecords
        Throws:
        SecurityException - If a security manager exists and if the caller does not have LoggingPermission("control")
      • getSession

        public Session getSession()
        Returns the session used to publish LogRecords.
        Returns:
        The session used to publish LogRecords
      • flush

        public void flush()
        Overridden. Does nothing.
        Specified by:
        flush in class Handler
      • publish

        public void publish​(LogRecord record)
        Overriden. Publishes a LogRecord.

        This method formats the supplied record and chooses a suitable Session method to log the record. If the record contains a throwable object, then this method calls the Session.logException method. Otherwise the action depends on the level and message of the record.

        If the message of the record starts with "ENTRY" or "RETURN", then the Session.enterMethod or Session.leaveMethod methods are called. The log level for these enterMethod and leaveMethod method calls depends on the level of the supplied record. The following table lists the conversion method:

        Log level conversions
        java.util.logging.Levelcom.gurock.smartinspect.Level
        Level.FINEST, Level.FINERLevel.Debug
        Level.FINE, LEVEL.CONFIGLevel.Verbose
        Level.INFOLevel.Message
        Level.WARNINGLevel.Warning
        Level.SEVERELevel.Error

        If the supplied record does not use any of the above Level values of the java.util.logging package, then this method defaults to the com.gurock.smartinspect.Level.Message level.

        In contrast, if the message of the record does not start with the "ENTRY" or "RETURN" tokens, this method sends a simple message. The log entry type of the message here depends on the level of the supplied record. The following table lists the conversion method:

        Log level conversions
        java.util.logging.LevelCorresponding Session method
        Level.FINEST, Level.FINERSession.logDebug
        Level.FINE, LEVEL.CONFIGSession.logVerbose
        Level.INFOSession.logMessage
        Level.WARNINGSession.logWarning
        Level.SEVERESession.logError

        Similar to above, if the supplied LogRecord does not use any of the listed Level values of the java.util.logging package, this method defaults to Session.logMessage.

        Specified by:
        publish in class Handler
        Parameters:
        record - The record to publish