public final class SmartInspectHandler extends Handler
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 and Description |
|---|
SmartInspectHandler()
Overloaded.
|
SmartInspectHandler(Session session)
Overloaded.
|
SmartInspectHandler(String sessionName)
Overloaded.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Overridden.
|
void |
flush()
Overridden.
|
Formatter |
getFormatter()
Overridden.
|
Session |
getSession()
Returns the session used to publish LogRecords.
|
void |
publish(LogRecord record)
Overriden.
|
void |
setFormatter(Formatter formatter)
Overridden.
|
getEncoding, getErrorManager, getFilter, getLevel, isLoggable, reportError, setEncoding, setErrorManager, setFilter, setLevelpublic SmartInspectHandler()
public SmartInspectHandler(String sessionName)
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.
sessionName - The name for the new sessionpublic SmartInspectHandler(Session session)
session - The session to usepublic Formatter getFormatter()
getFormatter in class Handlerpublic void setFormatter(Formatter formatter) throws SecurityException
Please note that this SmartInspectHandler only accepts instances of the SmartInspectFormatter class. Other formatters will silently be ignored.
setFormatter in class Handlerformatter - The formatter used to format LogRecordsSecurityException - If a security manager exists and if the caller does not have LoggingPermission("control")public Session getSession()
public void close()
throws SecurityException
close in class HandlerSecurityExceptionpublic void publish(LogRecord record)
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:
| 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:
| 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.
Copyright © 2023. All rights reserved.