commonMain.kr.jadekim.logger.pipeline.LoggerNameShorter.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of j-logger-jvm Show documentation
Show all versions of j-logger-jvm Show documentation
Logging Library for Kotlin
package kr.jadekim.logger.pipeline
import kr.jadekim.logger.Log
import kr.jadekim.logger.LogData
open class LoggerNameShorter(val preferLength: Int = 32) : JLogPipe {
companion object Key : JLogPipe.Key
override val key = Key
override fun install(pipeline: MutableList, index: Int) {
if (pipeline.any { LoggerNameShorter::class.isInstance(it) }) {
return
}
super.install(pipeline, index)
}
override fun handle(log: Log): Log {
if (log is LogData) {
return log.copy(loggerName = transform(log.loggerName))
}
return log
}
protected fun transform(name: String): String {
val result = mutableListOf()
val tokens = name.split(".")
if (tokens.size < 2) {
return name
}
var diff = name.length - preferLength
var idx = 0
while (diff > 0 && tokens.size - 1 > idx) {
val token = tokens[idx]
result.add(token[0].toString())
diff -= token.length - 1
idx++
}
while (tokens.size > idx) {
result.add(tokens[idx])
idx++
}
return result.joinToString(".")
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy