Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable logging support in python sdk #349

Closed
wants to merge 13 commits into from
5 changes: 3 additions & 2 deletions openapiart/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def send_recv(self, method, relative_url, payload=None, return_object=None, head
# content types
return response
else:
self.logger.debug('Status code: {} ; reason: {} ; data: {}'.format(response.status_code, response.reason, response.text))
raise Exception(response.status_code, yaml.safe_load(response.text))


Expand All @@ -155,10 +156,10 @@ class OpenApiBase(object):
YAML = "yaml"
DICT = "dict"

__slots__ = ()
__slots__ = ("logger")

def __init__(self):
pass
self.logger = logging.getLogger(self.__module__)

def serialize(self, encoding=JSON):
"""Serialize the current object according to a specified encoding.
Expand Down
21 changes: 20 additions & 1 deletion openapiart/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,26 @@ def _write_api_class(self, methods, factories):
self._write(1, '"""')
self._write()
self._write(1, "def __init__(self, **kwargs):")
self._write(2, "pass")
self._write(
2,
'self.logger = kwargs["logger"] if "logger" in kwargs else None',
)
self._write(
2,
'self.loglevel = kwargs["loglevel"] if "loglevel" in kwargs else logging.INFO',
)
self._write(2, "if self.logger is None:")
self._write(3, "self.logger = logging.getLogger(self.__module__)")
self._write(3, "self.logger.setLevel(self.loglevel)")
self._write(
3,
'formatter = logging.Formatter(fmt="%(asctime)s [%(name)s] [%(levelname)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S")',
)
self._write(3, "sh = logging.StreamHandler(sys.stdout)")
self._write(3, "sh.setFormatter(formatter)")
self._write(3, "if len(self.logger.handlers) > 0:")
self._write(4, "del self.logger.handlers[:]")
self._write(3, "self.logger.addHandler(sh)")

for method in methods:
print("generating method %s" % method["name"])
Expand Down
12 changes: 12 additions & 0 deletions openapiart/tests/test_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest
import sys
import os

sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", "art"))
import sanity


def test_log():
sanity_api = sanity.api()
sanity_api.logger.info("Testing logger")
sanity_api.logger.debug("Testing logger")