Class TimeLog


  • public class TimeLog
    extends Object
    Cette class permet de facilement trace le temps d'execution entre deux points

    usage

     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.
    • 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)
    • 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 info
        timeToLogWarn - 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 info
        timeToLogWarn - time in millisecond after that we log warn
    • Method Detail

      • getTime

        public static long getTime()
        return time in format acceptable for log(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
      • log

        public long log​(long startNs,
                        String methodName)
        add new trace, stop time is automatically 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,
                        String methodName,
                        String msg)
        add new trace, stop time is automatically 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,
                        String methodName,
                        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)
      • reset

        public void reset()