>From 09d1585704a75f54f0b60a560b1b87514739b1bd Mon Sep 17 00:00:00 2001
From: Nicolas Benes <nbenes@eso.org>
Date: Wed, 2 Sep 2020 15:51:48 +0200
Subject: [PATCH] Logging with custom clock

---
 Lib/logging/__init__.py | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index 0cfaec8..b5b4195 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -51,10 +51,15 @@ __date__    = "07 February 2010"
 #   Miscellaneous module data
 #---------------------------------------------------------------------------
 
+#
+#_clock_gettime is a callable to get the current time in seconds
+#
+_clock_gettime = time.time
+
 #
 #_startTime is used as the base when calculating the relative time of events
 #
-_startTime = time.time()
+_startTime = _clock_gettime()
 
 #
 #raiseExceptions is used to see if exceptions during handling should be
@@ -286,7 +291,7 @@ class LogRecord(object):
         """
         Initialize a logging record with interesting information.
         """
-        ct = time.time()
+        ct = _clock_gettime()
         self.name = name
         self.msg = msg
         #
@@ -536,8 +541,8 @@ class Formatter(object):
     %(lineno)d          Source line number where the logging call was issued
                         (if available)
     %(funcName)s        Function name
-    %(created)f         Time when the LogRecord was created (time.time()
-                        return value)
+    %(created)f         Time when the LogRecord was created (by default
+                        time.time() return value)
     %(asctime)s         Textual time when the LogRecord was created
     %(msecs)d           Millisecond portion of the creation time
     %(relativeCreated)d Time in milliseconds when the LogRecord was created,
@@ -1931,6 +1936,8 @@ def basicConfig(**kwargs):
               attached to the root logger are removed and closed, before
               carrying out the configuration as specified by the other
               arguments.
+    clock     If specified, this should be a callable that returns the current
+              time since an epoch in seconds.
     Note that you could specify a stream created using open(filename, mode)
     rather than passing the filename and mode in. However, it should be
     remembered that StreamHandler does not close its stream (since it may be
@@ -1960,6 +1967,11 @@ def basicConfig(**kwargs):
                 root.removeHandler(h)
                 h.close()
         if len(root.handlers) == 0:
+            clock = kwargs.pop("clock", None)
+            if clock is not None:
+                global _clock_gettime, _startTime
+                _clock_gettime = clock
+                _startTime = _clock_gettime()
             handlers = kwargs.pop("handlers", None)
             if handlers is None:
                 if "stream" in kwargs and "filename" in kwargs:
-- 
2.20.1


