From 09bbd9ea82555416cab1d8d4c2ff54f9f1b904d7 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sat, 28 Jan 2023 18:57:48 +0530 Subject: feat: wire up SLF4J integration --- .../passwordstore/util/log/LogcatLoggerFactory.kt | 17 ++++++++++++++++ .../main/java/org/slf4j/impl/StaticLoggerBinder.kt | 23 ++++++++++++++++++++++ .../main/java/org/slf4j/impl/StaticMDCBinder.kt | 21 ++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 app/src/main/java/app/passwordstore/util/log/LogcatLoggerFactory.kt create mode 100644 app/src/main/java/org/slf4j/impl/StaticLoggerBinder.kt create mode 100644 app/src/main/java/org/slf4j/impl/StaticMDCBinder.kt (limited to 'app/src/main') diff --git a/app/src/main/java/app/passwordstore/util/log/LogcatLoggerFactory.kt b/app/src/main/java/app/passwordstore/util/log/LogcatLoggerFactory.kt new file mode 100644 index 00000000..92464446 --- /dev/null +++ b/app/src/main/java/app/passwordstore/util/log/LogcatLoggerFactory.kt @@ -0,0 +1,17 @@ +package app.passwordstore.util.log + +import java.util.concurrent.ConcurrentHashMap +import org.slf4j.ILoggerFactory +import org.slf4j.Logger + +/** + * [ILoggerFactory] implementation that passes out instances of [LogcatLogger], maintaining an + * internal cache of [Logger]s to avoid duplicate initialization. + */ +class LogcatLoggerFactory : ILoggerFactory { + + private val loggers = ConcurrentHashMap() + override fun getLogger(name: String): Logger { + return loggers.getOrPut(name) { LogcatLogger(name) } + } +} diff --git a/app/src/main/java/org/slf4j/impl/StaticLoggerBinder.kt b/app/src/main/java/org/slf4j/impl/StaticLoggerBinder.kt new file mode 100644 index 00000000..85ab4d42 --- /dev/null +++ b/app/src/main/java/org/slf4j/impl/StaticLoggerBinder.kt @@ -0,0 +1,23 @@ +@file:Suppress("Unused") + +package org.slf4j.impl + +import app.passwordstore.util.log.LogcatLoggerFactory +import org.slf4j.ILoggerFactory +import org.slf4j.spi.LoggerFactoryBinder + +class StaticLoggerBinder : LoggerFactoryBinder { + private val loggerFactory: ILoggerFactory = LogcatLoggerFactory() + private val loggerFactoryClassStr = LogcatLoggerFactory::javaClass.name + + override fun getLoggerFactory(): ILoggerFactory { + return loggerFactory + } + override fun getLoggerFactoryClassStr(): String { + return loggerFactoryClassStr + } + + companion object { + @JvmStatic val singleton = StaticLoggerBinder() + } +} diff --git a/app/src/main/java/org/slf4j/impl/StaticMDCBinder.kt b/app/src/main/java/org/slf4j/impl/StaticMDCBinder.kt new file mode 100644 index 00000000..d75c8ed0 --- /dev/null +++ b/app/src/main/java/org/slf4j/impl/StaticMDCBinder.kt @@ -0,0 +1,21 @@ +@file:Suppress("Unused") + +package org.slf4j.impl + +import org.slf4j.helpers.BasicMDCAdapter +import org.slf4j.spi.MDCAdapter + +class StaticMDCBinder { + + fun getMDCA(): MDCAdapter { + return BasicMDCAdapter() + } + + fun getMDCAdapterClassStr(): String? { + return BasicMDCAdapter::class.java.name + } + + companion object { + @JvmStatic val singleton = StaticMDCBinder() + } +} -- cgit v1.2.3