You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.6 KiB
Kotlin

package me.eater.hefbrug.logging
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.core.LoggerContext
import org.apache.logging.log4j.core.appender.ConsoleAppender
import org.apache.logging.log4j.core.config.Configuration
import org.apache.logging.log4j.core.config.ConfigurationFactory
import org.apache.logging.log4j.core.config.ConfigurationSource
import org.apache.logging.log4j.core.config.Order
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory
import org.apache.logging.log4j.core.config.plugins.Plugin
import java.net.URI
@Plugin(name = "LoggerConfig", category = ConfigurationFactory.CATEGORY)
@Order(50)
class LoggerConfig : ConfigurationFactory() {
fun getConfiguration(): Configuration {
val builder = ConfigurationBuilderFactory.newConfigurationBuilder()
builder.add(
builder.newCustomLevel("SCRIPT", 700)
)
builder.setConfigurationName("HefbrugDefault")
builder.setStatusLevel(Level.INFO)
builder.add(
builder.newAppender("Stdout", "CONSOLE")
.addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT)
.add(
builder.newLayout("PatternLayout")
.addAttribute(
"pattern",
"%blue{[%d{HH:mm:ss}]}%highlight{[%-5level]}{SCRIPT=white} %msg{ansi}%n"
)
)
)
builder.add(
builder.newRootLogger(
when (System.getenv("HEFBRUG_LOG")?.toLowerCase()) {
"all" -> Level.ALL
"trace" -> Level.TRACE
"debug" -> Level.DEBUG
"warn" -> Level.WARN
"error" -> Level.ERROR
"fatal" -> Level.FATAL
"script" -> Level.forName("SCRIPT", 700)
"off" -> Level.OFF
else -> Level.INFO
}
)
.add(builder.newAppenderRef("Stdout"))
)
return builder.build()
}
override fun getConfiguration(loggerContext: LoggerContext, source: ConfigurationSource): Configuration =
getConfiguration()
override fun getConfiguration(loggerContext: LoggerContext, name: String, configLocation: URI?): Configuration =
getConfiguration()
override fun getConfiguration(
loggerContext: LoggerContext,
name: String,
configLocation: URI?,
loader: ClassLoader?
) = getConfiguration()
override fun getSupportedTypes(): Array<String> = arrayOf("*")
}