Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace structlog with logging #114

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
27 changes: 15 additions & 12 deletions src/arsenic/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
dimaqq marked this conversation as resolved.
Show resolved Hide resolved


def wrap_screen(data):
Expand Down Expand Up @@ -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
Expand All @@ -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":
Expand All @@ -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)
19 changes: 10 additions & 9 deletions src/arsenic/errors.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
10 changes: 4 additions & 6 deletions src/arsenic/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down