com.ecyrd.speed4j.log
Class PeriodicalLog

java.lang.Object
  extended by com.ecyrd.speed4j.log.Log
      extended by com.ecyrd.speed4j.log.Slf4jLog
          extended by com.ecyrd.speed4j.log.PeriodicalLog
All Implemented Interfaces:
DynamicMBean

public class PeriodicalLog
extends Slf4jLog
implements DynamicMBean

A Periodical log which can also expose its attributes via JMX.

The JMX name is based on the name of the Log. So if you don't set it via Log.setName(String), you'll end up something that Speed4J picks up on its own. Normally, if you use the property file to configure Speed4J, this gets automatically assigned for you.

The PeriodicalLog starts a new Thread to collect the statistics. Don't forget to shut it down with a call to shutdown(), or else you might risk a memory leak. This is a common problem with e.g. web applications, where redeployment regularly causes these.

In a web app, you could set up your own ServletContextListener to ensure the proper shutdown:

  class MyListener implements ServletContextListener {
     public void contextInitialized(ServletContextEvent sce) {}
     
     public void contextDestroyed(ServletContextEvent sce) {
         StopWatchFactory.getInstance("myLoggerName").shutdown();
     }
  }
  
PeriodicalLog adds its own JVM-wide shutdown hook, so you don't need to prepare for that.3


Field Summary
 
Fields inherited from class com.ecyrd.speed4j.log.Slf4jLog
m_log
 
Constructor Summary
PeriodicalLog()
          Creates an instance of PeriodicalLog.
 
Method Summary
 Object getAttribute(String attribute)
           
 AttributeList getAttributes(String[] attributes)
           
 MBeanInfo getMBeanInfo()
           
 Object invoke(String actionName, Object[] params, String[] signature)
           
 void log(StopWatch sw)
          Logs using the INFO priority.
 void setAttribute(Attribute attribute)
           
 AttributeList setAttributes(AttributeList attributes)
           
 void setJmx(String value)
          Set the tags which are shown via JMX.
 void setPeriod(int periodSeconds)
          Set the logging period in seconds.
 void shutdown()
          Shuts down the collector thread and removes the JMX bean if it is registered.
 
Methods inherited from class com.ecyrd.speed4j.log.Slf4jLog
setSlf4jLogname
 
Methods inherited from class com.ecyrd.speed4j.log.Log
getName, isEnabled, setEnable, setName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PeriodicalLog

public PeriodicalLog()
Creates an instance of PeriodicalLog.

Method Detail

log

public void log(StopWatch sw)
Description copied from class: Slf4jLog
Logs using the INFO priority.

Overrides:
log in class Slf4jLog
Parameters:
sw - The StopWatch to log.

setJmx

public void setJmx(String value)
Set the tags which are shown via JMX.

Parameters:
value - A comma-separated list of tags which are shown via JMX.

shutdown

public void shutdown()
Shuts down the collector thread and removes the JMX bean if it is registered. It is very important to call this or else you risk a memory leak.

Overrides:
shutdown in class Log

setPeriod

public void setPeriod(int periodSeconds)
Set the logging period in seconds. For example, a value of 5 would log every 5 seconds, at 0,5,10,15,20,25,30,35,40,45,50, and 55 seconds after the full minute.

Parameters:
periodSeconds - The period in seconds.

getAttribute

public Object getAttribute(String attribute)
                    throws AttributeNotFoundException,
                           MBeanException,
                           ReflectionException
Specified by:
getAttribute in interface DynamicMBean
Throws:
AttributeNotFoundException
MBeanException
ReflectionException

getAttributes

public AttributeList getAttributes(String[] attributes)
Specified by:
getAttributes in interface DynamicMBean

getMBeanInfo

public MBeanInfo getMBeanInfo()
Specified by:
getMBeanInfo in interface DynamicMBean

invoke

public Object invoke(String actionName,
                     Object[] params,
                     String[] signature)
              throws MBeanException,
                     ReflectionException
Specified by:
invoke in interface DynamicMBean
Throws:
MBeanException
ReflectionException

setAttribute

public void setAttribute(Attribute attribute)
                  throws AttributeNotFoundException,
                         InvalidAttributeValueException,
                         MBeanException,
                         ReflectionException
Specified by:
setAttribute in interface DynamicMBean
Throws:
AttributeNotFoundException
InvalidAttributeValueException
MBeanException
ReflectionException

setAttributes

public AttributeList setAttributes(AttributeList attributes)
Specified by:
setAttributes in interface DynamicMBean


Copyright © 2011. All Rights Reserved.