Skip to content

Commit f4ed72e

Browse files
Change print to use logging (#73)
* Monkey patched print to use logging module * Bumped version
1 parent e59776c commit f4ed72e

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

polyapi/__init__.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import sys
33
import copy
44
import truststore
5+
import logging
6+
import builtins
57
from typing import Any, Dict, Optional, overload, Literal
68
from typing_extensions import TypedDict
79
truststore.inject_into_ssl()
@@ -98,4 +100,19 @@ def copy(self) -> '_PolyCustom':
98100
return new
99101

100102

101-
polyCustom: PolyCustomDict = _PolyCustom()
103+
polyCustom: PolyCustomDict = _PolyCustom()
104+
105+
original_print = print
106+
107+
logging.basicConfig(level=logging.INFO, format='%(levelname)s - %(message)s')
108+
109+
def log_prints(*objects, sep=' ', end='\n', file=sys.stdout, flush=False):
110+
message = sep.join(map(str, objects)) + end
111+
if file is sys.stdout:
112+
logging.info(message)
113+
elif file is sys.stderr:
114+
logging.error(message)
115+
else:
116+
original_print(*objects, sep=sep, end=end, file=file, flush=flush)
117+
118+
builtins.print = log_prints

polyapi/execute.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from typing import Dict, Optional
22
import requests
33
import os
4-
import logging
4+
import sys
55
from requests import Response
66
from polyapi.config import get_api_key_and_url, get_mtls_config
77
from polyapi.exceptions import PolyApiException
88

9-
logger = logging.getLogger("poly")
109

1110
def direct_execute(function_type, function_id, data) -> Response:
1211
""" execute a specific function id/type
@@ -49,7 +48,7 @@ def direct_execute(function_type, function_id, data) -> Response:
4948
if (resp.status_code < 200 or resp.status_code >= 300):
5049
error_content = resp.content.decode("utf-8", errors="ignore")
5150
if function_type == 'api' and os.getenv("LOGS_ENABLED"):
52-
logger.error(f"Error executing api function with id: {function_id}. Status code: {resp.status_code}. Request data: {data}, Response: {error_content}")
51+
print(f"Error executing api function with id: {function_id}. Status code: {resp.status_code}. Request data: {data}, Response: {error_content}", file=sys.stderr)
5352
elif function_type != 'api':
5453
raise PolyApiException(f"{resp.status_code}: {error_content}")
5554

@@ -73,7 +72,7 @@ def execute(function_type, function_id, data) -> Response:
7372
if (resp.status_code < 200 or resp.status_code >= 300) and os.getenv("LOGS_ENABLED"):
7473
error_content = resp.content.decode("utf-8", errors="ignore")
7574
if function_type == 'api' and os.getenv("LOGS_ENABLED"):
76-
logger.error(f"Error executing api function with id: {function_id}. Status code: {resp.status_code}. Request data: {data}, Response: {error_content}")
75+
print(f"Error executing api function with id: {function_id}. Status code: {resp.status_code}. Request data: {data}, Response: {error_content}", file=sys.stderr)
7776
elif function_type != 'api':
7877
raise PolyApiException(f"{resp.status_code}: {error_content}")
7978

polyapi/schema.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import logging
44
import contextlib
55
import re
6+
import polyapi
7+
import builtins
68
from typing import Dict
79
from jsonschema_gentypes.cli import process_config
810
from jsonschema_gentypes import configuration
@@ -89,9 +91,12 @@ def generate_schema_types(input_data: Dict, root=None):
8991
}
9092

9193
# jsonschema_gentypes prints source to stdout
92-
# no option to surpress so we do this
94+
# no option to suppress so we do this
95+
# Not reverting the print monkeypatch causes print to bypass redirect
96+
builtins.print = polyapi.original_print
9397
with contextlib.redirect_stdout(None):
9498
process_config(config, [tmp_input])
99+
builtins.print = polyapi.log_prints
95100

96101
with open(tmp_output, encoding='utf-8') as f:
97102
output = f.read()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ requires = ["setuptools>=61.2", "wheel"]
33

44
[project]
55
name = "polyapi-python"
6-
version = "0.3.9.dev6"
6+
version = "0.3.9.dev7"
77
description = "The Python Client for PolyAPI, the IPaaS by Developers for Developers"
88
authors = [{ name = "Dan Fellin", email = "dan@polyapi.io" }]
99
dependencies = [

0 commit comments

Comments
 (0)