Skip to content

Commit cf4e7fd

Browse files
Fix: make network calls be logged using slf4j. (#343)
* Fix: make network calls be logged using slf4j. Now Okhttp's network calls are routed through SLF4J just like rest of SDK/Client APP that uses SDK logs so everything is formatted consistently and appears in the correct chronological order. Previously OkHttp was using the JVM's default java.util.logging, which caused its HTTP-level logs to be emitted separately and sometimes interleaved out of sequence. * PubNub SDK v10.4.8 release. --------- Co-authored-by: PubNub Release Bot <120067856+pubnub-release-bot@users.noreply.github.com>
1 parent 31e850c commit cf4e7fd

File tree

6 files changed

+37
-9
lines changed

6 files changed

+37
-9
lines changed

.pubnub.yml

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: kotlin
2-
version: 10.4.7
2+
version: 10.4.8
33
schema: 1
44
scm: github.com/pubnub/kotlin
55
files:
6-
- build/libs/pubnub-kotlin-10.4.7-all.jar
6+
- build/libs/pubnub-kotlin-10.4.8-all.jar
77
sdks:
88
-
99
type: library
@@ -23,8 +23,8 @@ sdks:
2323
-
2424
distribution-type: library
2525
distribution-repository: maven
26-
package-name: pubnub-kotlin-10.4.7
27-
location: https://repo.maven.apache.org/maven2/com/pubnub/pubnub-kotlin/10.4.7/pubnub-kotlin-10.4.7.jar
26+
package-name: pubnub-kotlin-10.4.8
27+
location: https://repo.maven.apache.org/maven2/com/pubnub/pubnub-kotlin/10.4.8/pubnub-kotlin-10.4.8.jar
2828
supported-platforms:
2929
supported-operating-systems:
3030
Android:
@@ -121,6 +121,11 @@ sdks:
121121
license-url: https://www.apache.org/licenses/LICENSE-2.0.txt
122122
is-required: Required
123123
changelog:
124+
- date: 2025-04-24
125+
version: v10.4.8
126+
changes:
127+
- type: bug
128+
text: "Make network calls be logged using slf4j."
124129
- date: 2025-04-15
125130
version: v10.4.7
126131
changes:

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v10.4.8
2+
April 24 2025
3+
4+
#### Fixed
5+
- Make network calls be logged using slf4j.
6+
17
## v10.4.7
28
April 15 2025
39

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ You will need the publish and subscribe keys to authenticate your app. Get your
2020
<dependency>
2121
<groupId>com.pubnub</groupId>
2222
<artifactId>pubnub-kotlin</artifactId>
23-
<version>10.4.7</version>
23+
<version>10.4.8</version>
2424
</dependency>
2525
```
2626

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ RELEASE_SIGNING_ENABLED=true
1818
SONATYPE_HOST=DEFAULT
1919
SONATYPE_AUTOMATIC_RELEASE=false
2020
GROUP=com.pubnub
21-
VERSION_NAME=10.4.7
21+
VERSION_NAME=10.4.8
2222
POM_PACKAGING=jar
2323

2424
POM_NAME=PubNub SDK

pubnub-kotlin/pubnub-kotlin-impl/src/main/kotlin/com/pubnub/internal/managers/RetrofitManager.kt

+19-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ import okhttp3.Call
2222
import okhttp3.OkHttpClient
2323
import okhttp3.logging.HttpLoggingInterceptor
2424
import org.jetbrains.annotations.TestOnly
25+
import org.slf4j.LoggerFactory
26+
import org.slf4j.helpers.NOPLoggerFactory
2527
import retrofit2.Retrofit
2628
import java.util.concurrent.ExecutorService
2729
import java.util.concurrent.TimeUnit
2830

31+
private const val PUBNUB_OKHTTP_REQUEST_RESPONSE_LOGGER_NAME = "pubnub.okhttp"
32+
2933
class RetrofitManager(
3034
val pubnub: PubNubImpl,
3135
private val configuration: PNConfiguration,
@@ -108,9 +112,17 @@ class RetrofitManager(
108112
with(configuration) {
109113
if (logVerbosity == PNLogVerbosity.BODY) {
110114
okHttpBuilder.addInterceptor(
111-
HttpLoggingInterceptor().apply {
115+
HttpLoggingInterceptor { message ->
116+
if (slf4jIsBound()) {
117+
// will follow whatever SLF4J config (logback, log4j2, etc.) is on the classpath
118+
LoggerFactory.getLogger(PUBNUB_OKHTTP_REQUEST_RESPONSE_LOGGER_NAME).debug(message)
119+
} else {
120+
// fallback: always print
121+
println("[$PUBNUB_OKHTTP_REQUEST_RESPONSE_LOGGER_NAME] $message")
122+
}
123+
}.apply {
112124
level = HttpLoggingInterceptor.Level.BODY
113-
},
125+
}
114126
)
115127
}
116128

@@ -144,6 +156,11 @@ class RetrofitManager(
144156
return okHttpClient
145157
}
146158

159+
private fun slf4jIsBound(): Boolean {
160+
val factory = LoggerFactory.getILoggerFactory()
161+
return factory !is NOPLoggerFactory
162+
}
163+
147164
private fun createRetrofit(callFactory: Call.Factory?): Retrofit {
148165
val retrofitBuilder =
149166
Retrofit.Builder()

pubnub-kotlin/pubnub-kotlin-impl/src/test/kotlin/com/pubnub/api/legacy/PubNubImplTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class PubNubImplTest : BaseTest() {
5656
fun getVersionAndTimeStamp() {
5757
val version = PubNubImpl.SDK_VERSION
5858
val timeStamp = PubNubImpl.timestamp()
59-
assertEquals("10.4.7", version)
59+
assertEquals("10.4.8", version)
6060
assertTrue(timeStamp > 0)
6161
}
6262

0 commit comments

Comments
 (0)