Skip to content

Commit

Permalink
Use django-style logging support (#28)
Browse files Browse the repository at this point in the history
Problem:
The package defined its own root logger and this constrained
(unnecessarily) the useage by third parties.

Solution:
Added module-named loggers at each endpoint to allow ultimate
flexibility in configuring logging by the user.

Signed-off-by: Paul Hewlett <phewlett76@gmail.com>
  • Loading branch information
eccles authored Jun 28, 2021
1 parent 263354f commit 0395064
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 42 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,41 @@ else:

```

## Logging

Follows the Django model as described here: https://docs.djangoproject.com/en/3.2/topics/logging/

The base logger for this pacakage is rooted at "archivist" with subloggers for each endpoint:

- "archivist.archivist"
- "archivist.assets"
- ...

etc. etc.

Logging is configured by either defining a root logger with suitable handlers, formatters etc. or
by using dictionary configuration as described here: https://docs.python.org/3/library/logging.config.html#logging-config-dictschema

A recommended minimum configuration would be:

```python
import logging

logging.dictConfig({
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"console": {
"class": "logging.StreamHandler",
},
},
"root": {
"handlers": ["console"],
"level": "INFO",
},
})
```

# Development

## Pre-requisites
Expand Down
5 changes: 5 additions & 0 deletions archivist/access_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"""

import logging

from copy import deepcopy

from .constants import (
Expand All @@ -37,6 +39,8 @@
#: be changed.
DEFAULT_PAGE_SIZE = 500

LOGGER = logging.getLogger(__name__)


class _AccessPoliciesClient:
"""AccessPoliciesClient
Expand Down Expand Up @@ -66,6 +70,7 @@ def create(self, props, filters, access_permissions):
:class:`AccessPolicy` instance
"""
LOGGER.debug("Create Access Policy %s", props)
return self.create_from_data(
self.__query(props, filters=filters, access_permissions=access_permissions),
)
Expand Down
16 changes: 5 additions & 11 deletions archivist/archivist.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
"""

import logging

import json
from os.path import isfile as os_path_isfile
from typing import Optional

from flatten_dict import flatten
import requests
Expand All @@ -58,6 +59,8 @@
from .access_policies import _AccessPoliciesClient
from .subjects import _SubjectsClient

LOGGER = logging.getLogger(__name__)

CLIENTS = {
"assets": _AssetsClient,
"events": _EventsClient,
Expand Down Expand Up @@ -108,16 +111,6 @@ def __init__(self, url, *, auth=None, cert=None, verify=True):

self._cert = cert

self._assets = None
self._events = None
self._locations = None
self._attachments = None

self.assets: Optional[_AssetsClient]
self.events: Optional[_EventsClient]
self.locations: Optional[_LocationsClient]
self.attachments: Optional[_AttachmentsClient]

def __getattr__(self, value):
"""Create endpoints on demand"""
client = CLIENTS.get(value)
Expand Down Expand Up @@ -170,6 +163,7 @@ def get(self, subpath, identity, *, headers=None):
dict representing the response body (entity).
"""
LOGGER.debug("get %s/%s", subpath, identity)
response = requests.get(
SEP.join((self.url, ROOT, subpath, identity)),
headers=self.__add_headers(headers),
Expand Down
5 changes: 5 additions & 0 deletions archivist/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"""

import logging

from copy import deepcopy

from .constants import (
Expand All @@ -35,6 +37,8 @@
#: be changed.
DEFAULT_PAGE_SIZE = 500

LOGGER = logging.getLogger(__name__)


class _AssetsClient:
"""AssetsClient
Expand Down Expand Up @@ -64,6 +68,7 @@ def create(self, behaviours, attrs, *, confirm=False):
:class:`Asset` instance
"""
LOGGER.debug("Create Asset %s", attrs)
return self.create_from_data(
{
"behaviours": behaviours,
Expand Down
5 changes: 5 additions & 0 deletions archivist/attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@

# pylint:disable=too-few-public-methods

import logging

from .constants import ATTACHMENTS_SUBPATH, ATTACHMENTS_LABEL

LOGGER = logging.getLogger(__name__)


class _AttachmentsClient:
"""AttachmentsClient
Expand Down Expand Up @@ -55,6 +59,7 @@ def upload(self, fd, *, mtype="image/jpg"):
"""

LOGGER.debug("Upload Attachment")
return Attachment(
**self._archivist.post_file(
f"{ATTACHMENTS_SUBPATH}/{ATTACHMENTS_LABEL}",
Expand Down
5 changes: 4 additions & 1 deletion archivist/confirm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Wrap base methods with constants for assets (path, etc...
"""

import logging

from copy import deepcopy

import backoff
Expand All @@ -14,10 +16,11 @@
CONFIRMATION_STATUS,
)
from .errors import ArchivistUnconfirmedError
from .logger import LOGGER

MAX_TIME = 1200

LOGGER = logging.getLogger(__name__)


def __lookup_max_time():
return MAX_TIME
Expand Down
5 changes: 5 additions & 0 deletions archivist/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"""

import logging

from copy import deepcopy

from .constants import (
Expand All @@ -38,6 +40,8 @@
#: be changed.
DEFAULT_PAGE_SIZE = 500

LOGGER = logging.getLogger(__name__)


class _EventsClient:
"""EventsClient
Expand Down Expand Up @@ -70,6 +74,7 @@ def create(self, asset_id, props, attrs, *, asset_attrs=None, confirm=False):
"""

LOGGER.debug("Create Event %s/%s", asset_id, props)
return self.create_from_data(
asset_id,
self.__query(props, attrs, asset_attrs),
Expand Down
5 changes: 5 additions & 0 deletions archivist/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"""

import logging

from .constants import LOCATIONS_SUBPATH, LOCATIONS_LABEL


Expand All @@ -30,6 +32,8 @@
#: be changed.
DEFAULT_PAGE_SIZE = 500

LOGGER = logging.getLogger(__name__)


class _LocationsClient:
"""LocationsClient
Expand Down Expand Up @@ -58,6 +62,7 @@ def create(self, props, *, attrs=None):
:class:`Location` instance
"""
LOGGER.debug("Create Location %s", props)
return self.create_from_data(self.__query(props, attrs))

def create_from_data(self, data):
Expand Down
28 changes: 0 additions & 28 deletions archivist/logger.py

This file was deleted.

5 changes: 5 additions & 0 deletions archivist/subjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"""

import logging

from .constants import (
SUBJECTS_SUBPATH,
SUBJECTS_LABEL,
Expand All @@ -31,6 +33,8 @@
#: be changed.
DEFAULT_PAGE_SIZE = 500

LOGGER = logging.getLogger(__name__)


class _SubjectsClient:
"""SubjectsClient
Expand Down Expand Up @@ -60,6 +64,7 @@ def create(self, display_name, wallet_pub_keys, tessera_pub_keys):
:class:`Subject` instance
"""
LOGGER.debug("Create Subject %s", display_name)
return self.create_from_data(
self.__query(
display_name=display_name,
Expand Down
37 changes: 37 additions & 0 deletions docs/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,40 @@ REST api (in any language):
* code style managed and enforced.

See the **examples/** directory for example code.

Logging
=======

Follows the Django model as described here: https://docs.djangoproject.com/en/3.2/topics/logging/

The base logger for this package is rooted at "archivist" with subloggers for each endpoint:

- "archivist.archivist"
- "archivist.assets"
- ...

etc. etc.

Logging is configured by either defining a root logger with suitable handlers, formatters etc. or
by using dictionary configuration as described here: https://docs.python.org/3/library/logging.config.html#logging-config-dictschema

A recommended minimum configuration would be:

.. code-block:: python
import logging
logging.dictConfig({
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"console": {
"class": "logging.StreamHandler",
},
},
"root": {
"handlers": ["console"],
"level": "INFO",
},
})
2 changes: 0 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ fail_under = 100

[coverage:run]
omit =
# utility
archivist/logger.py
# so simple - not worth testing
archivist/timestamp.py
# omit functional tests
Expand Down

0 comments on commit 0395064

Please sign in to comment.