Update logger.py to use new Twisted log api. No change in evennia-side usage

This commit is contained in:
Griatch 2022-02-13 21:14:33 +01:00
parent e9212ff898
commit 42ee0db1da
6 changed files with 230 additions and 210 deletions

View file

@ -15,7 +15,7 @@ from os.path import dirname, abspath
from twisted.application import internet, service
from twisted.internet.task import LoopingCall
from twisted.internet import protocol, reactor
from twisted.python.log import ILogObserver
from twisted.logger import globalLogPublisher
import django
@ -245,16 +245,17 @@ class Portal(object):
# what to execute from.
application = service.Application("Portal")
# custom logging
if "--nodaemon" not in sys.argv:
if ("--nodaemon" not in sys.argv
and not hasattr(settings, "_TEST_ENVIRONMENT") and settings._TEST_ENVIRONMENT):
# custom logging
logfile = logger.WeeklyLogFile(
os.path.basename(settings.PORTAL_LOG_FILE),
os.path.dirname(settings.PORTAL_LOG_FILE),
day_rotation=settings.PORTAL_LOG_DAY_ROTATION,
max_size=settings.PORTAL_LOG_MAX_SIZE,
)
application.setComponent(ILogObserver, logger.PortalLogObserver(logfile).emit)
globalLogPublisher.addObserver(logger.GetPortalLogObserver()(logfile))
# The main Portal server program. This sets up the database
# and is where we store all the other services.

View file

@ -16,7 +16,7 @@ from twisted.web import static
from twisted.application import internet, service
from twisted.internet import reactor, defer
from twisted.internet.task import LoopingCall
from twisted.python.log import ILogObserver
from twisted.logger import globalLogPublisher
import django
@ -647,7 +647,8 @@ except OperationalError:
# what to execute from.
application = service.Application("Evennia")
if "--nodaemon" not in sys.argv:
if ("--nodaemon" not in sys.argv
and not hasattr(settings, "_TEST_ENVIRONMENT") and settings._TEST_ENVIRONMENT):
# custom logging, but only if we are not running in interactive mode
logfile = logger.WeeklyLogFile(
os.path.basename(settings.SERVER_LOG_FILE),
@ -655,7 +656,8 @@ if "--nodaemon" not in sys.argv:
day_rotation=settings.SERVER_LOG_DAY_ROTATION,
max_size=settings.SERVER_LOG_MAX_SIZE,
)
application.setComponent(ILogObserver, logger.ServerLogObserver(logfile).emit)
globalLogPublisher.addObserver(logger.GetServerLogObserver()(logfile))
# The main evennia server program. This sets up the database
# and is where we store all the other services.

View file

@ -16,20 +16,27 @@ class EvenniaTestSuiteRunner(DiscoverRunner):
avoid running the large number of tests defined by Django
"""
def build_suite(self, test_labels, extra_tests=None, **kwargs):
"""
Build a test suite for Evennia. test_labels is a list of apps to test.
If not given, a subset of settings.INSTALLED_APPS will be used.
"""
def setup_test_environment(self, **kwargs):
# the portal looping call starts before the unit-test suite so we
# can't mock it - instead we stop it before starting the test - otherwise
# we'd get unclean reactor errors across test boundaries.
from evennia.server.portal.portal import PORTAL
PORTAL.maintenance_task.stop()
# initialize evennia itself
import evennia
evennia._init()
return super().build_suite(test_labels, extra_tests=extra_tests, **kwargs)
from django.conf import settings
# set testing flag while suite runs
settings._TEST_ENVIRONMENT = True
super().setup_test_environment(**kwargs)
def teardown_test_environment(self, **kwargs):
# remove testing flag after suite has run
from django.conf import settings
settings._TEST_ENVIRONMENT = False
super().teardown_test_environment(**kwargs)