Skip to content

Commit b124f9b

Browse files
committed
refactor: update logging and dependency injection configuration
- Move logging writers to core dependency injection module - Remove unused logging module and environment parameters from initialization - Integrate log writer registration into crash reporting lifecycle - Update exception handling and stack trace formatting in log writers - Simplify application entry point by removing environment-specific configuration
1 parent 7a7c52e commit b124f9b

File tree

7 files changed

+19
-10
lines changed

7 files changed

+19
-10
lines changed

composeApp/src/jvmMain/kotlin/com/meet/dev/analyzer/di/CoreModule.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import androidx.datastore.preferences.core.PreferenceDataStoreFactory
55
import androidx.datastore.preferences.core.Preferences
66
import com.meet.dev.analyzer.data.datastore.AppPreferenceManager
77
import com.meet.dev.analyzer.data.datastore.PathPreferenceManger
8+
import com.meet.dev.analyzer.utility.crash_report.FileLogWriter
9+
import com.meet.dev.analyzer.utility.crash_report.SentryLogWriter
810
import org.koin.core.module.dsl.singleOf
911
import org.koin.dsl.module
1012
import java.io.File
@@ -22,4 +24,7 @@ val coreModule = module {
2224
singleOf(::AppPreferenceManager)
2325
singleOf(::PathPreferenceManger)
2426

27+
// logging
28+
single<SentryLogWriter> { SentryLogWriter() }
29+
single<FileLogWriter> { FileLogWriter() }
2530
}

composeApp/src/jvmMain/kotlin/com/meet/dev/analyzer/di/LoggingModule.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.meet.dev.analyzer.utility.platform.AppEnvironment
1010
import org.koin.core.module.Module
1111
import org.koin.dsl.module
1212

13+
// this method not use. it not need it SentryLogWriter and FileLogWriter move core module, and logger object is not need it to koin
1314
fun getLoggingModule(
1415
appEnvironment: AppEnvironment,
1516
): Module =

composeApp/src/jvmMain/kotlin/com/meet/dev/analyzer/di/initKoin.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
package com.meet.dev.analyzer.di
22

3-
import com.meet.dev.analyzer.utility.platform.AppEnvironment
43
import org.koin.core.context.startKoin
54
import org.koin.dsl.KoinAppDeclaration
65

76
fun initKoin(
8-
appEnvironment: AppEnvironment,
97
appDeclaration: KoinAppDeclaration = {}
108
) =
119
startKoin {
1210
appDeclaration()
1311
modules(
1412
coreModule,
15-
getLoggingModule(
16-
appEnvironment = appEnvironment,
17-
),
1813
repositoryModule,
1914
viewModule
2015
)

composeApp/src/jvmMain/kotlin/com/meet/dev/analyzer/main.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ import java.awt.Toolkit
3535
fun main() {
3636
val properties = CustomProperties.loadProperties()
3737
val appConfig = CustomProperties.createAppConfig(properties)
38-
initKoin(
39-
appEnvironment = appConfig.appEnvironment,
40-
) {}
38+
initKoin()
4139
FileKit.init(appId = "DevAnalyzer")
4240
System.setProperty("apple.awt.application.appearance", "system")
4341
application {

composeApp/src/jvmMain/kotlin/com/meet/dev/analyzer/utility/crash_report/FileLogWriter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class FileLogWriter(
5151

5252
override fun log(severity: Severity, message: String, tag: String, throwable: Throwable?) {
5353
if (severity < minSeverity) return
54-
val stack = throwable?.stackTraceToString() ?: return
54+
val stack = throwable?.stackTraceToString() ?: ""
5555

5656
val file = getOrCreateLogFile()
5757

composeApp/src/jvmMain/kotlin/com/meet/dev/analyzer/utility/crash_report/Sentry.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package com.meet.dev.analyzer.utility.crash_report
22

3+
import co.touchlab.kermit.Logger
34
import io.sentry.Sentry
45
import io.sentry.SentryLevel
6+
import org.koin.mp.KoinPlatform.getKoin
57

68
fun initSentry(
79
dns: String,
810
version: String,
911
) {
12+
val sentryLogWriter = getKoin().get<SentryLogWriter>()
13+
if (Logger.config.logWriterList.none { it is FileLogWriter }) {
14+
Logger.addLogWriter(sentryLogWriter)
15+
}
1016
Sentry.init { options ->
1117
options.dsn = dns
1218

@@ -20,6 +26,10 @@ fun initSentry(
2026

2127
fun disableSentry() {
2228
Sentry.close()
29+
val sentryLogWriter = getKoin().get<SentryLogWriter>()
30+
Logger.setLogWriters(
31+
Logger.config.logWriterList.filterNot { w -> w == sentryLogWriter }
32+
)
2333
}
2434

2535

composeApp/src/jvmMain/kotlin/com/meet/dev/analyzer/utility/crash_report/SentryLogWriter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class SentryLogWriter(
4848

4949
if (shouldCaptureException) {
5050
// Avoid duplicate Sentry events for throwable logs.
51-
throwable?.let { captureException(it) }
51+
captureException(throwable)
5252
return
5353
}
5454

0 commit comments

Comments
 (0)