Skip to content

Commit 4da06f1

Browse files
committed
LinkChecker: Use logging instead of direct printing into console
1 parent 9d27899 commit 4da06f1

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ repositories {
2727
dependencies {
2828
implementation(libs.jackson.databind)
2929
implementation(libs.mockneat)
30+
implementation(libs.bundles.logger.api)
31+
32+
runtimeOnly(libs.logger.impl)
3033

3134
testImplementation(kotlin("test"))
3235
testImplementation(platform(libs.junit))

gradle/libs.versions.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ junit = { module = "org.junit:junit-bom", version.ref = "junit" }
1111
assertj = { module = "org.assertj:assertj-core", version = "3.23.1" }
1212
mockneat = { module = "net.andreinc:mockneat", version = "0.4.8" }
1313
jmh-ann = { module = "org.openjdk.jmh:jmh-generator-annprocess", version.ref = "jmh.tools" }
14+
logger-api4j = { module = "org.slf4j:slf4j-api", version = "2.0.6" }
15+
logger-api4k = { module = "io.github.microutils:kotlin-logging-jvm", version = "3.0.4" }
16+
logger-impl = { module = "ch.qos.logback:logback-classic", version = "1.4.5" }
1417

1518
[bundles]
19+
logger-api = ["logger.api4j", "logger.api4k"]
1620

1721
[plugins]
1822
jmh = { id = "me.champeau.jmh", version = "0.6.8" }

src/main/kotlin/name/valery1707/problem/LinkChecker.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class LinkChecker(private val root: Path) {
4040
.filter { it.second.second.first != 200 }
4141
.toList()
4242
// todo remove
43-
println("filePos2uriCheck = $filePos2uriCheck")
43+
logger.debug { "filePos2uriCheck = $filePos2uriCheck" }
4444
return filePos2uriCheck
4545
.associateBy(
4646
{ "${it.first.first}:${it.first.second}" },
@@ -59,6 +59,8 @@ class LinkChecker(private val root: Path) {
5959
}
6060

6161
companion object {
62+
private val logger = mu.KotlinLogging.logger {}
63+
6264
/**
6365
* https://stackoverflow.com/a/45690571
6466
*/
@@ -104,8 +106,7 @@ class LinkChecker(private val root: Path) {
104106
val request = HttpRequest.newBuilder(this).GET().build()
105107
// todo Cache
106108
return try {
107-
// todo Logging
108-
println("Check: $this")
109+
logger.info("Check: $this")
109110
val response = client.send(request, HttpResponse.BodyHandlers.discarding())
110111
when (response.statusCode()) {
111112
//Redirects: extract new location
@@ -128,16 +129,15 @@ class LinkChecker(private val root: Path) {
128129

129130
?: 500
130131

131-
// todo Logging
132-
println("Await: $await ms")
132+
logger.debug("Await: $await ms")
133133
Thread.sleep(await)
134134
check(client)
135135
}
136136

137137
else -> response.statusCode() to response.uri()
138138
}
139139
} catch (e: Exception) {
140-
// todo Logging
140+
logger.error(e) { "Handle error on checking $this" }
141141
-1 to URI.create("http://host?message=${e.message?.replace(" ", "%20")}")
142142
}
143143
}

0 commit comments

Comments
 (0)