We have had this since I started log4j. It is in both core and api as Timer.
You can do
Timer timer = new Timer(“Step1”);
….
….
timer.start()
…
…
…
timer.stop();
LOGGERr.debug(timer);
I had a version of this at one time that automatically logged when stop was
called. That could easily be done by doing:
Timer timer = new Timer(“Step 1”) {
public void stop() {
super();
LOGGER.debug(this);
}
Or we could make another version that does this. It might make sense to have
start and stop also return this so you could do:
Timer timer = new Timer(“Step 1).start();
….
….
Logger.debug(timer.stop());
This change would be backwards compatible so we could do that without any
problem.
Note that Timer also supports pause() and resume() if you want to eliminate the
time spent in called methods.
Ralph
> On May 1, 2017, at 6:56 PM, Gary Gregory <[email protected]> wrote:
>
> Hi All:
>
> I find myself writing a lot of methods like:
>
> LOGGER.debug("Doing things with {}", foo);
> final StopWatch watch = StopWatch.createStarted();
> // Do this
> // Do that
> // And then some
> watch.stop();
> LOGGER.debug("Did foo things in {}", watch);
>
> Where the stop watch is Apache Commons Lang's
> https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/time/StopWatch.html
>
> Any thoughts on making things less verbose?
>
> Making watch.stop() return "this" would save a line but I'd have to clone
> StopWatch to do that which I do not mind.
>
> Gary
>
> --
> E-Mail: [email protected] | [email protected]
> Java Persistence with Hibernate, Second Edition
> <https://www.amazon.com/gp/product/1617290459/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1617290459&linkCode=as2&tag=garygregory-20&linkId=cadb800f39946ec62ea2b1af9fe6a2b8>
>
> <http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1617290459>
> JUnit in Action, Second Edition
> <https://www.amazon.com/gp/product/1935182021/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1935182021&linkCode=as2&tag=garygregory-20&linkId=31ecd1f6b6d1eaf8886ac902a24de418%22>
>
> <http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1935182021>
> Spring Batch in Action
> <https://www.amazon.com/gp/product/1935182951/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1935182951&linkCode=%7B%7BlinkCode%7D%7D&tag=garygregory-20&linkId=%7B%7Blink_id%7D%7D%22%3ESpring+Batch+in+Action>
> <http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1935182951>
> Blog: http://garygregory.wordpress.com
> Home: http://garygregory.com/
> Tweet! http://twitter.com/GaryGregory