From 2c25bcec8135912c19b92ea9c74426a833f3b168 Mon Sep 17 00:00:00 2001 From: Syer10 Date: Fri, 9 Jul 2021 16:34:52 -0400 Subject: [PATCH] Programmatically initialize logger, with a new file logger --- .../ca/gosyer/core/logging/LoggingSetup.kt | 76 +++++++++++++++++++ src/main/kotlin/ca/gosyer/ui/main/main.kt | 11 +-- src/main/resources/log4j2.xml | 18 ----- 3 files changed, 80 insertions(+), 25 deletions(-) create mode 100644 src/main/kotlin/ca/gosyer/core/logging/LoggingSetup.kt delete mode 100644 src/main/resources/log4j2.xml diff --git a/src/main/kotlin/ca/gosyer/core/logging/LoggingSetup.kt b/src/main/kotlin/ca/gosyer/core/logging/LoggingSetup.kt new file mode 100644 index 00000000..cbc48751 --- /dev/null +++ b/src/main/kotlin/ca/gosyer/core/logging/LoggingSetup.kt @@ -0,0 +1,76 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package ca.gosyer.core.logging + +import org.apache.logging.log4j.Level +import org.apache.logging.log4j.LogManager +import org.apache.logging.log4j.core.LoggerContext +import org.apache.logging.log4j.core.appender.ConsoleAppender +import org.apache.logging.log4j.core.config.builder.api.ComponentBuilder +import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory +import java.io.File + +const val consolePattern = + "%highlight{%d{" + '$' + "{LOG_DATEFORMAT_PATTERN:-HH:mm:ss.SSS}} [%t] " + '$' + "{LOG_LEVEL_PATTERN:-%p}/%c{1}: %m%n" + '$' + "{LOG_EXCEPTION_CONVERSION_WORD:-%xEx}}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=black, TRACE=black}" +const val filePattern = + "%d{" + '$' + "{LOG_DATEFORMAT_PATTERN:-HH:mm:ss.SSS}} [%t] " + '$' + "{LOG_LEVEL_PATTERN:-%p}/%c{1}: %m%n" + '$' + "{LOG_EXCEPTION_CONVERSION_WORD:-%xEx}" + +@Suppress("UPPER_BOUND_VIOLATED_WARNING") +fun initializeLogger(loggingLocation: File) { + val ctx = LogManager.getContext(false) as LoggerContext + val builder = ConfigurationBuilderFactory.newConfigurationBuilder() + .apply { + setStatusLevel(Level.WARN) + setConfigurationName("LoggerBuilder") + add( + newAppender("Console", "Console") + .addAttribute( + "target", + ConsoleAppender.Target.SYSTEM_OUT + ) + .add( + newLayout("PatternLayout") + .addAttribute("disableAnsi", "false") + .addAttribute("pattern", consolePattern) + ) + ) + add( + newAppender("Rolling", "RollingFile") + .addAttribute( + "fileName", + loggingLocation.absolutePath.trimEnd { it == '/' || it == '\\' } + "/rolling.log" + ) + .addAttribute( + "filePattern", + loggingLocation.absolutePath.trimEnd { it == '/' || it == '\\' } + "/archive/rolling-%d{MM-dd-yy}.log.gz" + ) + .add( + newLayout("PatternLayout") + .addAttribute("pattern", filePattern) + ) + .addComponent( + newComponent>("Policies") + .addComponent( + newComponent>("CronTriggeringPolicy") + .addAttribute("schedule", "0 0 0 * * ?") + .addAttribute("evaluateOnStartup", "true") + ) + .addComponent( + newComponent>("SizeBasedTriggeringPolicy") + .addAttribute("size", "100M") + ) + ) + ) + add( + newRootLogger(Level.DEBUG) + .add(newAppenderRef("Console")) + .add(newAppenderRef("Rolling")) + ) + } + ctx.configuration = builder.build() + ctx.updateLoggers() +} diff --git a/src/main/kotlin/ca/gosyer/ui/main/main.kt b/src/main/kotlin/ca/gosyer/ui/main/main.kt index 784a8230..1b5c555e 100644 --- a/src/main/kotlin/ca/gosyer/ui/main/main.kt +++ b/src/main/kotlin/ca/gosyer/ui/main/main.kt @@ -13,6 +13,7 @@ import androidx.compose.runtime.getValue import androidx.compose.ui.input.key.Key import androidx.compose.ui.unit.IntOffset import ca.gosyer.BuildConfig +import ca.gosyer.core.logging.initializeLogger import ca.gosyer.data.DataModule import ca.gosyer.data.server.ServerService import ca.gosyer.data.server.ServerService.ServerResult @@ -27,6 +28,7 @@ import ca.gosyer.ui.base.theme.AppTheme import ca.gosyer.util.lang.launchUI import ca.gosyer.util.lang.withUIContext import ca.gosyer.util.system.getAsFlow +import ca.gosyer.util.system.userDataDir import com.github.weisj.darklaf.LafManager import com.github.weisj.darklaf.theme.DarculaTheme import com.github.weisj.darklaf.theme.IntelliJTheme @@ -36,20 +38,15 @@ import com.github.zsoltk.compose.savedinstancestate.Bundle import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.flow.launchIn -import org.apache.logging.log4j.core.config.Configurator import toothpick.configuration.Configuration import toothpick.ktp.KTP import toothpick.ktp.extension.getInstance +import java.io.File import java.util.logging.Level @OptIn(DelicateCoroutinesApi::class) fun main() { - val clazz = MainViewModel::class.java - Configurator.initialize( - null, - clazz.classLoader, - clazz.getResource("log4j2.xml")?.toURI() - ) + initializeLogger(File(userDataDir, "logging")) if (BuildConfig.DEBUG) { System.setProperty("kotlinx.coroutines.debug", "on") diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml deleted file mode 100644 index 95249865..00000000 --- a/src/main/resources/log4j2.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - -