public class LoggingStopWatch extends StopWatch
LoggingStopWatch stopWatch = new LoggingStopWatch(); ...some code stopWatch.stop("codeBlock1"); //calling stop writes the StopWatch string to std err.This class just writes all StopWatch messages to the standard error stream, but subclasses will use Loggers from various logging frameworks to persist the StopWatch.
DEFAULT_LOGGER_NAME
Constructor and Description |
---|
LoggingStopWatch()
Creates a LoggingStopWatch with a blank tag, no message and started at the instant of creation.
|
LoggingStopWatch(long startTime,
long elapsedTime,
String tag,
String message)
Creates a LoggingStopWatch with a specified start and elapsed time, tag, and message.
|
LoggingStopWatch(String tag)
Creates a LoggingStopWatch with the specified tag, no message and started at the instant of creation.
|
LoggingStopWatch(String tag,
String message)
Creates a LoggingStopWatch with the specified tag and message, started an the instant of creation.
|
Modifier and Type | Method and Description |
---|---|
LoggingStopWatch |
clone() |
String |
getNormalSuffix()
The suffix to append to the tag if normalAndSlowSuffixesEnabled=true and elapsedTime < timeThreshold and timeThreshold > 0.
|
String |
getSlowSuffix()
The suffix to append to the tag if normalAndSlowSuffixesEnabled=true and elapsedTime >= timeThreshold and timeThreshold > 0.
|
String |
getTag()
Gets the tag used to group this StopWatch instance with other instances used to time the same code block.
|
long |
getTimeThreshold()
Gets a threshold level, in milliseconds, below which logging calls will not be made.
|
boolean |
isLogging()
Determines whether or not logging is currently enabled for normal log messages for this StopWatch.
|
boolean |
isNormalAndSlowSuffixesEnabled()
Determines whether or not to append normalSuffix or slowSuffix to every tag logged by this stopwatch.
|
String |
lap(String tag,
String message,
Throwable exception)
Identical to
StopWatch.lap(String, String) , but also allows you to specify an exception to be logged. |
String |
lap(String tag,
Throwable exception)
Identical to
StopWatch.lap(String) , but also allows you to specify an exception to be logged. |
protected void |
log(String stopWatchAsString,
Throwable exception)
This log method can be overridden by subclasses in order to persist the StopWatch, for example by using a
log4j Logger.
|
LoggingStopWatch |
setMessage(String message)
Sends a message on this StopWatch instance to be printed when this instance is logged.
|
LoggingStopWatch |
setNormalAndSlowSuffixesEnabled(boolean normalAndSlowSuffixesEnabled)
Sets whether to append normalSuffix and slowSuffix when timeThreshold > 0 AND elapsedTime >= timeThreshold
|
LoggingStopWatch |
setNormalSuffix(String normalSuffix)
Sets the suffix to use when normalAndSlowSuffixesEnabled=true and timeThreshold > 0 and elapsedTime < timeThreshold.
|
LoggingStopWatch |
setSlowSuffix(String slowSuffix)
Sets the suffix to use when normalAndSlowSuffixesEnabled=true and timeThreshold > 0 and elapsedTime >= timeThreshold.
|
LoggingStopWatch |
setTag(String tag)
Sets the grouping tag for this StopWatch instance.
|
LoggingStopWatch |
setTimeThreshold(long timeThreshold)
Sets a threshold level, in milliseconds, below which logging calls will not be made.
|
String |
stop()
This stop method is overridden to perform the logging itself instead of needing to make a separate call to
persist the timing information.
|
String |
stop(String tag,
String message,
Throwable exception)
Identical to
StopWatch.stop(String, String) , but also allows you to specify an exception to be logged. |
String |
stop(String tag,
Throwable exception)
Identical to
StopWatch.stop(String) , but also allows you to specify an exception to be logged. |
String |
stop(Throwable exception)
In cases where a code block terminated by throwing an exception, you may wish to have the exception logged in
addition to the time it took to execute the block, in which case this method will write out the exception's
stack trace in addition to the StopWatch timing method.
|
equals, getElapsedTime, getMessage, getStartTime, hashCode, lap, lap, start, start, start, stop, stop, toString
public LoggingStopWatch()
public LoggingStopWatch(String tag)
tag
- The tag name for this timing call. Tags are used to group timing logs, thus each block of code being
timed should have a unique tag. Note that tags can take a hierarchical format using dot notation.public LoggingStopWatch(String tag, String message)
tag
- The tag name for this timing call. Tags are used to group timing logs, thus each block of code
being timed should have a unique tag. Note that tags can take a hierarchical format using dot
notation.message
- Additional text to be printed with the logging statement of this LoggingStopWatch.public LoggingStopWatch(long startTime, long elapsedTime, String tag, String message)
startTime
- The start time in millisecondselapsedTime
- The elapsed time in millisecondstag
- The tag used to group timing logs of the same code blockmessage
- Additional message textpublic long getTimeThreshold()
public LoggingStopWatch setTimeThreshold(long timeThreshold)
JmxAttributeStatisticsAppender
in the logging configuration to be notified when
times are outside acceptable thresholds.timeThreshold
- The minimum elapsed time, in milliseconds, below which log calls will not be made.JmxAttributeStatisticsAppender.getNotificationThresholds()
public boolean isNormalAndSlowSuffixesEnabled()
public LoggingStopWatch setNormalAndSlowSuffixesEnabled(boolean normalAndSlowSuffixesEnabled)
normalAndSlowSuffixesEnabled
- true enables logging extra suffixes to normal and slow events; false (default) suppresses the suffixespublic String getNormalSuffix()
public LoggingStopWatch setNormalSuffix(String normalSuffix)
normalSuffix
- the suffix to append if normalAndSlowSuffixesEnabled and the elapsedtime is under the thresholdpublic String getSlowSuffix()
public LoggingStopWatch setSlowSuffix(String slowSuffix)
slowSuffix
- the suffix to append if normalAndSlowSuffixesEnabled and the elapsedtime is under the thresholdpublic String getTag()
StopWatch
public LoggingStopWatch setTag(String tag)
StopWatch
public LoggingStopWatch setMessage(String message)
StopWatch
setMessage
in class StopWatch
message
- The message associated with this StopWatch, which may be null.public String stop()
public String stop(Throwable exception)
exception
- The exception that was thrown by the timed code blockpublic String stop(String tag, Throwable exception)
StopWatch.stop(String)
, but also allows you to specify an exception to be logged.tag
- The grouping tag for this StopWatchexception
- The exception that was thrown by the timed code blockpublic String stop(String tag, String message, Throwable exception)
StopWatch.stop(String, String)
, but also allows you to specify an exception to be logged.tag
- The grouping tag for this StopWatchmessage
- A descriptive message about the timed blockexception
- The exception that was thrown by the timed code blockpublic String lap(String tag, Throwable exception)
StopWatch.lap(String)
, but also allows you to specify an exception to be logged.tag
- The grouping tag for the PREVIOUS code block that was timed.exception
- The exception that was thrown by the timed code block.public String lap(String tag, String message, Throwable exception)
StopWatch.lap(String, String)
, but also allows you to specify an exception to be logged.tag
- The grouping tag for the PREVIOUS code block that was timed.message
- A descriptive message about the timed blockexception
- The exception that was thrown by the timed code blockpublic boolean isLogging()
protected void log(String stopWatchAsString, Throwable exception)
stopWatchAsString
- The serialized StopWatch stringexception
- An exception, if any, that was also passed to the stop() or lap() methods - may be null.public LoggingStopWatch clone()
Copyright © 2008–2015 jperf. All rights reserved.