Skip to content

Commit baddc08

Browse files
committed
Improve logging configuration
1 parent 87d9476 commit baddc08

File tree

11 files changed

+89
-103
lines changed

11 files changed

+89
-103
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
- name: Setup Gradle
4141
uses: gradle/actions/setup-gradle@v4
4242
- name: Package app with Gradle
43-
run: ./gradlew jpackage -Pversion="${{ github.ref_name }}" -PappLoggingStdout=false
43+
run: ./gradlew jpackage -Pversion="${{ github.ref_name }}"
4444
- name: Upload Asset
4545
shell: bash
4646
run: |

build.gradle

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@ import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
22
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
33

44
buildscript {
5-
ext.kotlinVersion = '2.2.0'
6-
repositories {
7-
mavenCentral()
8-
}
5+
ext.kotlinVersion = "2.2.0"
96
}
107

118
plugins {
12-
id 'java'
13-
id 'application'
14-
id 'org.jetbrains.kotlin.jvm' version "$kotlinVersion"
15-
id 'org.openjfx.javafxplugin' version '0.1.0'
9+
id "java"
10+
id "application"
11+
id "org.jetbrains.kotlin.jvm" version "$kotlinVersion"
12+
id "org.openjfx.javafxplugin" version "0.1.0"
1613
id "org.jetbrains.dokka" version "2.0.0"
1714
}
1815

19-
group 'ru.nucodelabs'
16+
group "ru.nucodelabs"
2017

2118
java {
2219
sourceCompatibility = JavaVersion.VERSION_21
@@ -34,18 +31,18 @@ ext {
3431
// For deps
3532
def osName = currentOS.isWindows() ? "windows" : currentOS.isMacOsX() ? "macosx" : "linux"
3633
def archName = currentArch.isArm64() && !currentOS.isWindows() ? "arm64" : "x86_64"
37-
set 'platform', "$osName-$archName"
34+
set "platform", "$osName-$archName"
3835

3936
println "OS: ${currentOS.name}"
4037
println "OS VERSION: ${System.getProperty("os.version")}"
4138
println "ARCHITECTURE: ${currentArch.name}"
4239
println "CLASSIFIER: ${platform}"
4340

44-
set 'runDir', "$projectDir/.run"
41+
set "runDir", "$projectDir/.run"
4542

4643
/* Dependency versions */
47-
set 'logbackVersion', "1.5.18"
48-
set 'guiceVersion', '7.0.0'
44+
set "logbackVersion", "1.5.18"
45+
set "guiceVersion", "7.0.0"
4946
}
5047

5148
processResources {
@@ -72,25 +69,25 @@ compileTestKotlin {
7269
}
7370

7471
tasks.withType(JavaCompile).configureEach {
75-
options.encoding = 'UTF-8'
72+
options.encoding = "UTF-8"
7673
}
7774

7875
application {
79-
mainClass = 'ru.nucodelabs.gem.app.StartGemApplication'
76+
mainClass = "ru.nucodelabs.gem.app.StartGemApplication"
8077
}
8178

8279
javafx {
83-
version = '23'
84-
modules = ['javafx.controls', 'javafx.fxml', 'javafx.graphics', 'javafx.swing']
80+
version = "23"
81+
modules = ["javafx.controls", "javafx.fxml", "javafx.graphics", "javafx.swing"]
8582
}
8683

8784
dependencies {
8885
/* Versions */
89-
final junitVersion = '5.9.2'
90-
final jacksonVersion = '2.19.2'
86+
final junitVersion = "5.9.2"
87+
final jacksonVersion = "2.19.2"
9188
final kotlinTestVersion = kotlinVersion
92-
final mapstructVersion = '1.6.3'
93-
final slf4jVersion = '2.0.17'
89+
final mapstructVersion = "1.6.3"
90+
final slf4jVersion = "2.0.17"
9491

9592
implementation project(":kfx-utils")
9693
implementation project(":common-utils")
@@ -103,32 +100,30 @@ dependencies {
103100
"$projectDir/lib/${System.mapLibraryName("MathVES")}"
104101
)
105102

106-
implementation "org.slf4j:slf4j-api:${slf4jVersion}"
103+
implementation("org.slf4j:slf4j-api:${slf4jVersion}")
107104
implementation("ch.qos.logback:logback-classic:$logbackVersion")
108105
implementation project(":app-logback-appender")
109-
implementation 'org.codehaus.janino:janino:3.1.12'
110-
// implementation "org.slf4j:slf4j-jdk14:${slf4jVersion}"
111106

112107
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
113-
implementation 'org.hibernate.validator:hibernate-validator:8.0.0.Final'
108+
implementation "org.hibernate.validator:hibernate-validator:8.0.0.Final"
114109

115110
implementation "org.mapstruct:mapstruct:$mapstructVersion"
116111
annotationProcessor "org.mapstruct:mapstruct-processor:$mapstructVersion"
117112

118113
/* FXML script tag Groovy support */
119-
implementation 'org.codehaus.groovy:groovy-jsr223:3.0.25'
114+
implementation "org.codehaus.groovy:groovy-jsr223:3.0.25"
120115

121-
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: jacksonVersion
122-
implementation group: 'org.apache.commons', name: 'commons-math3', version: '3.6.1'
116+
implementation group: "com.fasterxml.jackson.core", name: "jackson-databind", version: jacksonVersion
117+
implementation group: "org.apache.commons", name: "commons-math3", version: "3.6.1"
123118

124-
implementation group: 'com.google.inject', name: 'guice', version: guiceVersion
125-
implementation group: 'org.tinfour', name: 'TinfourCore', version: '2.1.8'
119+
implementation group: "com.google.inject", name: "guice", version: guiceVersion
120+
implementation group: "org.tinfour", name: "TinfourCore", version: "2.1.8"
126121
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion"
127-
implementation 'org.apache.commons:commons-collections4:4.4'
122+
implementation "org.apache.commons:commons-collections4:4.4"
128123

129124
/* Math Libraries */
130-
implementation 'org.glassfish.expressly:expressly:5.0.0'
131-
implementation 'com.github.haifengl:smile-core:4.4.0'
125+
implementation "org.glassfish.expressly:expressly:5.0.0"
126+
implementation "com.github.haifengl:smile-core:4.4.0"
132127
implementation "org.bytedeco:openblas:0.3.30-1.5.12:$platform"
133128
implementation "org.bytedeco:javacpp:1.5.12:$platform"
134129
implementation "org.bytedeco:arpack-ng:3.9.1-1.5.12:$platform"
@@ -144,42 +139,44 @@ dependencies {
144139

145140
/* Setup Runtime */
146141

147-
tasks.register('prepareRunDir') {
142+
tasks.register("prepareRunDir") {
148143
file(runDir).mkdirs()
149144
}
150145

151-
tasks.register('copyLibsToRunDir', Copy) {
146+
tasks.register("copyLibsToRunDir", Copy) {
152147
dependsOn prepareRunDir
153148
from "$projectDir/lib"
154149
into runDir
155150
}
156151

157-
tasks.register('copyDataToRunDir', Copy) {
152+
tasks.register("copyDataToRunDir", Copy) {
158153
dependsOn prepareRunDir
159154
from "$projectDir/data"
160155
into "$runDir/data"
161156
}
162157

163-
tasks.register('copyClrToRunDir', Copy) {
158+
tasks.register("copyClrToRunDir", Copy) {
164159
dependsOn prepareRunDir
165160
from "$projectDir/colormap/default.clr"
166161
into "$runDir/colormap"
167162
}
168163

169-
tasks.register('cleanRunDir', Delete) {
164+
tasks.register("cleanRunDir", Delete) {
170165
delete fileTree(runDir) { exclude "err-trace*.txt" }
171166
}
172167

173168
tasks.withType(JavaExec).configureEach {
174169
dependsOn copyClrToRunDir, copyDataToRunDir, copyLibsToRunDir
175170
jvmArgs = moduleExportsJvmArgs
171+
systemProperty("app.gem.log.stdout", true)
176172
workingDir = file(runDir)
177173
finalizedBy cleanRunDir
178174
}
179175

180176
test {
181177
dependsOn copyClrToRunDir, copyDataToRunDir, copyLibsToRunDir
182178
workingDir = file(runDir)
179+
systemProperty("app.gem.log.stdout", true)
183180
useJUnitPlatform()
184181
finalizedBy cleanRunDir
185182
}
@@ -189,8 +186,10 @@ test {
189186
final jpackageInputDir = layout.buildDirectory.dir("jars").get().asFile.absolutePath
190187

191188
tasks.register("copyNativeDependencies", Copy) {
192-
from file("$projectDir/lib/${System.mapLibraryName("MathVES_JNI")}"),
189+
from(
190+
file("$projectDir/lib/${System.mapLibraryName("MathVES_JNI")}"),
193191
file("$projectDir/lib/${System.mapLibraryName("MathVES")}")
192+
)
194193
into(jpackageInputDir)
195194
}
196195

gradle.properties

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
version=1.0.0-SNAPSHOT
2-
appLoggingLevel=INFO
3-
appLoggingStdout=true
1+
version=1.0.0-SNAPSHOT

settings.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
rootProject.name = "gem"
2-
include 'kfx-utils', 'common-utils'
2+
include 'kfx-utils'
3+
include 'common-utils'
34
include 'files'
45
include 'app-logback-appender'

src/main/java/ru/nucodelabs/gem/app/GemApplication.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,7 @@ class GemApplication : GuiceApplication(AppModule()) {
5555

5656
@Throws(Exception::class)
5757
override fun start(primaryStage: Stage) {
58-
Thread.setDefaultUncaughtExceptionHandler(
59-
UncaughtExceptionHandler(
60-
alertsFactory,
61-
parameters.raw.contains("--disable-uncaught-error-files")
62-
)
63-
)
58+
Thread.setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler(alertsFactory))
6459
val params: List<String> = parameters.raw + macOSHandledFiles
6560
if (
6661
params.any {
Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,20 @@
11
package ru.nucodelabs.gem.app
22

3+
import javafx.application.Platform
34
import ru.nucodelabs.gem.config.slf4j
45
import ru.nucodelabs.gem.view.AlertsFactory
5-
import ru.nucodelabs.util.OS
6-
import java.io.File
76
import java.lang.Thread.UncaughtExceptionHandler
8-
import java.time.LocalDateTime
9-
import java.time.format.DateTimeFormatter
107

118
class UncaughtExceptionHandler(
129
private val alertsFactory: AlertsFactory,
13-
private val writeToFileDisabled: Boolean
1410
) : UncaughtExceptionHandler {
1511

1612
val log = slf4j(this)
1713

1814
override fun uncaughtException(t: Thread, e: Throwable) {
1915
log.error("Uncaught exception", e)
20-
handleUnexpectedException(e)
21-
}
22-
23-
private fun handleUnexpectedException(e: Throwable) {
24-
alertsFactory.uncaughtExceptionAlert(e).show()
25-
if (!writeToFileDisabled) {
26-
val dateNow = LocalDateTime.now()
27-
val formattedDate = dateNow
28-
.format(DateTimeFormatter.ofPattern("dd.MM.yyyy"))
29-
val timestamp = java.time.Instant.now().toEpochMilli()
30-
val traceFile =
31-
File("err-trace_${OS.osNameClassifier}_${formattedDate}_${timestamp}.txt").also {
32-
it.createNewFile()
33-
}
34-
traceFile.writeText(e.stackTraceToString())
16+
if (Platform.isFxApplicationThread()) {
17+
alertsFactory.uncaughtExceptionAlert(e).show()
3518
}
3619
}
3720
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ru.nucodelabs.gem.config
2+
3+
import ch.qos.logback.classic.BasicConfigurator
4+
import ch.qos.logback.classic.Level
5+
import ch.qos.logback.classic.Logger
6+
import ch.qos.logback.classic.LoggerContext
7+
import ch.qos.logback.classic.layout.TTLLLayout
8+
import ch.qos.logback.classic.spi.Configurator
9+
import ch.qos.logback.classic.spi.ILoggingEvent
10+
import ch.qos.logback.core.encoder.LayoutWrappingEncoder
11+
import ch.qos.logback.core.spi.ContextAwareBase
12+
import ru.nucodelabs.logback.appender.AppLogFileAppender
13+
14+
class GemLogbackConfigurator : ContextAwareBase(), Configurator {
15+
16+
override fun configure(loggerContext: LoggerContext?): Configurator.ExecutionStatus? {
17+
if (System.getProperty("app.gem.log.stdout", "false").toBoolean()) {
18+
BasicConfigurator().also { it.context = loggerContext }.configure(loggerContext)
19+
}
20+
21+
val appender = AppLogFileAppender<ILoggingEvent>().apply {
22+
context = loggerContext
23+
name = "APP_FILE"
24+
appName = "ru.nucodelabs.gem"
25+
logFileName = "last_session.log"
26+
isAppend = false
27+
encoder = LayoutWrappingEncoder<ILoggingEvent>().apply {
28+
context = loggerContext
29+
layout = TTLLLayout().apply { context = loggerContext }.also { it.start() }
30+
}.also { it.start() }
31+
}.also { it.start() }
32+
33+
loggerContext?.getLogger(Logger.ROOT_LOGGER_NAME)?.let { root ->
34+
root.isAdditive = false
35+
root.level = Level.valueOf(System.getProperty("app.gem.log.level", "INFO"))
36+
root.addAppender(appender)
37+
}
38+
39+
return Configurator.ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY
40+
}
41+
}

src/main/java/ru/nucodelabs/gem/config/Logging.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package ru.nucodelabs.gem.config
22

33
import org.slf4j.LoggerFactory
44

5-
fun slf4j(instance: Any) = LoggerFactory.getLogger(instance::class.java)!!
5+
fun slf4j(instance: Any) = LoggerFactory.getLogger(instance::class.java)!!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ru.nucodelabs.gem.config.GemLogbackConfigurator

src/main/resources/logback.properties

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)