Skip to content

Commit b43188f

Browse files
committed
add structlog
1 parent fff95af commit b43188f

File tree

4 files changed

+93
-29
lines changed

4 files changed

+93
-29
lines changed

Pipfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ requests-mock = "*"
1111
requests = "*"
1212
structlog = "*"
1313
attr = "*"
14+
click = "*"
1415

1516
[requires]
1617
python_version = "3.7"
18+
19+
[scripts]
20+
dsaps = "python -c \"from dsaps.cli import main; main()\""

Pipfile.lock

Lines changed: 59 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dsaps/cli.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import datetime
2+
import logging
13
import time
24

35
import click
6+
import structlog
47

58
from dsaps import models
69

10+
logger = structlog.get_logger()
11+
712

813
@click.group()
914
@click.option('--url', envvar='DSPACE_URL')
@@ -15,7 +20,22 @@
1520
@click.pass_context
1621
def main(ctx, url, email, password):
1722
ctx.obj = {}
18-
print('Application start')
23+
dt = datetime.datetime.utcnow().isoformat(timespec='seconds')
24+
log_suffix = f'{dt}.log'
25+
structlog.configure(processors=[
26+
structlog.stdlib.filter_by_level,
27+
structlog.stdlib.add_log_level,
28+
structlog.stdlib.PositionalArgumentsFormatter(),
29+
structlog.processors.TimeStamper(fmt="iso"),
30+
structlog.processors.JSONRenderer()
31+
],
32+
context_class=dict,
33+
logger_factory=structlog.stdlib.LoggerFactory())
34+
logging.basicConfig(format="%(message)s",
35+
handlers=[logging.FileHandler(f'logs/log-{log_suffix}',
36+
'w')],
37+
level=logging.INFO)
38+
logger.info('Application start')
1939
client = models.Client(url, email, password)
2040
start_time = time.time()
2141
ctx.obj['client'] = client
@@ -38,7 +58,7 @@ def search(ctx, field, string, search_type):
3858
client = ctx.obj['client']
3959
start_time = ctx.obj['start_time']
4060
item_links = client.filtered_item_search(field, string, search_type)
41-
print(item_links)
61+
logger.info(item_links)
4262
models.elapsed_time(start_time, 'Elapsed time')
4363

4464

dsaps/models.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
import time
66

77
import attr
8+
import structlog
89

910
op = operator.attrgetter('name')
1011
Field = partial(attr.ib, default=None)
1112

13+
logger = structlog.get_logger()
14+
1215

1316
class Client:
1417
def __init__(self, url, email, password):
1518
self.url = url
16-
print('Initializing client')
19+
logger.info('Initializing client')
1720
data = {'email': email, 'password': password}
1821
header = {'content-type': 'application/json', 'accept':
1922
'application/json'}
@@ -25,7 +28,7 @@ def __init__(self, url, email, password):
2528
self.user_full_name = status['fullname']
2629
self.cookies = cookies
2730
self.header = header
28-
print(f'Authenticated to {self.url} as 'f'{self.user_full_name}')
31+
logger.info(f'Authenticated to {self.url} as 'f'{self.user_full_name}')
2932

3033
def get_record(self, uuid, rec_type):
3134
"""Retrieve an individual record of a particular type."""
@@ -39,7 +42,7 @@ def get_record(self, uuid, rec_type):
3942
elif rec_type == 'collections':
4043
rec_obj = self._pop_inst(Collection, record)
4144
else:
42-
print('Invalid record type.')
45+
logger.info('Invalid record type.')
4346
exit()
4447
return rec_obj
4548

@@ -52,7 +55,7 @@ def filtered_item_search(self, key, string, query_type,
5255
endpoint = f'{self.url}/rest/filtered-items?query_field[]='
5356
endpoint += f'{key}&query_op[]={query_type}&query_val[]={string}'
5457
endpoint += f'{selected_collections}&limit=200&offset={offset}'
55-
print(endpoint)
58+
logger.info(endpoint)
5659
response = requests.get(endpoint, headers=self.header,
5760
cookies=self.cookies).json()
5861
items = response['items']
@@ -117,4 +120,4 @@ class MetadataEntry(BaseRecord):
117120
def elapsed_time(start_time, label):
118121
"""Calculate elapsed time."""
119122
td = datetime.timedelta(seconds=time.time() - start_time)
120-
print(f'{label} : {td}')
123+
logger.info(f'{label} : {td}')

0 commit comments

Comments
 (0)