From 718e1787ea2800ff8ea48b2993f34e05b109ca78 Mon Sep 17 00:00:00 2001 From: Dima Tisnek Date: Tue, 12 Jan 2021 11:00:48 +0900 Subject: [PATCH 1/3] blind attempt to yank structlog --- .pre-commit-config.yaml | 2 +- pyproject.toml | 1 - src/arsenic/connection.py | 27 +++++++++++++++------------ src/arsenic/errors.py | 19 ++++++++++--------- src/arsenic/subprocess.py | 10 ++++------ 5 files changed, 30 insertions(+), 29 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f63ae45..d9c757a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/ambv/black - rev: 18.9b0 + rev: 20.8b1 hooks: - id: black args: [--line-length=88, --safe] diff --git a/pyproject.toml b/pyproject.toml index b41152b..a85a3fb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,7 +26,6 @@ classifiers = [ [tool.poetry.dependencies] python = "^3.7" attrs = ">=17.4.0" -structlog = "^20.1.0" aiohttp = ">=2" [tool.poetry.dev-dependencies] diff --git a/src/arsenic/connection.py b/src/arsenic/connection.py index eb0408f..ab325fa 100644 --- a/src/arsenic/connection.py +++ b/src/arsenic/connection.py @@ -4,17 +4,17 @@ from functools import wraps from io import BytesIO from json import JSONDecodeError +from logging import getLogger from pathlib import Path from typing import Any, Tuple from urllib.parse import urlparse, urlunparse from zipfile import ZIP_DEFLATED, ZipFile from aiohttp import ClientSession -from structlog import get_logger from arsenic import errors, constants -log = get_logger() +log = getLogger(__name__) def wrap_screen(data): @@ -98,7 +98,8 @@ async def request( body = json.dumps(data) if data is not None else None full_url = self.prefix + url log.info( - "request", url=strip_auth(full_url), method=method, header=header, body=body + "request %s", + dict(url=strip_auth(full_url), method=method, header=header, body=body), ) async with self.session.request( url=full_url, method=method, headers=header, data=body, timeout=timeout @@ -107,22 +108,24 @@ async def request( try: data = json.loads(response_body) except JSONDecodeError as exc: - log.error("json-decode", body=response_body) + log.error("json-decode %s", dict(body=response_body)) data = {"error": "!internal", "message": str(exc), "stacktrace": ""} wrap_screen(data) log.info( - "response", - url=strip_auth(full_url), - method=method, - body=body, - response=response, - data=data, + "response %s", + dict( + url=strip_auth(full_url), + method=method, + body=body, + response=response, + data=data, + ), ) check_response_error(data=data, status=response.status) return response.status, data async def upload_file(self, path: Path) -> Path: - log.info("upload-file", path=path, resolved_path=path) + log.info("upload-file %s", dict(path=path, resolved_path=path)) return path def prefixed(self, prefix: str) -> "Connection": @@ -139,5 +142,5 @@ async def upload_file(self, path: Path) -> Path: url="/file", method="POST", data={"file": content} ) value = unwrap(data.get("value", None)) - log.info("upload-file", path=path, resolved_path=value) + log.info("upload-file %s", dict(path=path, resolved_path=value)) return Path(value) diff --git a/src/arsenic/errors.py b/src/arsenic/errors.py index c405404..59aa6f6 100644 --- a/src/arsenic/errors.py +++ b/src/arsenic/errors.py @@ -1,8 +1,7 @@ +from logging import getLogger from typing import Union, Dict, Type, Any -from structlog import get_logger - -log = get_logger() +log = getLogger(__name__) class ArsenicError(Exception): @@ -95,11 +94,13 @@ def raise_exception(data: Dict[str, Any], status: int): screen = data.get("screen", None) exception_class = get(error) log.error( - "error", - type=exception_class, - message=message, - stacktrace=stacktrace, - data=data, - status=status, + "error %s", + dict( + type=exception_class, + message=message, + stacktrace=stacktrace, + data=data, + status=status, + ), ) raise exception_class(message, screen, stacktrace) diff --git a/src/arsenic/subprocess.py b/src/arsenic/subprocess.py index 33fa510..518545f 100644 --- a/src/arsenic/subprocess.py +++ b/src/arsenic/subprocess.py @@ -3,13 +3,11 @@ import os import subprocess import sys +from logging import getLogger from typing import List, TypeVar from asyncio.subprocess import DEVNULL, PIPE -from structlog import get_logger - -log = get_logger() - +log = getLogger(__name__) P = TypeVar("P") @@ -66,7 +64,7 @@ async def stop_process(self, process): try: await asyncio.wait_for(process.communicate(), 1) except asyncio.futures.TimeoutError: - log.warn("could not terminate process", process=process, impl=self) + log.warn("could not terminate process %s", dict(process=process, impl=self)) class ThreadedSubprocessImpl(BaseSubprocessImpl): @@ -106,7 +104,7 @@ def _stop_process(self, process: subprocess.Popen): try: process.communicate(timeout=1) except subprocess.TimeoutExpired: - log.warn("could not terminate process", process=process, impl=self) + log.warn("could not terminate process %s", dict(process=process, impl=self)) def get_subprocess_impl() -> BaseSubprocessImpl: From 97ffed0b39008b9b3cae1d1cb52caf4f0618aba1 Mon Sep 17 00:00:00 2001 From: Dima Tisnek Date: Tue, 12 Jan 2021 11:25:58 +0900 Subject: [PATCH 2/3] upgrade deps --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index a85a3fb..a071992 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,14 +26,14 @@ classifiers = [ [tool.poetry.dependencies] python = "^3.7" attrs = ">=17.4.0" -aiohttp = ">=2" +aiohttp = ">=3" [tool.poetry.dev-dependencies] jinja2 = "^2.11.2" pytest = "^6.0.1" pytest-asyncio = "^0.14.0" sphinx = "^3.2.1" -pillow = "^7.2.0" +pillow = "^8.1.0" pytest-cov = "^2.10.1" asyncio-extras = "^1.3.2" pre-commit = "^2.7.1" From 1aa4605407c19b1104ea7aedacc4724b6518ef0f Mon Sep 17 00:00:00 2001 From: Dima Tisnek Date: Tue, 12 Jan 2021 11:27:15 +0900 Subject: [PATCH 3/3] start with coarse-grained logging --- src/arsenic/connection.py | 2 +- src/arsenic/errors.py | 2 +- src/arsenic/subprocess.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/arsenic/connection.py b/src/arsenic/connection.py index ab325fa..b339a0f 100644 --- a/src/arsenic/connection.py +++ b/src/arsenic/connection.py @@ -14,7 +14,7 @@ from arsenic import errors, constants -log = getLogger(__name__) +log = getLogger(__package__) def wrap_screen(data): diff --git a/src/arsenic/errors.py b/src/arsenic/errors.py index 59aa6f6..76ad7de 100644 --- a/src/arsenic/errors.py +++ b/src/arsenic/errors.py @@ -1,7 +1,7 @@ from logging import getLogger from typing import Union, Dict, Type, Any -log = getLogger(__name__) +log = getLogger(__package__) class ArsenicError(Exception): diff --git a/src/arsenic/subprocess.py b/src/arsenic/subprocess.py index 518545f..c2d54a9 100644 --- a/src/arsenic/subprocess.py +++ b/src/arsenic/subprocess.py @@ -7,7 +7,7 @@ from typing import List, TypeVar from asyncio.subprocess import DEVNULL, PIPE -log = getLogger(__name__) +log = getLogger(__package__) P = TypeVar("P")