Skip to content

Commit b8e1638

Browse files
committed
Support usecs in datefmt for logging
1 parent 6ca5b9c commit b8e1638

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

docs/config_reference.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,8 @@ All logging handlers share the following set of common attributes:
13381338

13391339
.. py:attribute:: logging.handlers.datefmt
13401340
1341+
:default: ``"%FT%T.%f"``
1342+
13411343
.. object:: logging.handlers_perflog.datefmt
13421344

13431345
:required: No
@@ -1347,6 +1349,13 @@ All logging handlers share the following set of common attributes:
13471349
There are two timestamp fields available: ``%(asctime)s`` and ``%(check_job_completion_time)s``.
13481350
In addition to the format directives supported by the standard library's `time.strftime() <https://docs.python.org/3.8/library/time.html#time.strftime>`__ function, ReFrame allows you to use the ``%:z`` directive -- a GNU ``date`` extension -- that will print the time zone difference in a RFC3339 compliant way, i.e., ``+/-HH:MM`` instead of ``+/-HHMM``.
13491351

1352+
.. note::
1353+
1354+
The default format for non-perflog log handlers includes now the microseconds.
1355+
Note also that the ``%f`` format string is not supported in ``datefmt`` for versions < 4.9.
1356+
1357+
.. versionchanged:: 4.9
1358+
13501359

13511360
.. _file-handler:
13521361

reframe/core/logging.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import sys
1818
import time
1919
import urllib
20+
from datetime import datetime
2021

2122
import reframe.utility.color as color
2223
import reframe.utility.jsonext as jsonext
@@ -301,11 +302,18 @@ def close(self):
301302

302303

303304
def _format_time_rfc3339(timestamp, datefmt):
304-
tz_suffix = time.strftime('%z', timestamp)
305-
tz_rfc3339 = tz_suffix[:-2] + ':' + tz_suffix[-2:]
305+
if timestamp is None:
306+
timestamp = time.time()
306307

307-
# Python < 3.7 truncates the `%`, whereas later versions don't
308-
return re.sub(r'(%)?\:z', tz_rfc3339, time.strftime(datefmt, timestamp))
308+
date = datetime.fromtimestamp(timestamp).astimezone()
309+
if r'%:z' in datefmt:
310+
tz_suffix = date.strftime(r'%z')
311+
tz_rfc3339 = tz_suffix[:-2] + ':' + tz_suffix[-2:]
312+
313+
# Python < 3.7 truncates the `%`, whereas later versions don't
314+
return re.sub(r'(%)?\:z', tz_rfc3339, date.strftime(datefmt))
315+
316+
return date.strftime(datefmt)
309317

310318

311319
def _xfmt(val):
@@ -399,7 +407,7 @@ def formatMessage(self, record):
399407
ct = record.check_job_completion_time_unix
400408
if ct is not None:
401409
record_proxy['check_job_completion_time'] = _format_time_rfc3339(
402-
time.localtime(ct), datefmt
410+
ct, datefmt
403411
)
404412

405413
try:
@@ -412,11 +420,7 @@ def formatMessage(self, record):
412420
class RFC3339Formatter(CheckFieldFormatter):
413421
def formatTime(self, record, datefmt=None):
414422
datefmt = datefmt or self.default_time_format
415-
if '%:z' not in datefmt:
416-
return super().formatTime(record, datefmt)
417-
else:
418-
timestamp = self.converter(record.created)
419-
return _format_time_rfc3339(timestamp, datefmt)
423+
return _format_time_rfc3339(record.created, datefmt)
420424

421425

422426
def _create_logger(site_config, *handlers_groups):
@@ -926,8 +930,7 @@ def _update_check_extras(self):
926930
# Add special extras
927931
self.extra['check_info'] = self.check.info()
928932
self.extra['check_job_completion_time'] = _format_time_rfc3339(
929-
time.localtime(self.extra['check_job_completion_time_unix']),
930-
'%FT%T%:z'
933+
self.extra['check_job_completion_time_unix'], r'%FT%T%:z'
931934
)
932935

933936
def log_performance(self, level, task, msg=None, multiline=False):

reframe/frontend/reporting/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def update_run_stats(self, stats):
395395

396396
if entry['job_completion_time_unix']:
397397
entry['job_completion_time'] = _format_time_rfc3339(
398-
time.localtime(entry['job_completion_time_unix']),
398+
entry['job_completion_time_unix'],
399399
'%FT%T%:z'
400400
)
401401

reframe/schemas/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,13 +627,14 @@
627627
"logging/handlers*/*_level": "info",
628628
"logging/handlers*/*_format": "%(message)s",
629629
"logging/handlers*/*_format_perfvars": "",
630-
"logging/handlers*/*_datefmt": "%FT%T",
631630
"logging/handlers*/file_append": false,
632631
"logging/handlers*/file_name": "",
633632
"logging/handlers*/file_timestamp": false,
634633
"logging/handlers*/stream_name": "stdout",
635634
"logging/handlers*/syslog_socktype": "udp",
636635
"logging/handlers*/syslog_facility": "user",
636+
"logging/handlers/*_datefmt": "%FT%T.%f",
637+
"logging/handlers_perflog/datefmt": "%FT%T",
637638
"logging/handlers_perflog/filelog_append": true,
638639
"logging/handlers_perflog/filelog_basedir": "./perflogs",
639640
"logging/handlers_perflog/filelog_ignore_keys": [],

0 commit comments

Comments
 (0)