This is part of a solution for issue #113.

Due to the fact we work usually on a higly distributed
environment, displaying the messages using UTC is the
best way to help people to debug issues.

Consulting the logging module documentation:

http://docs.python.org/library/logging.html#formatter-objects

The way to make the timestamps to display UTC is to
change the converter function of the Formatter objects
to time.gmtime. So, create a specialized version of
the Formatter object, and set it for the logs stored on
disk. Console log formatting remains unchanged.

Signed-off-by: Lucas Meneghel Rodrigues <l...@redhat.com>
---
 client/common_lib/logging_config.py |   18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/client/common_lib/logging_config.py 
b/client/common_lib/logging_config.py
index cad5936..e5d7a3c 100644
--- a/client/common_lib/logging_config.py
+++ b/client/common_lib/logging_config.py
@@ -21,19 +21,27 @@ class AllowBelowSeverity(logging.Filter):
         return record.levelno < self.level
 
 
+class AutotestFormatter(logging.Formatter):
+    def __init__(self, fmt, datefmt, use_gmtime=False):
+        logging.Formatter.__init__(self, fmt, datefmt)
+        if use_gmtime:
+            self.converter = time.gmtime
+
+
 class LoggingConfig(object):
     global_level = logging.DEBUG
     stdout_level = logging.INFO
     stderr_level = logging.ERROR
 
-    file_formatter = logging.Formatter(
-        fmt='%(asctime)s %(levelname)-5.5s|%(module)10.10s:%(lineno)4.4d| '
+    file_formatter = AutotestFormatter(
+        fmt='%(asctime)s UTC %(levelname)-5.5s|%(module)10.10s:%(lineno)4.4d| '
             '%(message)s',
-        datefmt='%m/%d %H:%M:%S')
+        datefmt='%m/%d %H:%M:%S', use_gmtime=True)
 
-    console_formatter = logging.Formatter(
+    console_formatter = AutotestFormatter(
         fmt='%(asctime)s %(levelname)-5.5s| %(message)s',
-        datefmt='%H:%M:%S')
+        datefmt='%H:%M:%S', use_gmtime=False)
+
 
     def __init__(self, use_console=True):
         self.logger = logging.getLogger()
-- 
1.7.9.3

_______________________________________________
Autotest mailing list
Autotest@test.kernel.org
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

Reply via email to