Skip to content

Commit 28c6a39

Browse files
Merge pull request #53 from muhammadzkralla/logger
feat: Introducing a way to close printing logs to the console from the client.
2 parents be0a9b7 + 4e2a944 commit 28c6a39

File tree

8 files changed

+62
-34
lines changed

8 files changed

+62
-34
lines changed

zhttp/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ publishing {
2828

2929
groupId = "com.github.muhammadzkralla"
3030
artifactId = "zhttp-jvm"
31-
version = "2.8.8"
31+
version = "2.8.9"
3232
}
3333
}
3434
}

zhttp/src/main/java/com/zkrallah/zhttp/client/ZHttpClient.kt

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class ZHttpClient private constructor(builder: Builder) {
3333
private val filesBufferSize = builder.getBufferSize()
3434
private val gson = Gson()
3535
val requestRetryMechanism = builder.getRequestRetryMechanism()
36+
var allowLogging = builder.getAllowLogging()
3637

3738
/**
3839
* Get the base URL of the client.
@@ -409,9 +410,9 @@ class ZHttpClient private constructor(builder: Builder) {
409410
var retryCount = requestRetryMechanism!!.retryCount
410411

411412
while (retryCount-- > 0) {
412-
println("ZHttp: $type: remaining attempts :$retryCount")
413+
if (allowLogging) println("ZHttp: $type: remaining attempts :$retryCount")
413414
val response = requestBlock()
414-
println("ZHttp: $type: response :$response")
415+
if (allowLogging) println("ZHttp: $type: response :$response")
415416

416417
if (response != null) {
417418
if (response.exception == null || requestRetryMechanism.retryOnExceptions.none {
@@ -426,7 +427,7 @@ class ZHttpClient private constructor(builder: Builder) {
426427
}
427428

428429
if (retryCount == 0 && response != null) {
429-
System.err.println("ZHttp: $type: maximum attempts exceeded.")
430+
if (allowLogging) System.err.println("ZHttp: $type: maximum attempts exceeded.")
430431
return response
431432
}
432433

@@ -452,6 +453,7 @@ class ZHttpClient private constructor(builder: Builder) {
452453
)
453454
private var filesBufferSize = 1024
454455
private var requestRetryMechanism: RequestRetryMechanism? = null
456+
private var allowLogging = false
455457

456458
/**
457459
* Set the base URL for the HTTP client.
@@ -554,6 +556,17 @@ class ZHttpClient private constructor(builder: Builder) {
554556
return this
555557
}
556558

559+
/**
560+
* Set the logging status for the HTTP client.
561+
*
562+
* @param allowLogging The logging status to be set.
563+
* @return This builder instance for method chaining.
564+
*/
565+
fun allowLogging(allowLogging: Boolean): Builder {
566+
this.allowLogging = allowLogging
567+
return this
568+
}
569+
557570
/**
558571
* Get the base URL configured in the builder.
559572
*
@@ -608,6 +621,15 @@ class ZHttpClient private constructor(builder: Builder) {
608621
return requestRetryMechanism
609622
}
610623

624+
/**
625+
* Get the logging status configured in the builder.
626+
*
627+
* @return The status.
628+
*/
629+
internal fun getAllowLogging(): Boolean {
630+
return allowLogging
631+
}
632+
611633
/**
612634
* Build and return a new instance of ZHttpClient with the configured settings.
613635
*

zhttp/src/main/java/com/zkrallah/zhttp/core/ZDelete.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class ZDelete(val client: ZHttpClient) {
7373
}
7474
} catch (e: SocketTimeoutException) {
7575
// If a socket timeout occurs, return an HttpResponse with the exception
76-
System.err.println("ZHttp: doDelete: $e")
76+
if (client.allowLogging) System.err.println("ZHttp: doDelete: $e")
7777
return HttpResponse(exception = e)
7878
} catch (e: Exception) {
7979
// If there's an error, read the error stream for additional information
@@ -83,7 +83,8 @@ class ZDelete(val client: ZHttpClient) {
8383
while (reader.readLine().also { line = it } != null) response.append(line)
8484
}
8585
} catch (e: Exception) {
86-
System.err.println("ZHttp: doDelete: $e. No error stream sent by the server")
86+
if (client.allowLogging) System.err.println("ZHttp: doDelete: $e. No error stream sent by the server")
87+
return HttpResponse(exception = e)
8788
}
8889
}
8990

@@ -97,7 +98,7 @@ class ZDelete(val client: ZHttpClient) {
9798
)
9899
} catch (e: Exception) {
99100
// If an exception occurs, log the error and return an HttpResponse with the exception
100-
System.err.println("ZHttp: doDelete: $e")
101+
if (client.allowLogging) System.err.println("ZHttp: doDelete: $e")
101102
HttpResponse(exception = e)
102103
} finally {
103104
// Disconnect the connection when done
@@ -121,7 +122,7 @@ class ZDelete(val client: ZHttpClient) {
121122
try {
122123
doDelete(endpoint, queries, headers)
123124
} catch (e: Exception) {
124-
System.err.println("ZHttp: doSuspendedDeleteRequest: $e")
125+
if (client.allowLogging) System.err.println("ZHttp: doSuspendedDeleteRequest: $e")
125126
val response = HttpResponse(exception = e)
126127
response
127128
}
@@ -143,7 +144,7 @@ class ZDelete(val client: ZHttpClient) {
143144
val response = doSuspendedDeleteRequest(endpoint, queries, headers).await() ?: return null
144145

145146
response.exception?.let {
146-
System.err.println("ZHttp: processDelete: $it")
147+
if (client.allowLogging) System.err.println("ZHttp: processDelete: $it")
147148
}
148149

149150
val body = try {

zhttp/src/main/java/com/zkrallah/zhttp/core/ZGet.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class ZGet(val client: ZHttpClient) {
7373
}
7474
} catch (e: SocketTimeoutException) {
7575
// If a socket timeout occurs, return an HttpResponse with the exception
76-
System.err.println("ZHttp: doGet: $e")
76+
if (client.allowLogging) System.err.println("ZHttp: doGet: $e")
7777
return HttpResponse(exception = e)
7878
} catch (e: Exception) {
7979
// If there's an error, read the error stream for additional information
@@ -83,7 +83,8 @@ class ZGet(val client: ZHttpClient) {
8383
while (reader.readLine().also { line = it } != null) response.append(line)
8484
}
8585
} catch (e: Exception) {
86-
System.err.println("ZHttp: doGet: $e. No error stream sent by the server")
86+
if (client.allowLogging) System.err.println("ZHttp: doGet: $e. No error stream sent by the server")
87+
return HttpResponse(exception = e)
8788
}
8889
}
8990

@@ -97,7 +98,7 @@ class ZGet(val client: ZHttpClient) {
9798
)
9899
} catch (e: Exception) {
99100
// If an exception occurs, log the error and return an HttpResponse with the exception
100-
System.err.println("ZHttp: doGet: $e")
101+
if (client.allowLogging) System.err.println("ZHttp: doGet: $e")
101102
HttpResponse(exception = e)
102103
} finally {
103104
// Disconnect the connection when done
@@ -121,7 +122,7 @@ class ZGet(val client: ZHttpClient) {
121122
try {
122123
doGet(endpoint, queries, headers)
123124
} catch (e: Exception) {
124-
System.err.println("ZHttp: doSuspendedGetRequest: $e")
125+
if (client.allowLogging) System.err.println("ZHttp: doSuspendedGetRequest: $e")
125126
val response = HttpResponse(exception = e)
126127
response
127128
}
@@ -143,7 +144,7 @@ class ZGet(val client: ZHttpClient) {
143144
val response = doSuspendedGetRequest(endpoint, queries, headers).await() ?: return null
144145

145146
response.exception?.let {
146-
System.err.println("ZHttp: processGet: $it")
147+
if (client.allowLogging) System.err.println("ZHttp: processGet: $it")
147148
}
148149

149150
val body = try {

zhttp/src/main/java/com/zkrallah/zhttp/core/ZMultipart.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class ZMultipart(val client: ZHttpClient) {
116116
}
117117
} catch (e: SocketTimeoutException) {
118118
// If a socket timeout occurs, return an HttpResponse with the exception
119-
System.err.println("ZHttp: doMultipart: $e")
119+
if (client.allowLogging) System.err.println("ZHttp: doMultipart: $e")
120120
return HttpResponse(exception = e)
121121
} catch (e: Exception) {
122122
// If there's an error, read the error stream for additional information
@@ -126,7 +126,8 @@ class ZMultipart(val client: ZHttpClient) {
126126
while (reader.readLine().also { line = it } != null) response.append(line)
127127
}
128128
} catch (e: Exception) {
129-
System.err.println("ZHttp: doMultipart: $e. No error stream sent by the server")
129+
if (client.allowLogging) System.err.println("ZHttp: doMultipart: $e. No error stream sent by the server")
130+
return HttpResponse(exception = e)
130131
}
131132
}
132133

@@ -140,7 +141,7 @@ class ZMultipart(val client: ZHttpClient) {
140141
)
141142
} catch (e: Exception) {
142143
// If an exception occurs, log the error and return an HttpResponse with the exception
143-
System.err.println("ZHttp: doMultipart: $e")
144+
if (client.allowLogging) System.err.println("ZHttp: doMultipart: $e")
144145
HttpResponse(exception = e)
145146
} finally {
146147
// Disconnect the connection when done
@@ -165,7 +166,7 @@ class ZMultipart(val client: ZHttpClient) {
165166
try {
166167
doMultipart(endpoint, parts, queries, headers)
167168
} catch (e: Exception) {
168-
System.err.println("ZHttp doSuspendedMultipartRequest: $e")
169+
if (client.allowLogging) System.err.println("ZHttp doSuspendedMultipartRequest: $e")
169170
val response = HttpResponse(exception = e)
170171
response
171172
}
@@ -189,7 +190,7 @@ class ZMultipart(val client: ZHttpClient) {
189190
doSuspendedMultipartRequest(endpoint, parts, queries, headers).await() ?: return null
190191

191192
response.exception?.let {
192-
System.err.println("ZHttp: processMultiPart: $it")
193+
if (client.allowLogging) System.err.println("ZHttp: processMultiPart: $it")
193194
}
194195

195196
val body = try {

zhttp/src/main/java/com/zkrallah/zhttp/core/ZPatch.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class ZPatch(val client: ZHttpClient) {
8484
}
8585
} catch (e: SocketTimeoutException) {
8686
// If a socket timeout occurs, return an HttpResponse with the exception
87-
System.err.println("ZHttp: doPatch: $e")
87+
if (client.allowLogging) System.err.println("ZHttp: doPatch: $e")
8888
return HttpResponse(exception = e)
8989
} catch (e: Exception) {
9090
// If there's an error, read the error stream for additional information
@@ -94,7 +94,8 @@ class ZPatch(val client: ZHttpClient) {
9494
while (reader.readLine().also { line = it } != null) response.append(line)
9595
}
9696
} catch (e: Exception) {
97-
System.err.println("ZHttp: doPatch: $e. No error stream sent by the server")
97+
if (client.allowLogging) System.err.println("ZHttp: doPatch: $e. No error stream sent by the server")
98+
return HttpResponse(exception = e)
9899
}
99100
}
100101

@@ -108,7 +109,7 @@ class ZPatch(val client: ZHttpClient) {
108109
)
109110
} catch (e: Exception) {
110111
// If an exception occurs, log the error and return an HttpResponse with the exception
111-
System.err.println("ZHttp: doPatch: $e")
112+
if (client.allowLogging) System.err.println("ZHttp: doPatch: $e")
112113
HttpResponse(exception = e)
113114
} finally {
114115
// Disconnect the connection when done
@@ -133,7 +134,7 @@ class ZPatch(val client: ZHttpClient) {
133134
try {
134135
doPatch(endpoint, requestBody, queries, headers)
135136
} catch (e: Exception) {
136-
System.err.println("ZHttp: doSuspendedPatchRequest: $e")
137+
if (client.allowLogging) System.err.println("ZHttp: doSuspendedPatchRequest: $e")
137138
val response = HttpResponse(exception = e)
138139
response
139140
}
@@ -157,7 +158,7 @@ class ZPatch(val client: ZHttpClient) {
157158
doSuspendedPatchRequest(endpoint, requestBody, queries, headers).await() ?: return null
158159

159160
response.exception?.let {
160-
System.err.println("ZHttp: processPatch: $it")
161+
if (client.allowLogging) System.err.println("ZHttp: processPatch: $it")
161162
}
162163

163164
val body = try {

zhttp/src/main/java/com/zkrallah/zhttp/core/ZPost.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class ZPost(val client: ZHttpClient) {
8484
}
8585
} catch (e: SocketTimeoutException) {
8686
// If a socket timeout occurs, return an HttpResponse with the exception
87-
System.err.println("ZHttp: doPost: $e")
87+
if (client.allowLogging) System.err.println("ZHttp: doPost: $e")
8888
return HttpResponse(exception = e)
8989
} catch (e: Exception) {
9090
// If there's an error, read the error stream for additional information
@@ -94,7 +94,8 @@ class ZPost(val client: ZHttpClient) {
9494
while (reader.readLine().also { line = it } != null) response.append(line)
9595
}
9696
} catch (e: Exception) {
97-
System.err.println("ZHttp: doPost: $e. No error stream sent by the server")
97+
if (client.allowLogging) System.err.println("ZHttp: doPost: $e. No error stream sent by the server")
98+
return HttpResponse(exception = e)
9899
}
99100
}
100101

@@ -108,7 +109,7 @@ class ZPost(val client: ZHttpClient) {
108109
)
109110
} catch (e: Exception) {
110111
// If an exception occurs, log the error and return an HttpResponse with the exception
111-
System.err.println("ZHttp: doPost: $e")
112+
if (client.allowLogging) System.err.println("ZHttp: doPost: $e")
112113
HttpResponse(exception = e)
113114
} finally {
114115
// Disconnect the connection when done
@@ -133,7 +134,7 @@ class ZPost(val client: ZHttpClient) {
133134
try {
134135
doPost(endpoint, requestBody, queries, headers)
135136
} catch (e: Exception) {
136-
System.err.println("ZHttp: doSuspendedPostRequest: $e")
137+
if (client.allowLogging) System.err.println("ZHttp: doSuspendedPostRequest: $e")
137138
val response = HttpResponse(exception = e)
138139
response
139140
}
@@ -157,7 +158,7 @@ class ZPost(val client: ZHttpClient) {
157158
doSuspendedPostRequest(endpoint, requestBody, queries, headers).await() ?: return null
158159

159160
response.exception?.let {
160-
System.err.println("ZHttp: processPost: $it")
161+
if (client.allowLogging) System.err.println("ZHttp: processPost: $it")
161162
}
162163

163164
val body = try {

zhttp/src/main/java/com/zkrallah/zhttp/core/ZPut.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class ZPut(val client: ZHttpClient) {
8484
}
8585
} catch (e: SocketTimeoutException) {
8686
// If a socket timeout occurs, return an HttpResponse with the exception
87-
System.err.println("ZHttp: doPut: $e")
87+
if (client.allowLogging) System.err.println("ZHttp: doPut: $e")
8888
return HttpResponse(exception = e)
8989
} catch (e: Exception) {
9090
// If there's an error, read the error stream for additional information
@@ -94,7 +94,8 @@ class ZPut(val client: ZHttpClient) {
9494
while (reader.readLine().also { line = it } != null) response.append(line)
9595
}
9696
} catch (e: Exception) {
97-
System.err.println("ZHttp: doPut: $e. No error stream sent by the server")
97+
if (client.allowLogging) System.err.println("ZHttp: doPut: $e. No error stream sent by the server")
98+
return HttpResponse(exception = e)
9899
}
99100
}
100101

@@ -108,7 +109,7 @@ class ZPut(val client: ZHttpClient) {
108109
)
109110
} catch (e: Exception) {
110111
// If an exception occurs, log the error and return an HttpResponse with the exception
111-
System.err.println("ZHttp: doPut: $e")
112+
if (client.allowLogging) System.err.println("ZHttp: doPut: $e")
112113
HttpResponse(exception = e)
113114
} finally {
114115
// Disconnect the connection when done
@@ -133,7 +134,7 @@ class ZPut(val client: ZHttpClient) {
133134
try {
134135
doPut(endpoint, requestBody, queries, headers)
135136
} catch (e: Exception) {
136-
System.err.println("ZHttp: doSuspendedPutRequest: $e")
137+
if (client.allowLogging) System.err.println("ZHttp: doSuspendedPutRequest: $e")
137138
val response = HttpResponse(exception = e)
138139
response
139140
}
@@ -157,7 +158,7 @@ class ZPut(val client: ZHttpClient) {
157158
doSuspendedPutRequest(endpoint, requestBody, queries, headers).await() ?: return null
158159

159160
response.exception?.let {
160-
System.err.println("ZHttp: processPut: $it")
161+
if (client.allowLogging) System.err.println("ZHttp: processPut: $it")
161162
}
162163

163164
val body = try {

0 commit comments

Comments
 (0)