Skip to content

Commit eb76c23

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

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
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
}

versions.lock

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
# Run ./gradlew --write-locks to regenerate this file
2+
ch.qos.logback:logback-classic:1.4.5 (1 constraints: 0c050136)
3+
ch.qos.logback:logback-core:1.4.5 (1 constraints: 0b0d071d)
24
com.fasterxml.jackson:jackson-bom:2.14.1 (3 constraints: 7f37c0d3)
35
com.fasterxml.jackson.core:jackson-annotations:2.14.1 (2 constraints: 3f21f093)
46
com.fasterxml.jackson.core:jackson-core:2.14.1 (2 constraints: 3f21f093)
57
com.fasterxml.jackson.core:jackson-databind:2.14.1 (2 constraints: f1138582)
68
com.github.mifmif:generex:1.0.2 (1 constraints: e00921ac)
79
commons-codec:commons-codec:1.15 (1 constraints: b6094aa2)
810
dk.brics.automaton:automaton:1.11-8 (1 constraints: 900be8e7)
11+
io.github.microutils:kotlin-logging-jvm:3.0.4 (1 constraints: 0905fe35)
912
net.andreinc:aleph:0.1.1 (1 constraints: df091eac)
1013
net.andreinc:markovneat:1.8 (1 constraints: 88099b98)
1114
net.andreinc:mockneat:0.4.8 (1 constraints: 0e05ff35)
1215
org.apache.commons:commons-lang3:3.12.0 (2 constraints: ab17f565)
1316
org.apache.commons:commons-text:1.9 (1 constraints: 89099c98)
1417
org.jetbrains:annotations:13.0 (1 constraints: df0e795c)
1518
org.jetbrains.kotlin:kotlin-stdlib:1.7.22 (3 constraints: 463049c4)
16-
org.jetbrains.kotlin:kotlin-stdlib-common:1.7.22 (1 constraints: 450fd27a)
19+
org.jetbrains.kotlin:kotlin-stdlib-common:1.7.22 (2 constraints: 71207b44)
1720
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.22 (1 constraints: e310fbd2)
18-
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.22 (1 constraints: 3e05453b)
21+
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.22 (2 constraints: 6a16ed11)
22+
org.slf4j:slf4j-api:2.0.6 (3 constraints: 0723153f)
1923

2024
[Test dependencies]
2125
net.bytebuddy:byte-buddy:1.12.10 (1 constraints: 7b0bbcea)

0 commit comments

Comments
 (0)