Ara*_*yan 10 java groovy logging
在java/groovy应用程序中,我正在使用org.slf4j.Logger
我喜欢记录方法执行时间并建议使用以下代码
def startTime
LOGGER.isDebugEnabled() {
startTime = System.currentTimeMillis()
}
doSomething()
LOGGER.debug("Execution took {}ms", (System.currentTimeMillis() - startTime))
Run Code Online (Sandbox Code Playgroud)
我认为这段代码很"难看".谁能提出更优雅的建议?
您可以使用此类来计算经过的时间.
public class StopWatch {
/* Private Instance Variables */
/** Stores the start time when an object of the StopWatch class is initialized. */
private long startTime;
/**
* Custom constructor which initializes the {@link #startTime} parameter.
*/
public StopWatch() {
startTime = System.currentTimeMillis();
}
/**
* Gets the elapsed time (in seconds) since the time the object of StopWatch was initialized.
*
* @return Elapsed time in seconds.
*/
public double getElapsedTime() {
long endTime = System.currentTimeMillis();
return (double) (endTime - startTime) / (1000);
}
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
public class SWTest {
public static void main(String[] args) {
StopWatch stopWatch = new StopWatch();
doSomething();
LOGGER.debug("Execution took in seconds: ", (stopWatch.getElapsedTime());
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想让代码看起来不那么难看:
更改
def startTime
LOGGER.isDebugEnabled() {
startTime = System.currentTimeMillis()
}
Run Code Online (Sandbox Code Playgroud)
至
def startTime = System.currentTimeMillis()
Run Code Online (Sandbox Code Playgroud)
我不是isDebugEnabled这些衬里的粉丝.没有它,你不会看到任何性能差异.
| 归档时间: |
|
| 查看次数: |
25172 次 |
| 最近记录: |