Skip to content

Commit ed179e0

Browse files
committed
test: apply microsecond precision to test framework logging
This makes it easier to compare test and bitcoind logs - combine_logs.py orders by timestamp and will be more precise.
1 parent e872a56 commit ed179e0

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

test/functional/test_framework/test_framework.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import configparser
88
from enum import Enum
99
import argparse
10+
from datetime import datetime, timezone
1011
import logging
1112
import os
1213
import platform
@@ -837,9 +838,16 @@ def _start_logging(self):
837838
# User can provide log level as a number or string (eg DEBUG). loglevel was caught as a string, so try to convert it to an int
838839
ll = int(self.options.loglevel) if self.options.loglevel.isdigit() else self.options.loglevel.upper()
839840
ch.setLevel(ll)
841+
840842
# Format logs the same as bitcoind's debug.log with microprecision (so log files can be concatenated and sorted)
841-
formatter = logging.Formatter(fmt='%(asctime)s.%(msecs)03d000Z %(name)s (%(levelname)s): %(message)s', datefmt='%Y-%m-%dT%H:%M:%S')
842-
formatter.converter = time.gmtime
843+
class MicrosecondFormatter(logging.Formatter):
844+
def formatTime(self, record, _=None):
845+
dt = datetime.fromtimestamp(record.created, timezone.utc)
846+
return dt.strftime('%Y-%m-%dT%H:%M:%S.%f')
847+
848+
formatter = MicrosecondFormatter(
849+
fmt='%(asctime)sZ %(name)s (%(levelname)s): %(message)s',
850+
)
843851
fh.setFormatter(formatter)
844852
ch.setFormatter(formatter)
845853
# add the handlers to the logger

0 commit comments

Comments
 (0)