Skip to content

Commit bf0b7ad

Browse files
sschuberthmnonnenmacher
authored andcommitted
Switch to slf4k and logback to be able to set the log level at runtime
SimpleLogger cannot set the log level at runtime, and kotlin-logging does not seem to expose a way to access the actual logger implementation in order to set the log level, see oshai/kotlin-logging#20 (comment) Change-Id: I8f769fc4113bff25ac2dc84732862523025edd68
1 parent 6fbcce3 commit bf0b7ad

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

build.gradle

+8-11
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,8 @@ dependencyUpdates.resolutionStrategy = {
3434
}
3535
}
3636

37-
mainClassName = 'Main'
38-
3937
applicationName = 'pran'
40-
applicationDefaultJvmArgs = [
41-
'-Dorg.slf4j.simpleLogger.levelInBrackets=true',
42-
'-Dorg.slf4j.simpleLogger.showLogName=false',
43-
'-Dorg.slf4j.simpleLogger.showThreadName=false'
44-
]
38+
mainClassName = 'Main'
4539

4640
sourceSets {
4741
funTest {
@@ -52,6 +46,10 @@ sourceSets {
5246

5347
repositories {
5448
jcenter()
49+
50+
maven {
51+
url 'http://dl.bintray.com/nfrankel/maven'
52+
}
5553
}
5654

5755
dependencies {
@@ -61,10 +59,9 @@ dependencies {
6159
compile 'com.github.salomonbrys.kotson:kotson:2.5.0'
6260
compile 'com.vdurmont:semver4j:2.0.2'
6361

64-
// Use slf4j-simple as logger for kotlin-logging, as the latter is a wrapper for slf4j and depends on an
65-
// implementation of slf4j.
66-
compile 'io.github.microutils:kotlin-logging:1.4.5'
67-
compile 'org.slf4j:slf4j-simple:1.7.25'
62+
// Use logback-classic as the logger for slf4k as it allows to change the log level at runtime.
63+
compile 'ch.frankel.log4k:slf4k-api:1.0.0'
64+
compile 'ch.qos.logback:logback-classic:1.2.3'
6865

6966
funTestCompile sourceSets.main.output
7067
funTestCompile sourceSets.test.output

src/main/kotlin/Main.kt

+11-3
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ import java.nio.file.attribute.BasicFileAttributes
1313

1414
import kotlin.system.exitProcess
1515

16-
import mu.KotlinLogging
17-
18-
internal val log = KotlinLogging.logger {}
16+
@Suppress("UnsafeCast")
17+
internal val log = org.slf4j.LoggerFactory.getLogger({}.javaClass) as ch.qos.logback.classic.Logger
1918

2019
/**
2120
* The main entry point of the application.
2221
*/
2322
object Main {
23+
init {
24+
// Change the default log level from DEBUG to INFO before any other constructors are run.
25+
log.level = ch.qos.logback.classic.Level.INFO
26+
}
27+
2428
class PackageManagerListConverter : IStringConverter<List<PackageManager>> {
2529
override fun convert(managers: String): List<PackageManager> {
2630
// Map lower-cased package manager class names to their instances.
@@ -57,6 +61,10 @@ object Main {
5761
jc.parse(*args)
5862
jc.programName = "pran"
5963

64+
if (debug) {
65+
log.level = ch.qos.logback.classic.Level.DEBUG
66+
}
67+
6068
if (help) {
6169
jc.usage()
6270
exitProcess(1)

src/main/kotlin/Utils.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ProcessCapture(workingDir: File?, vararg command: String) {
4444
private val process = builder.start()
4545

4646
init {
47-
if (Main.debug) {
47+
if (log.isDebugEnabled) {
4848
log.debug("Keeping temporary files:")
4949
log.debug(stdoutFile.absolutePath)
5050
log.debug(stderrFile.absolutePath)

src/main/kotlin/managers/NPM.kt

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.here.provenanceanalyzer.managers
22

3+
import ch.frankel.slf4k.*
4+
35
import com.github.salomonbrys.kotson.contains
46
import com.github.salomonbrys.kotson.forEach
57
import com.github.salomonbrys.kotson.fromJson
@@ -11,7 +13,6 @@ import com.google.gson.Gson
1113
import com.google.gson.JsonObject
1214

1315
import com.here.provenanceanalyzer.model.Dependency
14-
import com.here.provenanceanalyzer.Main
1516
import com.here.provenanceanalyzer.OS
1617
import com.here.provenanceanalyzer.PackageManager
1718
import com.here.provenanceanalyzer.ProcessCapture
@@ -98,9 +99,7 @@ object NPM : PackageManager(
9899
* dependency tree.
99100
*/
100101
fun installDependencies(workingDir: File, managerCommand: String): Dependency {
101-
if (Main.debug) {
102-
log.debug("Using '$managerCommand' to install ${javaClass.simpleName} dependencies.")
103-
}
102+
log.debug { "Using '$managerCommand' to install ${javaClass.simpleName} dependencies." }
104103

105104
// Install all NPM dependencies to enable NPM to list dependencies.
106105
val install = ProcessCapture(workingDir, managerCommand, "install")
@@ -124,9 +123,7 @@ object NPM : PackageManager(
124123
val modulesDir = File(workingDir, "node_modules")
125124

126125
// Collect first-order production dependencies and their transitive production dependencies.
127-
if (Main.debug) {
128-
log.debug("Using '$npm' to list production dependencies.")
129-
}
126+
log.debug { "Using '$npm' to list production dependencies." }
130127

131128
// Note that listing dependencies fails if peer dependencies are missing.
132129
@Suppress("UnsafeCast")
@@ -138,9 +135,7 @@ object NPM : PackageManager(
138135
}
139136

140137
// Collect first-order development dependencies and their transitive production dependencies.
141-
if (Main.debug) {
142-
log.debug("Using '$npm' to list development dependencies.")
143-
}
138+
log.debug { "Using '$npm' to list development dependencies." }
144139

145140
// Note that listing dependencies fails if peer dependencies are missing.
146141
@Suppress("UnsafeCast")

0 commit comments

Comments
 (0)