|
5 | 5 | import numbers |
6 | 6 | import os |
7 | 7 | import pprint |
| 8 | +import re |
8 | 9 | import shutil |
9 | 10 | import sys |
10 | 11 | import socket |
11 | | -from datetime import datetime |
| 12 | +import time |
12 | 13 |
|
13 | 14 | import reframe |
14 | 15 | import reframe.utility.color as color |
@@ -132,6 +133,34 @@ def close(self): |
132 | 133 | super().close() |
133 | 134 |
|
134 | 135 |
|
| 136 | +def _format_time_rfc3339(timestamp, datefmt): |
| 137 | + tz_suffix = time.strftime('%z', timestamp) |
| 138 | + tz_rfc3339 = tz_suffix[:-2] + ':' + tz_suffix[-2:] |
| 139 | + |
| 140 | + # Python < 3.7 truncates the `%`, whereas later versions don't |
| 141 | + return re.sub(r'(%)?\:z', tz_rfc3339, time.strftime(datefmt, timestamp)) |
| 142 | + |
| 143 | + |
| 144 | +class RFC3339Formatter(logging.Formatter): |
| 145 | + def formatTime(self, record, datefmt=None): |
| 146 | + datefmt = datefmt or self.default_time_format |
| 147 | + if '%:z' not in datefmt: |
| 148 | + return super().formatTime(record, datefmt) |
| 149 | + else: |
| 150 | + timestamp = self.converter(record.created) |
| 151 | + return _format_time_rfc3339(timestamp, datefmt) |
| 152 | + |
| 153 | + def format(self, record): |
| 154 | + datefmt = self.datefmt or self.default_time_format |
| 155 | + if record.check_job_completion_time is not None: |
| 156 | + ct = self.converter(record.check_job_completion_time) |
| 157 | + record.check_job_completion_time = _format_time_rfc3339( |
| 158 | + ct, datefmt |
| 159 | + ) |
| 160 | + |
| 161 | + return super().format(record) |
| 162 | + |
| 163 | + |
135 | 164 | def load_from_dict(logging_config): |
136 | 165 | if not isinstance(logging_config, collections.abc.Mapping): |
137 | 166 | raise TypeError('logging configuration is not a dict') |
@@ -177,8 +206,7 @@ def _create_file_handler(handler_config): |
177 | 206 | timestamp = handler_config.get('timestamp', None) |
178 | 207 | if timestamp: |
179 | 208 | basename, ext = os.path.splitext(filename) |
180 | | - filename = '%s_%s%s' % (basename, |
181 | | - datetime.now().strftime(timestamp), ext) |
| 209 | + filename = '%s_%s%s' % (basename, time.strftime(timestamp), ext) |
182 | 210 |
|
183 | 211 | append = handler_config.get('append', False) |
184 | 212 | return logging.handlers.RotatingFileHandler(filename, |
@@ -305,7 +333,7 @@ def _extract_handlers(handlers_list): |
305 | 333 | level = handler_config.get('level', 'debug').lower() |
306 | 334 | fmt = handler_config.get('format', '%(message)s') |
307 | 335 | datefmt = handler_config.get('datefmt', '%FT%T') |
308 | | - hdlr.setFormatter(logging.Formatter(fmt=fmt, datefmt=datefmt)) |
| 336 | + hdlr.setFormatter(RFC3339Formatter(fmt=fmt, datefmt=datefmt)) |
309 | 337 | hdlr.setLevel(_check_level(level)) |
310 | 338 | handlers.append(hdlr) |
311 | 339 |
|
@@ -428,11 +456,7 @@ def _update_check_extras(self): |
428 | 456 | if self.check.job: |
429 | 457 | self.extra['check_jobid'] = self.check.job.jobid |
430 | 458 | if self.check.job.completion_time: |
431 | | - # Use the logging handlers' date format to format |
432 | | - # completion_time |
433 | | - # NOTE: All handlers use the same date format |
434 | | - fmt = self.logger.handlers[0].formatter.datefmt |
435 | | - ct = self.check.job.completion_time.strftime(fmt) |
| 459 | + ct = self.check.job.completion_time |
436 | 460 | self.extra['check_job_completion_time'] = ct |
437 | 461 |
|
438 | 462 | def log_performance(self, level, tag, value, ref, |
|
0 commit comments