Skip to content

Commit 0395064

Browse files
authored
Use django-style logging support (#28)
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>
1 parent 263354f commit 0395064

12 files changed

Lines changed: 111 additions & 42 deletions

File tree

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,41 @@ else:
6666

6767
```
6868

69+
## Logging
70+
71+
Follows the Django model as described here: https://docs.djangoproject.com/en/3.2/topics/logging/
72+
73+
The base logger for this pacakage is rooted at "archivist" with subloggers for each endpoint:
74+
75+
- "archivist.archivist"
76+
- "archivist.assets"
77+
- ...
78+
79+
etc. etc.
80+
81+
Logging is configured by either defining a root logger with suitable handlers, formatters etc. or
82+
by using dictionary configuration as described here: https://docs.python.org/3/library/logging.config.html#logging-config-dictschema
83+
84+
A recommended minimum configuration would be:
85+
86+
```python
87+
import logging
88+
89+
logging.dictConfig({
90+
"version": 1,
91+
"disable_existing_loggers": False,
92+
"handlers": {
93+
"console": {
94+
"class": "logging.StreamHandler",
95+
},
96+
},
97+
"root": {
98+
"handlers": ["console"],
99+
"level": "INFO",
100+
},
101+
})
102+
```
103+
69104
# Development
70105

71106
## Pre-requisites

archivist/access_policies.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
2222
"""
2323

24+
import logging
25+
2426
from copy import deepcopy
2527

2628
from .constants import (
@@ -37,6 +39,8 @@
3739
#: be changed.
3840
DEFAULT_PAGE_SIZE = 500
3941

42+
LOGGER = logging.getLogger(__name__)
43+
4044

4145
class _AccessPoliciesClient:
4246
"""AccessPoliciesClient
@@ -66,6 +70,7 @@ def create(self, props, filters, access_permissions):
6670
:class:`AccessPolicy` instance
6771
6872
"""
73+
LOGGER.debug("Create Access Policy %s", props)
6974
return self.create_from_data(
7075
self.__query(props, filters=filters, access_permissions=access_permissions),
7176
)

archivist/archivist.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929
3030
"""
3131

32+
import logging
33+
3234
import json
3335
from os.path import isfile as os_path_isfile
34-
from typing import Optional
3536

3637
from flatten_dict import flatten
3738
import requests
@@ -58,6 +59,8 @@
5859
from .access_policies import _AccessPoliciesClient
5960
from .subjects import _SubjectsClient
6061

62+
LOGGER = logging.getLogger(__name__)
63+
6164
CLIENTS = {
6265
"assets": _AssetsClient,
6366
"events": _EventsClient,
@@ -108,16 +111,6 @@ def __init__(self, url, *, auth=None, cert=None, verify=True):
108111

109112
self._cert = cert
110113

111-
self._assets = None
112-
self._events = None
113-
self._locations = None
114-
self._attachments = None
115-
116-
self.assets: Optional[_AssetsClient]
117-
self.events: Optional[_EventsClient]
118-
self.locations: Optional[_LocationsClient]
119-
self.attachments: Optional[_AttachmentsClient]
120-
121114
def __getattr__(self, value):
122115
"""Create endpoints on demand"""
123116
client = CLIENTS.get(value)
@@ -170,6 +163,7 @@ def get(self, subpath, identity, *, headers=None):
170163
dict representing the response body (entity).
171164
172165
"""
166+
LOGGER.debug("get %s/%s", subpath, identity)
173167
response = requests.get(
174168
SEP.join((self.url, ROOT, subpath, identity)),
175169
headers=self.__add_headers(headers),

archivist/assets.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
2222
"""
2323

24+
import logging
25+
2426
from copy import deepcopy
2527

2628
from .constants import (
@@ -35,6 +37,8 @@
3537
#: be changed.
3638
DEFAULT_PAGE_SIZE = 500
3739

40+
LOGGER = logging.getLogger(__name__)
41+
3842

3943
class _AssetsClient:
4044
"""AssetsClient
@@ -64,6 +68,7 @@ def create(self, behaviours, attrs, *, confirm=False):
6468
:class:`Asset` instance
6569
6670
"""
71+
LOGGER.debug("Create Asset %s", attrs)
6772
return self.create_from_data(
6873
{
6974
"behaviours": behaviours,

archivist/attachments.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424

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

27+
import logging
28+
2729
from .constants import ATTACHMENTS_SUBPATH, ATTACHMENTS_LABEL
2830

31+
LOGGER = logging.getLogger(__name__)
32+
2933

3034
class _AttachmentsClient:
3135
"""AttachmentsClient
@@ -55,6 +59,7 @@ def upload(self, fd, *, mtype="image/jpg"):
5559
5660
"""
5761

62+
LOGGER.debug("Upload Attachment")
5863
return Attachment(
5964
**self._archivist.post_file(
6065
f"{ATTACHMENTS_SUBPATH}/{ATTACHMENTS_LABEL}",

archivist/confirm.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Wrap base methods with constants for assets (path, etc...
44
"""
55

6+
import logging
7+
68
from copy import deepcopy
79

810
import backoff
@@ -14,10 +16,11 @@
1416
CONFIRMATION_STATUS,
1517
)
1618
from .errors import ArchivistUnconfirmedError
17-
from .logger import LOGGER
1819

1920
MAX_TIME = 1200
2021

22+
LOGGER = logging.getLogger(__name__)
23+
2124

2225
def __lookup_max_time():
2326
return MAX_TIME

archivist/events.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
2323
"""
2424

25+
import logging
26+
2527
from copy import deepcopy
2628

2729
from .constants import (
@@ -38,6 +40,8 @@
3840
#: be changed.
3941
DEFAULT_PAGE_SIZE = 500
4042

43+
LOGGER = logging.getLogger(__name__)
44+
4145

4246
class _EventsClient:
4347
"""EventsClient
@@ -70,6 +74,7 @@ def create(self, asset_id, props, attrs, *, asset_attrs=None, confirm=False):
7074
7175
"""
7276

77+
LOGGER.debug("Create Event %s/%s", asset_id, props)
7378
return self.create_from_data(
7479
asset_id,
7580
self.__query(props, attrs, asset_attrs),

archivist/locations.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
2323
"""
2424

25+
import logging
26+
2527
from .constants import LOCATIONS_SUBPATH, LOCATIONS_LABEL
2628

2729

@@ -30,6 +32,8 @@
3032
#: be changed.
3133
DEFAULT_PAGE_SIZE = 500
3234

35+
LOGGER = logging.getLogger(__name__)
36+
3337

3438
class _LocationsClient:
3539
"""LocationsClient
@@ -58,6 +62,7 @@ def create(self, props, *, attrs=None):
5862
:class:`Location` instance
5963
6064
"""
65+
LOGGER.debug("Create Location %s", props)
6166
return self.create_from_data(self.__query(props, attrs))
6267

6368
def create_from_data(self, data):

archivist/logger.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

archivist/subjects.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
2222
"""
2323

24+
import logging
25+
2426
from .constants import (
2527
SUBJECTS_SUBPATH,
2628
SUBJECTS_LABEL,
@@ -31,6 +33,8 @@
3133
#: be changed.
3234
DEFAULT_PAGE_SIZE = 500
3335

36+
LOGGER = logging.getLogger(__name__)
37+
3438

3539
class _SubjectsClient:
3640
"""SubjectsClient
@@ -60,6 +64,7 @@ def create(self, display_name, wallet_pub_keys, tessera_pub_keys):
6064
:class:`Subject` instance
6165
6266
"""
67+
LOGGER.debug("Create Subject %s", display_name)
6368
return self.create_from_data(
6469
self.__query(
6570
display_name=display_name,

0 commit comments

Comments
 (0)