Skip to content

Instantly share code, notes, and snippets.

@mrts
Last active August 29, 2015 14:13
Show Gist options
  • Save mrts/7e02b312be2d2e0c9e17 to your computer and use it in GitHub Desktop.
Save mrts/7e02b312be2d2e0c9e17 to your computer and use it in GitHub Desktop.
package util;
import org.apache.log4j.Logger;
import org.springframework.util.StopWatch;
public class DebugLogStopwatch implements AutoCloseable {
Logger logger;
StopWatch stopwatch;
public DebugLogStopwatch(Logger log, String label) {
logger = log;
stopwatch = new StopWatch(label);
stopwatch.start();
}
@Override
public void close() throws Exception {
stopwatch.stop();
if (logger.isDebugEnabled()) {
logger.debug(stopwatch.prettyPrint());
}
}
}
/* Usage:
try (DebugLogStopwatch sw = new DebugLogStopwatch(logger, "label")) {
...
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment