Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ requests-mock = "*"
requests = "*"
structlog = "*"
attr = "*"
click = "*"

[requires]
python_version = "3.7"

[scripts]
dsaps = "python -c \"from dsaps.cli import main; main()\""
81 changes: 59 additions & 22 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 22 additions & 2 deletions dsaps/cli.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import datetime
import logging
import time

import click
import structlog

from dsaps import models

logger = structlog.get_logger()


@click.group()
@click.option('--url', envvar='DSPACE_URL')
Expand All @@ -15,7 +20,22 @@
@click.pass_context
def main(ctx, url, email, password):
ctx.obj = {}
print('Application start')
dt = datetime.datetime.utcnow().isoformat(timespec='seconds')
log_suffix = f'{dt}.log'
structlog.configure(processors=[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to remove this entire block and it logged fine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you getting 2019-11-26 13:39.16 Application start or {"event": "Application start", "logger": "__main__", "level": "info", "timestamp": "2019-11-26T18:40:30.819933Z"}? I just commented it out again and I'm still having the same problem where it gives me unstructured log entries in the terminal but nothing to the file

structlog.stdlib.filter_by_level,
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.JSONRenderer()
],
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory())
logging.basicConfig(format="%(message)s",
handlers=[logging.FileHandler(f'logs/log-{log_suffix}',
'w')],
level=logging.INFO)
logger.info('Application start')
client = models.Client(url, email, password)
start_time = time.time()
ctx.obj['client'] = client
Expand All @@ -38,7 +58,7 @@ def search(ctx, field, string, search_type):
client = ctx.obj['client']
start_time = ctx.obj['start_time']
item_links = client.filtered_item_search(field, string, search_type)
print(item_links)
logger.info(item_links)
models.elapsed_time(start_time, 'Elapsed time')


Expand Down
13 changes: 8 additions & 5 deletions dsaps/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
import time

import attr
import structlog

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

logger = structlog.get_logger()


class Client:
def __init__(self, url, email, password):
self.url = url
print('Initializing client')
logger.info('Initializing client')
data = {'email': email, 'password': password}
header = {'content-type': 'application/json', 'accept':
'application/json'}
Expand All @@ -25,7 +28,7 @@ def __init__(self, url, email, password):
self.user_full_name = status['fullname']
self.cookies = cookies
self.header = header
print(f'Authenticated to {self.url} as 'f'{self.user_full_name}')
logger.info(f'Authenticated to {self.url} as 'f'{self.user_full_name}')

def get_record(self, uuid, rec_type):
"""Retrieve an individual record of a particular type."""
Expand All @@ -39,7 +42,7 @@ def get_record(self, uuid, rec_type):
elif rec_type == 'collections':
rec_obj = self._pop_inst(Collection, record)
else:
print('Invalid record type.')
logger.info('Invalid record type.')
exit()
return rec_obj

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