Work with newer versions of Eliot

This commit is contained in:
Itamar Turner-Trauring 2023-11-15 11:14:48 -05:00
parent 22192b5e09
commit 4b75b7c6e8
3 changed files with 17 additions and 22 deletions

View File

@ -125,5 +125,5 @@ if sys.platform == "win32":
initialize()
from eliot import to_file
from allmydata.util.eliotutil import eliot_json_encoder
to_file(open("eliot.log", "wb"), encoder=eliot_json_encoder)
from allmydata.util.eliotutil import BytesEliotJSONEncoder
to_file(open("eliot.log", "wb"), encoder=BytesEliotJSONEncoder)

View File

@ -29,6 +29,7 @@ from eliot import (
ILogger,
)
from eliot.testing import (
MemoryLogger,
swap_logger,
check_for_errors,
)
@ -38,7 +39,7 @@ from twisted.python.monkey import (
)
from ..util.eliotutil import (
MemoryLogger,
BytesEliotJSONEncoder
)
_NAME = Field.for_types(
@ -146,7 +147,7 @@ def with_logging(
"""
@wraps(test_method)
def run_with_logging(*args, **kwargs):
validating_logger = MemoryLogger()
validating_logger = MemoryLogger(encoder=BytesEliotJSONEncoder)
original = swap_logger(None)
try:
swap_logger(_TwoLoggers(original, validating_logger))

View File

@ -3,17 +3,6 @@ Tools aimed at the interaction between Tahoe-LAFS implementation and Eliot.
Ported to Python 3.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import (
unicode_literals,
print_function,
absolute_import,
division,
)
__all__ = [
"MemoryLogger",
@ -26,11 +15,6 @@ __all__ = [
"capture_logging",
]
from future.utils import PY2
if PY2:
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401
from six import ensure_text
from sys import (
stdout,
)
@ -42,6 +26,7 @@ from logging import (
)
from json import loads
from six import ensure_text
from zope.interface import (
implementer,
)
@ -65,7 +50,7 @@ from eliot.testing import (
MemoryLogger,
capture_logging,
)
from eliot.json import EliotJSONEncoder as eliot_json_encoder
from eliot.json import EliotJSONEncoder
from eliot._validation import (
ValidationError,
@ -94,6 +79,15 @@ from twisted.internet.defer import (
from twisted.application.service import Service
class BytesEliotJSONEncoder(EliotJSONEncoder):
"""Support encoding bytes."""
def default(self, o):
if isinstance(o, bytes):
return o.decode("utf-8", "backslashreplace")
return EliotJSONEncoder.default(self, o)
def validateInstanceOf(t):
"""
Return an Eliot validator that requires values to be instances of ``t``.
@ -310,7 +304,7 @@ class _DestinationParser(object):
rotateLength=rotate_length,
maxRotatedFiles=max_rotated_files,
)
return lambda reactor: FileDestination(get_file(), eliot_json_encoder)
return lambda reactor: FileDestination(get_file(), encoder=BytesEliotJSONEncoder)
_parse_destination_description = _DestinationParser().parse