From b43188fedaeba5119d9bd38ac2cfa337214fe2c6 Mon Sep 17 00:00:00 2001 From: Eric Hanson Date: Fri, 15 Nov 2019 13:23:42 -0500 Subject: [PATCH] add structlog --- Pipfile | 4 +++ Pipfile.lock | 81 +++++++++++++++++++++++++++++++++++-------------- dsaps/cli.py | 24 +++++++++++++-- dsaps/models.py | 13 +++++--- 4 files changed, 93 insertions(+), 29 deletions(-) diff --git a/Pipfile b/Pipfile index 57a2a7c..d94a2cb 100644 --- a/Pipfile +++ b/Pipfile @@ -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()\"" diff --git a/Pipfile.lock b/Pipfile.lock index 64fb70d..ed5ef2a 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "f0f4eb8bc3615617b266bbd7623ed39a1954441f310c8d9f28b4e3c25a8509fc" + "sha256": "b3b90945556e626398a7ce5d09116ed13635364ca3ddc5e01a9e5a6aced5f337" }, "pipfile-spec": 6, "requires": { @@ -38,6 +38,14 @@ ], "version": "==3.0.4" }, + "click": { + "hashes": [ + "sha256:2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13", + "sha256:5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7" + ], + "index": "pypi", + "version": "==7.0" + }, "idna": { "hashes": [ "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407", @@ -53,14 +61,6 @@ "index": "pypi", "version": "==2.22.0" }, - "requests-mock": { - "hashes": [ - "sha256:510df890afe08d36eca5bb16b4aa6308a6f85e3159ad3013bac8b9de7bd5a010", - "sha256:88d3402dd8b3c69a9e4f9d3a73ad11b15920c6efd36bc27bf1f701cf4a8e4646" - ], - "index": "pypi", - "version": "==1.7.0" - }, "six": { "hashes": [ "sha256:1f1b7d42e254082a9db6279deae68afb421ceba6158efa6131de7b3003ee93fd", @@ -85,13 +85,6 @@ } }, "develop": { - "atomicwrites": { - "hashes": [ - "sha256:03472c30eb2c5d1ba9227e4c2ca66ab8287fbfbbda3888aa93dc2e28fc6811b4", - "sha256:75a9445bac02d8d058d5e1fe689654ba5a6556a1dfd8ce6ec55a0ed79866cfa6" - ], - "version": "==1.3.0" - }, "attrs": { "hashes": [ "sha256:08a96c641c3a74e44eb59afb61a24f2cb9f4d7188748e76ba4bb5edfa3cb7d1c", @@ -99,6 +92,27 @@ ], "version": "==19.3.0" }, + "certifi": { + "hashes": [ + "sha256:e4f3620cfea4f83eedc95b24abd9cd56f3c4b146dd0177e83a21b4eb49e21e50", + "sha256:fd7c7c74727ddcf00e9acd26bba8da604ffec95bf1c2144e67aff7a8b50e6cef" + ], + "version": "==2019.9.11" + }, + "chardet": { + "hashes": [ + "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae", + "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691" + ], + "version": "==3.0.4" + }, + "idna": { + "hashes": [ + "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407", + "sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c" + ], + "version": "==2.8" + }, "importlib-metadata": { "hashes": [ "sha256:aa18d7378b00b40847790e7c27e11673d7fed219354109d0e7b9e5b25dc3ad26", @@ -123,10 +137,10 @@ }, "pluggy": { "hashes": [ - "sha256:0db4b7601aae1d35b4a033282da476845aa19185c1e6964b25cf324b5e4ec3e6", - "sha256:fa5fa1622fa6dd5c030e9cad086fa19ef6a0cf6d7a2d12318e10cb49d6d68f34" + "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0", + "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d" ], - "version": "==0.13.0" + "version": "==0.13.1" }, "py": { "hashes": [ @@ -144,11 +158,27 @@ }, "pytest": { "hashes": [ - "sha256:27abc3fef618a01bebb1f0d6d303d2816a99aa87a5968ebc32fe971be91eb1e6", - "sha256:58cee9e09242937e136dbb3dab466116ba20d6b7828c7620f23947f37eb4dae4" + "sha256:63344a2e3bce2e4d522fd62b4fdebb647c019f1f9e4ca075debbd13219db4418", + "sha256:f67403f33b2b1d25a6756184077394167fe5e2f9d8bdaab30707d19ccec35427" ], "index": "pypi", - "version": "==5.2.2" + "version": "==5.3.1" + }, + "requests": { + "hashes": [ + "sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4", + "sha256:9cf5292fcd0f598c671cfc1e0d7d1a7f13bb8085e9a590f48c010551dc6c4b31" + ], + "index": "pypi", + "version": "==2.22.0" + }, + "requests-mock": { + "hashes": [ + "sha256:510df890afe08d36eca5bb16b4aa6308a6f85e3159ad3013bac8b9de7bd5a010", + "sha256:88d3402dd8b3c69a9e4f9d3a73ad11b15920c6efd36bc27bf1f701cf4a8e4646" + ], + "index": "pypi", + "version": "==1.7.0" }, "six": { "hashes": [ @@ -157,6 +187,13 @@ ], "version": "==1.13.0" }, + "urllib3": { + "hashes": [ + "sha256:a8a318824cc77d1fd4b2bec2ded92646630d7fe8619497b142c84a9e6f5a7293", + "sha256:f3c5fd51747d450d4dcf6f923c81f78f811aab8205fda64b0aba34a4e48b0745" + ], + "version": "==1.25.7" + }, "wcwidth": { "hashes": [ "sha256:3df37372226d6e63e1b1e1eda15c594bca98a22d33a23832a90998faa96bc65e", diff --git a/dsaps/cli.py b/dsaps/cli.py index e831623..af68ffc 100644 --- a/dsaps/cli.py +++ b/dsaps/cli.py @@ -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') @@ -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=[ + 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 @@ -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') diff --git a/dsaps/models.py b/dsaps/models.py index 883af0b..66a906b 100644 --- a/dsaps/models.py +++ b/dsaps/models.py @@ -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'} @@ -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.""" @@ -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 @@ -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'] @@ -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}')