Skip to content

Commit b878191

Browse files
feat(logger): possibility to override debug mode's default logging mechanism. See README.md
1 parent fbe6065 commit b878191

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1590
-160
lines changed

README.md

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

163164
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.
164165

166+
### Customize Logging / Outputs
167+
168+
When having the debug mode activated \HEXONET\Logger will be used for doing outputs.
169+
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.
170+
171+
```java
172+
import net.hexonet.apiconnector.APIClient;
173+
import net.hexonet.apiconnector.Response;
174+
import java.util.HashMap;
175+
import java.util.Map;
176+
177+
public static void main(String[] args) {
178+
APIClient cl = new APIClient();
179+
cl.useOTESystem()
180+
.enableDebugMode() // activate debug outputs
181+
.setCustomLogger(new MyCustomerLogger()) // provide your mechanism here
182+
.setCredentials("test.user", "test.passw0rd");
183+
Map<String, Object> cmd = new HashMap<String, String>();
184+
cmd.put("COMMAND", "StatusAccount");
185+
Response r = cl.request(cmd);
186+
}
187+
```
188+
189+
NOTE: Find an example for a custom logger class implementation in `src/main/java/net/hexonet/apiconnector/CustomLogger.java`. If you have questions, feel free to open a github issue.
190+
165191
### Usage Examples
166192

167193
Please have an eye on our [HEXONET Backend API documentation](https://github.com/hexonet/hexonet-api-documentation/tree/master/API). Here you can find information on available Commands and their response data.

src/main/java/net/hexonet/apiconnector/APIClient.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class APIClient {
5151
private String ua;
5252
/** additional connection settings */
5353
private Map<String, String> curlopts;
54+
/** logger instance */
55+
private Logger logger;
5456

5557
/**
5658
* Class constructor. Creates an API Client ready to use.
@@ -62,6 +64,28 @@ public APIClient() {
6264
this.socketConfig = new SocketConfig();
6365
this.useLIVESystem();
6466
this.curlopts = new HashMap<String, String>();
67+
this.setDefaultLogger();
68+
}
69+
70+
/**
71+
* Set a custom logger for debug mdoe
72+
*
73+
* @param logger
74+
* @return Current APIClient instance for method chaining
75+
*/
76+
public APIClient setCustomLogger(Logger logger) {
77+
this.logger = logger;
78+
return this;
79+
}
80+
81+
/**
82+
* Activate the default logger for debug mode
83+
*
84+
* @return Current APIClient instance for method chaining
85+
*/
86+
public APIClient setDefaultLogger() {
87+
this.logger = new Logger();
88+
return this;
6589
}
6690

6791
/**
@@ -472,6 +496,7 @@ public Response request(Map<String, Object> cmd) {
472496
cfg.put("CONNECTION_URL", this.socketURL);
473497

474498
StringBuilder response;
499+
Response r;
475500
try {
476501
response = new StringBuilder("");
477502
URL myurl = new URL(cfg.get("CONNECTION_URL"));
@@ -513,15 +538,15 @@ public Response request(Map<String, Object> cmd) {
513538
} catch (Exception e) {
514539
ResponseTemplate tpl = ResponseTemplateManager.getInstance().getTemplate("httperror");
515540
response = new StringBuilder(tpl.getPlain());
541+
r = new Response(response.toString(), newcmd, cfg);
516542
if (this.debugMode) {
517-
System.err.println(e);
543+
this.logger.log(secured, r, e.getMessage());
518544
}
545+
return r;
519546
}
520-
Response r = new Response(response.toString(), newcmd, cfg);
547+
r = new Response(response.toString(), newcmd, cfg);
521548
if (this.debugMode) {
522-
System.out.println(secured);
523-
System.out.println(newcmd);
524-
System.out.println(r.getPlain());
549+
this.logger.log(secured, r);
525550
}
526551
return r;
527552
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package net.hexonet.apiconnector; // of course use your namespace instead of our one
2+
3+
/**
4+
* Custom Logger class example for debug outputs
5+
*
6+
* @author Kai Schwarz
7+
* @version %I%, %G%
8+
* @since 3.2
9+
*/
10+
public class CustomLogger {
11+
/**
12+
* log incoming data
13+
*
14+
* @param post request data
15+
* @param r API response object instance
16+
* @param error http error message or null
17+
*/
18+
public void log(String post, Response r, String error) {
19+
// your logic here
20+
21+
// System.out.println(r.getCommandPlain());
22+
// System.out.println(post);
23+
// if (error != null) {
24+
// System.err.println("HTTP communication failed: " + error);
25+
// }
26+
}
27+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package net.hexonet.apiconnector;
2+
3+
/**
4+
* Default Logger class for debug outputs Overridable by custom integration, check
5+
* CustomLogger.java.
6+
*
7+
* @author Kai Schwarz
8+
* @version %I%, %G%
9+
* @since 3.2
10+
*/
11+
public class Logger {
12+
/**
13+
* log incoming data
14+
*
15+
* @param post request data
16+
* @param r API response object instance
17+
*/
18+
public void log(String post, Response r) {
19+
this.log(post, r, null);
20+
}
21+
22+
/**
23+
* log incoming data
24+
*
25+
* @param post request data
26+
* @param r API response object instance
27+
* @param error http error message or null
28+
*/
29+
public void log(String post, Response r, String error) {
30+
System.out.println(r.getCommandPlain());
31+
System.out.println(post);
32+
if (error != null) {
33+
System.err.println("HTTP communication failed: " + error);
34+
}
35+
}
36+
}

target/apidocs/allclasses-index.html

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<!-- NewPage -->
33
<html lang="en">
44
<head>
5-
<!-- Generated by javadoc (11.0.7) on Tue Apr 28 11:11:02 CEST 2020 -->
6-
<title>All Classes (java-sdk 3.0.0 API)</title>
5+
<!-- Generated by javadoc (11.0.7) on Tue Apr 28 19:40:25 CEST 2020 -->
6+
<title>All Classes (java-sdk 3.1.0 API)</title>
77
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
88
<meta name="dc.created" content="2020-04-28">
99
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">
@@ -22,7 +22,7 @@
2222
<script type="text/javascript"><!--
2323
try {
2424
if (location.href.indexOf('is-external=true') == -1) {
25-
parent.document.title="All Classes (java-sdk 3.0.0 API)";
25+
parent.document.title="All Classes (java-sdk 3.1.0 API)";
2626
}
2727
}
2828
catch(err) {
@@ -118,37 +118,50 @@ <h1 title="All&amp;nbsp;Classes" class="title">All&nbsp;Classes</h1>
118118
</th>
119119
</tr>
120120
<tr id="i2" class="altColor">
121+
<td class="colFirst"><a href="net/hexonet/apiconnector/CustomLogger.html" title="class in net.hexonet.apiconnector">CustomLogger</a></td>
122+
<th class="colLast" scope="row">
123+
<div class="block">Custom Logger class example for debug outputs</div>
124+
</th>
125+
</tr>
126+
<tr id="i3" class="rowColor">
127+
<td class="colFirst"><a href="net/hexonet/apiconnector/Logger.html" title="class in net.hexonet.apiconnector">Logger</a></td>
128+
<th class="colLast" scope="row">
129+
<div class="block">Default Logger class for debug outputs Overridable by custom integration, check
130+
CustomLogger.java.</div>
131+
</th>
132+
</tr>
133+
<tr id="i4" class="altColor">
121134
<td class="colFirst"><a href="net/hexonet/apiconnector/Record.html" title="class in net.hexonet.apiconnector">Record</a></td>
122135
<th class="colLast" scope="row">
123136
<div class="block">Record covers Row Data in a better accessible way</div>
124137
</th>
125138
</tr>
126-
<tr id="i3" class="rowColor">
139+
<tr id="i5" class="rowColor">
127140
<td class="colFirst"><a href="net/hexonet/apiconnector/Response.html" title="class in net.hexonet.apiconnector">Response</a></td>
128141
<th class="colLast" scope="row">
129142
<div class="block">Response covers all functionality to wrap a Backend API Response like accessing data</div>
130143
</th>
131144
</tr>
132-
<tr id="i4" class="altColor">
145+
<tr id="i6" class="altColor">
133146
<td class="colFirst"><a href="net/hexonet/apiconnector/ResponseParser.html" title="class in net.hexonet.apiconnector">ResponseParser</a></td>
134147
<th class="colLast" scope="row">
135148
<div class="block">ResponseParser covers all functionality to parse and serialize API response data</div>
136149
</th>
137150
</tr>
138-
<tr id="i5" class="rowColor">
151+
<tr id="i7" class="rowColor">
139152
<td class="colFirst"><a href="net/hexonet/apiconnector/ResponseTemplate.html" title="class in net.hexonet.apiconnector">ResponseTemplate</a></td>
140153
<th class="colLast" scope="row">
141154
<div class="block">ResponseTemplate is the base class to work with parsed API response data or hardcoded response
142155
template data</div>
143156
</th>
144157
</tr>
145-
<tr id="i6" class="altColor">
158+
<tr id="i8" class="altColor">
146159
<td class="colFirst"><a href="net/hexonet/apiconnector/ResponseTemplateManager.html" title="class in net.hexonet.apiconnector">ResponseTemplateManager</a></td>
147160
<th class="colLast" scope="row">
148161
<div class="block">ResponseTemplateManager covers functionality to manage response tempaltes</div>
149162
</th>
150163
</tr>
151-
<tr id="i7" class="rowColor">
164+
<tr id="i9" class="rowColor">
152165
<td class="colFirst"><a href="net/hexonet/apiconnector/SocketConfig.html" title="class in net.hexonet.apiconnector">SocketConfig</a></td>
153166
<th class="colLast" scope="row">
154167
<div class="block">SocketConfig is the base class to configure the socket for API communication</div>

target/apidocs/allclasses.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<!-- NewPage -->
33
<html lang="en">
44
<head>
5-
<!-- Generated by javadoc (11.0.7) on Tue Apr 28 11:11:02 CEST 2020 -->
6-
<title>All Classes (java-sdk 3.0.0 API)</title>
5+
<!-- Generated by javadoc (11.0.7) on Tue Apr 28 19:40:25 CEST 2020 -->
6+
<title>All Classes (java-sdk 3.1.0 API)</title>
77
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
88
<meta name="dc.created" content="2020-04-28">
99
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">
@@ -24,6 +24,8 @@ <h1 class="bar">All&nbsp;Classes</h1>
2424
<ul>
2525
<li><a href="net/hexonet/apiconnector/APIClient.html" title="class in net.hexonet.apiconnector">APIClient</a></li>
2626
<li><a href="net/hexonet/apiconnector/Column.html" title="class in net.hexonet.apiconnector">Column</a></li>
27+
<li><a href="net/hexonet/apiconnector/CustomLogger.html" title="class in net.hexonet.apiconnector">CustomLogger</a></li>
28+
<li><a href="net/hexonet/apiconnector/Logger.html" title="class in net.hexonet.apiconnector">Logger</a></li>
2729
<li><a href="net/hexonet/apiconnector/Record.html" title="class in net.hexonet.apiconnector">Record</a></li>
2830
<li><a href="net/hexonet/apiconnector/Response.html" title="class in net.hexonet.apiconnector">Response</a></li>
2931
<li><a href="net/hexonet/apiconnector/ResponseParser.html" title="class in net.hexonet.apiconnector">ResponseParser</a></li>

target/apidocs/allpackages-index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<!-- NewPage -->
33
<html lang="en">
44
<head>
5-
<!-- Generated by javadoc (11.0.7) on Tue Apr 28 11:11:02 CEST 2020 -->
6-
<title>All Packages (java-sdk 3.0.0 API)</title>
5+
<!-- Generated by javadoc (11.0.7) on Tue Apr 28 19:40:25 CEST 2020 -->
6+
<title>All Packages (java-sdk 3.1.0 API)</title>
77
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
88
<meta name="dc.created" content="2020-04-28">
99
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">
@@ -22,7 +22,7 @@
2222
<script type="text/javascript"><!--
2323
try {
2424
if (location.href.indexOf('is-external=true') == -1) {
25-
parent.document.title="All Packages (java-sdk 3.0.0 API)";
25+
parent.document.title="All Packages (java-sdk 3.1.0 API)";
2626
}
2727
}
2828
catch(err) {

target/apidocs/constant-values.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<!-- NewPage -->
33
<html lang="en">
44
<head>
5-
<!-- Generated by javadoc (11.0.7) on Tue Apr 28 11:11:02 CEST 2020 -->
6-
<title>Constant Field Values (java-sdk 3.0.0 API)</title>
5+
<!-- Generated by javadoc (11.0.7) on Tue Apr 28 19:40:25 CEST 2020 -->
6+
<title>Constant Field Values (java-sdk 3.1.0 API)</title>
77
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
88
<meta name="dc.created" content="2020-04-28">
99
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">
@@ -22,7 +22,7 @@
2222
<script type="text/javascript"><!--
2323
try {
2424
if (location.href.indexOf('is-external=true') == -1) {
25-
parent.document.title="Constant Field Values (java-sdk 3.0.0 API)";
25+
parent.document.title="Constant Field Values (java-sdk 3.1.0 API)";
2626
}
2727
}
2828
catch(err) {

target/apidocs/deprecated-list.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<!-- NewPage -->
33
<html lang="en">
44
<head>
5-
<!-- Generated by javadoc (11.0.7) on Tue Apr 28 11:11:02 CEST 2020 -->
6-
<title>Deprecated List (java-sdk 3.0.0 API)</title>
5+
<!-- Generated by javadoc (11.0.7) on Tue Apr 28 19:40:25 CEST 2020 -->
6+
<title>Deprecated List (java-sdk 3.1.0 API)</title>
77
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
88
<meta name="dc.created" content="2020-04-28">
99
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">
@@ -22,7 +22,7 @@
2222
<script type="text/javascript"><!--
2323
try {
2424
if (location.href.indexOf('is-external=true') == -1) {
25-
parent.document.title="Deprecated List (java-sdk 3.0.0 API)";
25+
parent.document.title="Deprecated List (java-sdk 3.1.0 API)";
2626
}
2727
}
2828
catch(err) {

target/apidocs/help-doc.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<!-- NewPage -->
33
<html lang="en">
44
<head>
5-
<!-- Generated by javadoc (11.0.7) on Tue Apr 28 11:11:02 CEST 2020 -->
6-
<title>API Help (java-sdk 3.0.0 API)</title>
5+
<!-- Generated by javadoc (11.0.7) on Tue Apr 28 19:40:25 CEST 2020 -->
6+
<title>API Help (java-sdk 3.1.0 API)</title>
77
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
88
<meta name="dc.created" content="2020-04-28">
99
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">
@@ -22,7 +22,7 @@
2222
<script type="text/javascript"><!--
2323
try {
2424
if (location.href.indexOf('is-external=true') == -1) {
25-
parent.document.title="API Help (java-sdk 3.0.0 API)";
25+
parent.document.title="API Help (java-sdk 3.1.0 API)";
2626
}
2727
}
2828
catch(err) {

0 commit comments

Comments
 (0)