-
Notifications
You must be signed in to change notification settings - Fork 35
Interval Logging
To assist with application diagnostics and security forensics an Interval Logging feature is available. To run the interval logger quickly with default settings you can add this to your application,
IntervalLoggerController wd = SecurityLoggingFactory.getControllerInstance();
wd.start();
The code starts a background thread that generates a log message every 15 seconds that prints a message similar to the following,
20:10:10.204 [Thread-0] INFO Watchdog: MemoryTotal=64.5MB, FreeMemory=58.2MB, MaxMemory=947.7MB, Threads Total=5, Threads New=0, Threads Runnable=3, Threads Blocked=0, Threads Waiting=2, Threads Terminated=0
While some of the information is readily available from operating system features like DTRACE the interval logger provides additional value since it is able to capture information about your applications state. The following example is slightly more sophisticated but demonstrates how to remove a default property from the interval logger and add a new property that tracks the number of open file handles.
IntervalLoggerController wd = SecurityLoggingFactory.getControllerInstance();
// Add support for security markers
wd.setStatusMessageView( new DefaultIntervalLoggerView() {
@Override
public void logMessage( String message ) {
logger.info( SecurityMarkers.RESTRICTED, message );
}
});
// Add a new property to the list of current properties
DefaultIntervalLoggerModel model = new DefaultIntervalLoggerModel();
model.addProperty( new DefaultIntervalProperty("OpenFiles") {
public void refresh() {
// restricted class, display open file handle count on *nix if we can.
OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
if(os instanceof UnixOperatingSystemMXBean)
value = Long.toString(((UnixOperatingSystemMXBean) os).getOpenFileDescriptorCount());
}
}
);
// Remove default property from middle of the list like ThreadNew
IntervalProperty[] properties = model.getProperties();
for ( IntervalProperty i : properties ) {
if( i.getName().equals("ThreadNew") )
model.removeProperty(i);
}
wd.setStatusMessageModel(model);
// Update interval to every 3 seconds
wd.start(3000);
Of course, the number of open file handles is operating system property data and not specific to the virtual machine. It's only provide as an example. Better examples of properties would be, the number of users logged on to the application, active users vs sleeping users, application operations per second, application errors, etc.