From e6db0bf5b81d209aa0a6a4951ac6c71ff1d65a20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Dierick?= Date: Mon, 23 Oct 2023 18:21:56 +0200 Subject: [PATCH 1/4] add env var that controls query logging --- Dockerfile | 1 + helpers.py | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index dc579dc..ae0cda9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,6 +14,7 @@ RUN chmod +x /start.sh # Template config ENV APP_ENTRYPOINT web ENV LOG_LEVEL info +ENV LOG_SPARQL_ALL True ENV MU_SPARQL_ENDPOINT 'http://database:8890/sparql' ENV MU_SPARQL_UPDATEPOINT 'http://database:8890/sparql' ENV MU_APPLICATION_GRAPH 'http://mu.semte.ch/application' diff --git a/helpers.py b/helpers.py index de685be..83dac3c 100644 --- a/helpers.py +++ b/helpers.py @@ -42,6 +42,8 @@ consoleHandler = logging.StreamHandler(stream=sys.stdout)# or stderr? logger.addHandler(consoleHandler) +LOG_SPARQL_ALL = os.environ.get('LOG_SPARQL_ALL') in ('TRUE', 'True', 'true') + def generate_uuid(): """Generates a random unique user id (UUID) based on the host ID and current time""" return str(uuid.uuid1()) @@ -119,7 +121,6 @@ def validate_resource_type(expected_type, data): def query(the_query): """Execute the given SPARQL query (select/ask/construct) on the triplestore and returns the results in the given return Format (JSON by default).""" - log("execute query: \n" + the_query) for header in MU_HEADERS: if header in request.headers: sparqlQuery.customHttpHeaders[header] = request.headers[header] @@ -127,6 +128,8 @@ def query(the_query): if header in sparqlQuery.customHttpHeaders: del sparqlQuery.customHttpHeaders[header] sparqlQuery.setQuery(the_query) + if LOG_SPARQL_ALL: + log("execute query: \n" + the_query) return sparqlQuery.query().convert() @@ -140,6 +143,8 @@ def update(the_query): del sparqlUpdate.customHttpHeaders[header] sparqlUpdate.setQuery(the_query) if sparqlUpdate.isSparqlUpdateRequest(): + if LOG_SPARQL_ALL: + log("execute query: \n" + the_query) sparqlUpdate.query() From e2be5bd34b58ce9e2a74d2f1bcd9e2ae6b10b90e Mon Sep 17 00:00:00 2001 From: Kobe Mertens Date: Tue, 1 Oct 2024 13:31:15 +0200 Subject: [PATCH 2/4] Add separate query/update logging and update README --- README.md | 8 ++++++++ helpers.py | 18 +++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 83b5327..5efc7b2 100644 --- a/README.md +++ b/README.md @@ -300,6 +300,7 @@ my-python: ``` ### Environment variables +#### General - `LOG_LEVEL` takes the same options as defined in the Python [logging](https://docs.python.org/3/library/logging.html#logging-levels) module. @@ -317,7 +318,14 @@ my-python: - `MU_SPARQL_TIMEOUT` is used to configure the timeout (in seconds) for SPARQL queries. +#### SPARQL Query Logging +- `LOG_SPARQL_ALL`: Log *all* executed queries, read as well as update (default `true`) +- `LOG_SPARQL_QUERIES`: Log *read* queries (default: `undefined`). Overrules `LOG_SPARQL_ALL` + +- `LOG_SPARQL_UPDATES`: Log *update* queries (default `undefined`). Overrules `LOG_SPARQL_ALL`. + +#### Meinheld Gunicorn Docker Variables Since this template is based on the meinheld-gunicorn-docker image, all possible environment config for that image is also available for the template. See [meinheld-gunicorn-docker#environment-variables](https://github.com/tiangolo/meinheld-gunicorn-docker#environment-variables) for more info. The template configures `WEB_CONCURRENCY` in particular to `1` by default. ### Production diff --git a/helpers.py b/helpers.py index 83dac3c..d6675e8 100644 --- a/helpers.py +++ b/helpers.py @@ -42,7 +42,15 @@ consoleHandler = logging.StreamHandler(stream=sys.stdout)# or stderr? logger.addHandler(consoleHandler) -LOG_SPARQL_ALL = os.environ.get('LOG_SPARQL_ALL') in ('TRUE', 'True', 'true') +LOG_SPARQL_ALL_VAR = os.environ.get('LOG_SPARQL_ALL') +LOG_SPARQL_QUERIES = os.environ.get( + 'LOG_SPARQL_QUERIES', + default=LOG_SPARQL_ALL_VAR +).lower() == 'true' +LOG_SPARQL_UPDATES = os.environ.get( + 'LOG_SPARQL_UPDATES', + default=LOG_SPARQL_ALL_VAR +).lower() == 'true' def generate_uuid(): """Generates a random unique user id (UUID) based on the host ID and current time""" @@ -128,8 +136,8 @@ def query(the_query): if header in sparqlQuery.customHttpHeaders: del sparqlQuery.customHttpHeaders[header] sparqlQuery.setQuery(the_query) - if LOG_SPARQL_ALL: - log("execute query: \n" + the_query) + if LOG_SPARQL_QUERIES: + log("Execute query: \n" + the_query) return sparqlQuery.query().convert() @@ -143,8 +151,8 @@ def update(the_query): del sparqlUpdate.customHttpHeaders[header] sparqlUpdate.setQuery(the_query) if sparqlUpdate.isSparqlUpdateRequest(): - if LOG_SPARQL_ALL: - log("execute query: \n" + the_query) + if LOG_SPARQL_UPDATES: + log("Execute query: \n" + the_query) sparqlUpdate.query() From e62bf593435bd167ead8b9b2f961d55d9786df78 Mon Sep 17 00:00:00 2001 From: Aad Versteden Date: Tue, 8 Oct 2024 18:53:01 +0200 Subject: [PATCH 3/4] Remove dangling whitespace --- helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers.py b/helpers.py index d6675e8..eec918e 100644 --- a/helpers.py +++ b/helpers.py @@ -42,7 +42,7 @@ consoleHandler = logging.StreamHandler(stream=sys.stdout)# or stderr? logger.addHandler(consoleHandler) -LOG_SPARQL_ALL_VAR = os.environ.get('LOG_SPARQL_ALL') +LOG_SPARQL_ALL_VAR = os.environ.get('LOG_SPARQL_ALL') LOG_SPARQL_QUERIES = os.environ.get( 'LOG_SPARQL_QUERIES', default=LOG_SPARQL_ALL_VAR From 982f2411b26fa10b36bfd7c56580d7f2b19f1f56 Mon Sep 17 00:00:00 2001 From: Aad Versteden Date: Wed, 9 Oct 2024 10:40:35 +0200 Subject: [PATCH 4/4] Explicit which values are considered true for logging Co-author: @erikap --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5efc7b2..45f2e6c 100644 --- a/README.md +++ b/README.md @@ -319,12 +319,14 @@ my-python: - `MU_SPARQL_TIMEOUT` is used to configure the timeout (in seconds) for SPARQL queries. #### SPARQL Query Logging -- `LOG_SPARQL_ALL`: Log *all* executed queries, read as well as update (default `true`) +- `LOG_SPARQL_ALL`: Log *all* executed queries, read as well as update (default `True`) - `LOG_SPARQL_QUERIES`: Log *read* queries (default: `undefined`). Overrules `LOG_SPARQL_ALL` - `LOG_SPARQL_UPDATES`: Log *update* queries (default `undefined`). Overrules `LOG_SPARQL_ALL`. +The string "true", ignoring casing, is considered `True`. All other values are considered `False`. + #### Meinheld Gunicorn Docker Variables Since this template is based on the meinheld-gunicorn-docker image, all possible environment config for that image is also available for the template. See [meinheld-gunicorn-docker#environment-variables](https://github.com/tiangolo/meinheld-gunicorn-docker#environment-variables) for more info. The template configures `WEB_CONCURRENCY` in particular to `1` by default.