Custom logging for tests to spot possible performance bottlenecks easier

This commit is contained in:
shortcutme 2017-10-04 13:30:17 +02:00
parent 8e2be5cfe2
commit be755fe25e
No known key found for this signature in database
GPG Key ID: 5B63BAE6CB9613AE
1 changed files with 25 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import logging
import json
import shutil
import gc
import datetime
import pytest
import mock
@ -27,8 +28,32 @@ sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + "/..")) # Import
from Config import config
config.argv = ["none"] # Dont pass any argv to config parser
config.parse(silent=True) # Plugins need to access the configuration
config.action = "test"
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
# Set custom formatter with realative time format (via: https://stackoverflow.com/questions/31521859/python-logging-module-time-since-last-log)
class TimeFilter(logging.Filter):
def filter(self, record):
try:
last = self.last
except AttributeError:
last = record.relativeCreated
delta = datetime.datetime.fromtimestamp(record.relativeCreated/1000.0) - datetime.datetime.fromtimestamp(last/1000.0)
record.relative = '{0:.3f}'.format(delta.seconds + delta.microseconds/1000000.0)
self.last = record.relativeCreated
return True
log = logging.getLogger()
fmt = logging.Formatter(fmt='+%(relative)ss %(levelname)-8s %(name)s %(message)s')
[hndl.addFilter(TimeFilter()) for hndl in log.handlers]
[hndl.setFormatter(fmt) for hndl in log.handlers]
# Load plugins
from Plugin import PluginManager
PluginManager.plugin_manager.loadPlugins()
config.loadPlugins()