Skip to content

Commit 3690293

Browse files
committed
Added log4j support
1 parent 8abc256 commit 3690293

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ repositories {
2020
}
2121
}
2222

23+
val log4jVersion = "2.12.1"
2324

2425
dependencies {
2526
kapt("info.picocli:picocli-codegen:4.6.1")
@@ -30,6 +31,9 @@ dependencies {
3031
implementation ("com.github.paulorb:modbus-kt:1.0.12")
3132
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2")
3233
implementation("org.apache.commons:commons-csv:1.10.0")
34+
implementation("org.apache.logging.log4j:log4j-core:$log4jVersion")
35+
implementation("org.apache.logging.log4j:log4j-api:$log4jVersion")
36+
implementation("org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion")
3337
testImplementation(kotlin("test"))
3438
}
3539

src/main/kotlin/Main.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import picocli.CommandLine.Model.CommandSpec
66
import java.util.*
77
import java.util.concurrent.Callable
88
import kotlin.system.exitProcess
9-
9+
import org.apache.logging.log4j.core.config.Configurator
10+
import org.slf4j.LoggerFactory
1011

1112
data class EnvParameter(
1213
var symbol : String,
@@ -50,6 +51,10 @@ class Checksum : Callable<Int> {
5051
private lateinit var modbusServer: ModbusServer
5152
private lateinit var environmentParameters: List<EnvParameter>
5253

54+
companion object {
55+
val logger = LoggerFactory.getLogger("main")
56+
}
57+
5358
private fun processEnvironmentParameters(parameters: MutableList<String?>?): List<EnvParameter> {
5459
val envParameter = mutableListOf<EnvParameter>()
5560
parameters?.forEach { param ->
@@ -66,17 +71,18 @@ class Checksum : Callable<Int> {
6671
}
6772

6873
override fun call(): Int {
74+
Configurator.initialize(null, "log4j2.xml")
6975
val mainCoroutineScope = CoroutineScope(Dispatchers.Default)
7076
val configuration = ConfigurationParser()
7177

7278
if(simulationRandomValues && file.isNotEmpty()){
73-
println("-f and -sr cannot be mixed, one of the simulations must be chosen")
79+
logger.error("-f and -sr cannot be mixed, one of the simulations must be chosen")
7480
return -1
7581
}
7682

7783
environmentParameters = processEnvironmentParameters(parameters)
7884
if(environmentParameters.isNotEmpty()){
79-
println("environment parameters: ${environmentParameters.toString()}")
85+
logger.warn("environment parameters: ${environmentParameters.toString()}")
8086
}
8187

8288
//val fileContents = Files.readAllBytes(file.toPath())

src/main/resources/log4j2.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Extra logging related to initialization of Log4j.
3+
Set to debug or trace if log4j initialization is failing. -->
4+
<Configuration status="warn">
5+
<Appenders>
6+
<!-- Console appender configuration -->
7+
<Console name="console" target="SYSTEM_OUT">
8+
<PatternLayout
9+
pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
10+
</Console>
11+
</Appenders>
12+
<Loggers>
13+
<!-- Root logger referring to console appender -->
14+
<Root level="info" additivity="false">
15+
<AppenderRef ref="console" />
16+
</Root>
17+
</Loggers>
18+
</Configuration>

0 commit comments

Comments
 (0)