Programmatically initialize logger, with a new file logger

This commit is contained in:
Syer10
2021-07-09 16:34:52 -04:00
parent f6a6f333c5
commit 2c25bcec81
3 changed files with 80 additions and 25 deletions

View File

@@ -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<ComponentBuilder<*>>("Policies")
.addComponent(
newComponent<ComponentBuilder<*>>("CronTriggeringPolicy")
.addAttribute("schedule", "0 0 0 * * ?")
.addAttribute("evaluateOnStartup", "true")
)
.addComponent(
newComponent<ComponentBuilder<*>>("SizeBasedTriggeringPolicy")
.addAttribute("size", "100M")
)
)
)
add(
newRootLogger(Level.DEBUG)
.add(newAppenderRef("Console"))
.add(newAppenderRef("Rolling"))
)
}
ctx.configuration = builder.build()
ctx.updateLoggers()
}

View File

@@ -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")

View File

@@ -1,18 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://logging.apache.org/log4j/2.0/config"
xsi:noNamespaceSchemaLocation="Log4j-config.xsd">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout disableAnsi="false" pattern="%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}" />
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>