Skip to content

Commit 944d9de

Browse files
feat(logger): possibility to override debug mode's default logging mechanism
1 parent 65c68db commit 944d9de

File tree

6 files changed

+115
-10
lines changed

6 files changed

+115
-10
lines changed

AUTHORS.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
> ⚠ This document _only_ reflects the responsible developers at HEXONET GmbH. For a list of MUCH-APPRECIATED CONTRIBUTORS -- people who helped developing and extending this library, applying patches, adding helpful comments
22
and thus generally made it that much better, see [GitHub's list of contributors](https://github.com/hexonet/python-sdk/contributors).
33

4-
* [Anthony Schneider](//github.com/anthonyschn) - Original Implementation
5-
* [Kai Schwarz](//github.com/papakai) - Full Review
4+
* [Kai Schwarz](//github.com/papakai) - Development Lead

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ This module is a connector library for the insanely fast HEXONET Backend API. Fo
2323
* Automatic IDN Domain name conversion to punycode (our API accepts only punycode format in commands)
2424
* Allows nested associative arrays in API commands to improve for bulk parameters
2525
* Connecting and communication with our API
26+
* Possibility to use a custom mechanism for debug mode
2627
* Several ways to access and deal with response data
2728
* Getting the command again returned together with the response
2829
* Sessionless communication
@@ -91,6 +92,29 @@ e.g. `$cl->setURL("http://127.0.0.1:8765/api/call.cgi");` would change the port.
9192

9293
Don't use `https` for that setup as it leads to slowing things down as of the https `overhead` of securing the connection. In this setup we just connect to localhost, so no direct outgoing network traffic using `http`. The apache configuration finally takes care passing it to `https` for the final communication to the HEXONET API.
9394

95+
### Customize Logging / Outputs
96+
97+
When having the debug mode activated hexonet.apiconnector.logger will be used for doing outputs by default.
98+
Of course it could be of interest for integrators to look for a way of getting this replaced by a custom mechanism like forwarding things to a 3rd-party software, logging into file or whatever.
99+
100+
```python
101+
from hexonet.apiconnector.apiclient import APIClient as AC
102+
# import your module from your custom namespace of course
103+
from hexonet.apiconnector.customlogger import CustomLogger as CL
104+
105+
cl = AC()
106+
# LIVE System would be used otherwise by default
107+
cl.useOTESystem()
108+
# enable debug output (inactive by default)
109+
cl.enableDebugMode()
110+
# provide your custom logger mechanism
111+
cl.setCustomLogger(CL())
112+
cl.setCredentials("test.user", "test.passw0rd")
113+
r = cl.request({"COMMAND" => "StatusAccount"})
114+
```
115+
116+
NOTE: Find an example for a custom logger class implementation in `hexonet/apiconnector/customlogger.py`. If you have questions, feel free to open a github issue.
117+
94118
## How to use this module in your project
95119

96120
All you need to know, can be found [here](https://hexonet-python-sdk.readthedocs.io/en/latest/#usage-guide).

hexonet/apiconnector/apiclient.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
:license: MIT, see LICENSE for more details.
99
"""
1010

11+
from hexonet.apiconnector.logger import Logger
1112
from hexonet.apiconnector.response import Response
1213
from hexonet.apiconnector.responsetemplatemanager import ResponseTemplateManager as RTM
1314
from hexonet.apiconnector.socketconfig import SocketConfig
@@ -39,6 +40,22 @@ def __init__(self):
3940
self.__ua = ""
4041
# additional connection settings
4142
self.__curlopts = {}
43+
# logger class instance
44+
self.setDefaultLogger()
45+
46+
def setCustomLogger(self, logger):
47+
"""
48+
Set custom logger to use instead of the default one
49+
"""
50+
self.__logger = logger
51+
return self
52+
53+
def setDefaultLogger(self):
54+
"""
55+
Set default logger to use
56+
"""
57+
self.__logger = Logger()
58+
return self
4259

4360
def setProxy(self, proxy):
4461
"""
@@ -270,6 +287,7 @@ def request(self, cmd):
270287
}
271288
data = self.getPOSTData(newcmd).encode('UTF-8')
272289
secured = self.getPOSTData(newcmd, True).encode('UTF-8')
290+
error = None
273291
try:
274292
headers = {
275293
'User-Agent': self.getUserAgent()
@@ -281,13 +299,13 @@ def request(self, cmd):
281299
proxyurl = urlparse(self.__curlopts["PROXY"])
282300
req.set_proxy(proxyurl.netloc, proxyurl.scheme)
283301
body = urlopen(req, timeout=self.__socketTimeout).read()
284-
if (self.__debugMode):
285-
print((self.__socketURL, secured, body, '\n', '\n'))
286-
except Exception:
302+
except Exception as e:
303+
error = str(e)
287304
body = rtm.getTemplate("httperror").getPlain()
288-
if (self.__debugMode):
289-
print((self.__socketURL, secured, "HTTP communication failed", body, '\n', '\n'))
290-
return Response(body, newcmd, cfg)
305+
r = Response(body, newcmd, cfg)
306+
if (self.__debugMode):
307+
self.__logger.log(secured, r, error)
308+
return r
291309

292310
def requestNextResponsePage(self, rr):
293311
"""
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
hexonet.apiconnector.customlogger
4+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5+
This module provides all necessary functionality for
6+
debug outputs
7+
:copyright: © 2020 by HEXONET GmbH.
8+
:license: MIT, see LICENSE for more details.
9+
"""
10+
11+
from hexonet.apiconnector.logger import Logger
12+
import sys
13+
14+
15+
class CustomLogger(Logger, object):
16+
"""
17+
The Logger class covers all you need to cover debug outputs of the API communication.
18+
"""
19+
20+
def __init__(self):
21+
"""
22+
constructor calling parent constructor
23+
"""
24+
super(CustomLogger, self).__init__()
25+
26+
def log(self, post, r, error):
27+
"""
28+
output/log given data
29+
"""
30+
#
31+
# implement your own logic here
32+
#
33+
# print(r.getCommandPlain())
34+
# print(post)
35+
# if error:
36+
# print("HTTP communication failed: %s" % (error), sys.stderr)
37+
# print(r.getPlain())

hexonet/apiconnector/logger.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
hexonet.apiconnector.logger
4+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5+
This module provides all necessary functionality for
6+
debug outputs see the customlogger class on how to override this
7+
:copyright: © 2018 by HEXONET GmbH.
8+
:license: MIT, see LICENSE for more details.
9+
"""
10+
11+
import sys
12+
13+
14+
class Logger(object):
15+
"""
16+
The Logger class covers all you need to cover debug outputs of the API communication.
17+
"""
18+
19+
def log(self, post, r, error):
20+
"""
21+
output/log given data
22+
"""
23+
print(r.getCommandPlain())
24+
print(post)
25+
if error:
26+
print("HTTP communication failed: %s" % (error), sys.stderr)
27+
print(r.getPlain())

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ def find_version(*file_paths):
3535
description=PACKAGE + ' is a connector library for the insanely fast HEXONET Backend API',
3636
long_description=long_description,
3737
long_description_content_type="text/markdown",
38-
author='Anthony Schneider',
39-
author_email='anthonys@hexonet.net',
38+
author='Kai Schwarz',
39+
author_email='kschwarz@hexonet.net',
4040
maintainer='Kai Schwarz',
4141
maintainer_email='[email protected]',
4242
url='https://github.com/hexonet/python-sdk/',

0 commit comments

Comments
 (0)