Class SmartInspectHandler
- java.lang.Object
-
- java.util.logging.Handler
-
- com.gurock.smartinspect.jdk.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 Summary
Constructors Constructor Description SmartInspectHandler()Overloaded.SmartInspectHandler(Session session)Overloaded.SmartInspectHandler(String sessionName)Overloaded.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Overridden.voidflush()Overridden.FormattergetFormatter()Overridden.SessiongetSession()Returns the session used to publish LogRecords.voidpublish(LogRecord record)Overriden.voidsetFormatter(Formatter formatter)Overridden.-
Methods inherited from class java.util.logging.Handler
getEncoding, getErrorManager, getFilter, getLevel, isLoggable, reportError, setEncoding, setErrorManager, setFilter, setLevel
-
-
-
-
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:
getFormatterin classHandler- 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:
setFormatterin classHandler- 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
-
close
public void close() throws SecurityExceptionOverridden. Does nothing.- Specified by:
closein classHandler- Throws:
SecurityException
-
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.Level com.gurock.smartinspect.Level Level.FINEST, Level.FINER Level.Debug Level.FINE, LEVEL.CONFIG Level.Verbose Level.INFO Level.Message Level.WARNING Level.Warning Level.SEVERE Level.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.Level Corresponding Session method Level.FINEST, Level.FINER Session.logDebug Level.FINE, LEVEL.CONFIG Session.logVerbose Level.INFO Session.logMessage Level.WARNING Session.logWarning Level.SEVERE Session.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.
-
-