Package io.ultreia.java4all.util
Class TimeLog
- java.lang.Object
-
- io.ultreia.java4all.util.TimeLog
-
public class TimeLog extends Object
Cette class permet de facilement trace le temps d'execution entre deux pointsusage
private final static final Log log = LogFactory.getLog(MyClass.class); private final static TimeLog timeLog = new TimeLog(MyClass.class, 5000, 1000); long start = timeLog.getTime(); ... // do some work ... start = timeLog.log(start, "MyMethod step 1", "do some work"); ... // do some work ... timeLog.log(start, "MyMethod step 2", "do other work"); System.out.println ("time: " + timeLog.getCallCount());You can configure log level in configuration file with:
log4j.logger.org.yo.MyClass=DEBUG log4j.logger.org.yo.MyClass.TimeLog=INFO
- Since:
- 1.0.12
- Author:
- Benjamin Poussin - poussin@codelutin.com, Tony Chemit - dev@tchemit.fr Created on 10/06/2021.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTimeLog.CallStat
-
Field Summary
Fields Modifier and Type Field Description protected Map<String,TimeLog.CallStat>callCountfor each method of all proxies, keep number of callprotected org.apache.logging.log4j.LoggertimeLogLog used to log timeprotected longtimeToLogInfotime to trigger log time in info level (ns) (default: 1s)protected longtimeToLogWarntime to trigger log time in warn level (ns) (default: 3s)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected StringcomputeLogMessage(String methodName, String msg, TimeLog.CallStat calls, long time)Map<String,TimeLog.CallStat>getCallCount()static longgetTime()return time in format acceptable forlog(long, long, String, String)method.longlog(long startNs, long stopNs, String methodName, String msg)add new tracelonglog(long startNs, String methodName)add new trace, stop time is automatically computed.longlog(long startNs, String methodName, String msg)add new trace, stop time is automatically computedvoidreset()voidsetTimeToLogInfo(long timeToLogInfoMs)voidsetTimeToLogWarn(long timeToLogWarnMs)
-
-
-
Field Detail
-
timeLog
protected org.apache.logging.log4j.Logger timeLog
Log used to log time
-
timeToLogInfo
protected long timeToLogInfo
time to trigger log time in info level (ns) (default: 1s)
-
timeToLogWarn
protected long timeToLogWarn
time to trigger log time in warn level (ns) (default: 3s)
-
callCount
protected Map<String,TimeLog.CallStat> callCount
for each method of all proxies, keep number of call
-
-
Constructor Detail
-
TimeLog
public TimeLog(String logName)
- Parameters:
logName- log category used to log time message. This category must be category used to log message in class that use this TimeLog (normally class name). TimeLog is added at the end.
-
TimeLog
public TimeLog(Class<?> logName)
- Parameters:
logName- log category used to log time message. This category must be category used to log message in class that use this TimeLog (normally class name)- Since:
- 2.1
-
TimeLog
public TimeLog(String logName, long timeToLogInfo, long timeToLogWarn)
- Parameters:
logName- log category used to log time message. This category must be category used to log message in class that use this TimeLog (normally class name)timeToLogInfo- time in millisecond after that we log infotimeToLogWarn- time in millisecond after that we log warn
-
TimeLog
public TimeLog(Class<?> logName, long timeToLogInfo, long timeToLogWarn)
- Parameters:
logName- log category used to log time message. This category must be category used to log message in class that use this TimeLog (normally class name)timeToLogInfo- time in millisecond after that we log infotimeToLogWarn- time in millisecond after that we log warn
-
-
Method Detail
-
getTime
public static long getTime()
return time in format acceptable forlog(long, long, String, String)method.- Returns:
- the current time in nanoseconds
-
setTimeToLogInfo
public void setTimeToLogInfo(long timeToLogInfoMs)
- Parameters:
timeToLogInfoMs- time in millisecond after that we log info
-
setTimeToLogWarn
public void setTimeToLogWarn(long timeToLogWarnMs)
- Parameters:
timeToLogWarnMs- time in millisecond after that we log warn
-
getCallCount
public Map<String,TimeLog.CallStat> getCallCount()
-
log
public long log(long startNs, String methodName)add new trace, stop time is automatically computed.- Parameters:
startNs- time returned bygetTime()methodmethodName- key name to store this time- Returns:
- time used as stop time, this permit to chain many add in same method to trace time.
-
log
public long log(long startNs, String methodName, String msg)add new trace, stop time is automatically computed- Parameters:
startNs- time returned bygetTime()methodmethodName- key name to store this timemsg- message to add to log- Returns:
- time used as stop time, this permit to chain many add in same method to trace time.
-
reset
public void reset()
-
computeLogMessage
protected String computeLogMessage(String methodName, String msg, TimeLog.CallStat calls, long time)
-
-