Class TimeLog

java.lang.Object
org.nuiton.util.TimeLog

public class TimeLog
extends java.lang.Object
Cette classe permet de facilement trace le temps d'execution entre deux points usage
 final private static final Log log = LogFactory.getLog(MyClass.class);
 final static private 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.codelutin.MyClass=DEBUG log4j.logger.org.codelutin.MyClass.TimeLog=INFO
Since:
2.1
Author:
Benjamin Poussin - poussin@codelutin.com
See Also:
CallAnalyse
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    static class  TimeLog.CallStat  
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected java.util.Map<java.lang.String,​TimeLog.CallStat> callCount
    for each method of all proxies, keep number of call
    protected org.apache.commons.logging.Log timeLog
    loggueur used to log time
    protected long timeToLogInfo
    time to trigger log time in info level (ns) (default: 1s)
    protected long timeToLogWarn
    time to trigger log time in warn level (ns) (default: 3s)
  • Constructor Summary

    Constructors 
    Modifier Constructor Description
      TimeLog​(java.lang.Class<?> logName)  
      TimeLog​(java.lang.Class<?> logName, long timeToLogInfo, long timeToLogWarn)  
      TimeLog​(java.lang.String logName)  
      TimeLog​(java.lang.String logName, long timeToLogInfo, long timeToLogWarn)  
    protected TimeLog​(org.apache.commons.logging.Log logTime)  
  • Method Summary

    Modifier and Type Method Description
    java.util.Map<java.lang.String,​TimeLog.CallStat> getCallCount()  
    static long getTime()
    return time in format acceptable for log(long, long, String, String) method.
    long log​(long startNs, long stopNs, java.lang.String methodName, java.lang.String msg)
    add new trace
    long log​(long startNs, java.lang.String methodName)
    add new trace, stop time is automaticaly computed.
    long log​(long startNs, java.lang.String methodName, java.lang.String msg)
    add new trace, stop time is automaticaly computed
    void setTimeToLogInfo​(long timeToLogInfoMs)  
    void setTimeToLogWarn​(long timeToLogWarnMs)  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • timeLog

      protected org.apache.commons.logging.Log timeLog
      loggueur 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 java.util.Map<java.lang.String,​TimeLog.CallStat> callCount
      for each method of all proxies, keep number of call
  • Constructor Details

    • TimeLog

      protected TimeLog​(org.apache.commons.logging.Log logTime)
      Parameters:
      logTime - log used to log time message.
    • TimeLog

      public TimeLog​(java.lang.String logName)
      Parameters:
      logName - log category used to log time message. This category must be category used to log message in classe that use this TimeLog (normaly class name). TimeLog is added at the end.
    • TimeLog

      public TimeLog​(java.lang.Class<?> logName)
      Parameters:
      logName - log category used to log time message. This category must be category used to log message in classe that use this TimeLog (normaly class name)
      Since:
      2.1
    • TimeLog

      public TimeLog​(java.lang.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 classe that use this TimeLog (normaly class name)
      timeToLogInfo - time in milliseconde after that we log info
      timeToLogWarn - time in milliseconde after that we log warn
    • TimeLog

      public TimeLog​(java.lang.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 classe that use this TimeLog (normaly class name)
      timeToLogInfo - time in milliseconde after that we log info
      timeToLogWarn - time in milliseconde after that we log warn
  • Method Details

    • setTimeToLogInfo

      public void setTimeToLogInfo​(long timeToLogInfoMs)
      Parameters:
      timeToLogInfoMs - time in milliseconde after that we log info
    • setTimeToLogWarn

      public void setTimeToLogWarn​(long timeToLogWarnMs)
      Parameters:
      timeToLogWarnMs - time in milliseconde after that we log warn
    • getCallCount

      public java.util.Map<java.lang.String,​TimeLog.CallStat> getCallCount()
    • getTime

      public static long getTime()
      return time in format acceptable for log(long, long, String, String) method.
      Returns:
      the current time in nanoseconds
    • log

      public long log​(long startNs, java.lang.String methodName)
      add new trace, stop time is automaticaly computed.
      Parameters:
      startNs - time returned by getTime() method
      methodName - 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, java.lang.String methodName, java.lang.String msg)
      add new trace, stop time is automaticaly computed
      Parameters:
      startNs - time returned by getTime() method
      methodName - key name to store this time
      msg - message to add to log
      Returns:
      time used as stop time, this permit to chain many add in same method to trace time.
    • log

      public long log​(long startNs, long stopNs, java.lang.String methodName, java.lang.String msg)
      add new trace
      Parameters:
      startNs - time returned by getTime() method
      stopNs - time returned by getTime() method
      methodName - key name to store this time
      msg - message to add to log
      Returns:
      time used as stop time (stopNs)