From f257758c63e46fa8cceb0e2a60375a680034a4db Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Fri, 15 Apr 2022 19:35:28 +0200 Subject: [PATCH] Added output formatter --- .../main/java/taggy/composition/LogModule.kt | 4 +++ .../taggy/composition/LogModuleDefault.kt | 13 ++++++++++ .../main/java/taggy/logging/LoggerColored.kt | 2 +- .../java/taggy/logging/SimpleFormatter.kt | 12 +++++++++ .../java/taggy/logging/SimpleFormatterTest.kt | 26 +++++++++++++++++++ 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 feature-logging/src/main/java/taggy/logging/SimpleFormatter.kt create mode 100644 feature-logging/src/test/java/taggy/logging/SimpleFormatterTest.kt diff --git a/app/src/main/java/taggy/composition/LogModule.kt b/app/src/main/java/taggy/composition/LogModule.kt index 4b7b466..d178ce2 100644 --- a/app/src/main/java/taggy/composition/LogModule.kt +++ b/app/src/main/java/taggy/composition/LogModule.kt @@ -1,9 +1,13 @@ package taggy.composition +import java.util.logging.Formatter +import java.util.logging.Handler import java.util.logging.Logger interface LogModule { fun getLogger(): Logger + fun getHandler(): Handler + fun getFormatter(): Formatter } \ No newline at end of file diff --git a/app/src/main/java/taggy/composition/LogModuleDefault.kt b/app/src/main/java/taggy/composition/LogModuleDefault.kt index b3e4323..519f23c 100644 --- a/app/src/main/java/taggy/composition/LogModuleDefault.kt +++ b/app/src/main/java/taggy/composition/LogModuleDefault.kt @@ -1,7 +1,10 @@ package taggy.composition import taggy.logging.LoggerColored +import taggy.logging.SimpleFormatter import java.util.logging.ConsoleHandler +import java.util.logging.Formatter +import java.util.logging.Handler import java.util.logging.Logger class LogModuleDefault : LogModule { @@ -10,4 +13,14 @@ class LogModuleDefault : LogModule { return LoggerColored(ConsoleHandler()) } + override fun getHandler(): Handler { + return ConsoleHandler().also { + it.formatter = getFormatter() + } + } + + override fun getFormatter(): Formatter { + return SimpleFormatter() + } + } \ No newline at end of file diff --git a/feature-logging/src/main/java/taggy/logging/LoggerColored.kt b/feature-logging/src/main/java/taggy/logging/LoggerColored.kt index 5dc5805..fa0a384 100644 --- a/feature-logging/src/main/java/taggy/logging/LoggerColored.kt +++ b/feature-logging/src/main/java/taggy/logging/LoggerColored.kt @@ -7,7 +7,7 @@ import java.util.logging.Logger class LoggerColored( handler: Handler -) : Logger("taggy.logger.colored", null) { +) : Logger("taggy", null) { init { addHandler(handler) diff --git a/feature-logging/src/main/java/taggy/logging/SimpleFormatter.kt b/feature-logging/src/main/java/taggy/logging/SimpleFormatter.kt new file mode 100644 index 0000000..dc89891 --- /dev/null +++ b/feature-logging/src/main/java/taggy/logging/SimpleFormatter.kt @@ -0,0 +1,12 @@ +package taggy.logging + +import java.util.logging.Formatter +import java.util.logging.LogRecord + +class SimpleFormatter : Formatter() { + + override fun format(record: LogRecord): String { + return record.message.orEmpty() + } + +} \ No newline at end of file diff --git a/feature-logging/src/test/java/taggy/logging/SimpleFormatterTest.kt b/feature-logging/src/test/java/taggy/logging/SimpleFormatterTest.kt new file mode 100644 index 0000000..6f037a4 --- /dev/null +++ b/feature-logging/src/test/java/taggy/logging/SimpleFormatterTest.kt @@ -0,0 +1,26 @@ +package taggy.logging + +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import java.util.logging.Level +import java.util.logging.LogRecord +import kotlin.test.expect + +internal class SimpleFormatterTest { + + private lateinit var formatter: SimpleFormatter + + @BeforeEach + fun setUp() { + formatter = SimpleFormatter() + } + + @Test + fun format() { + val message = "hello hello!" + expect(message) { + formatter.format(LogRecord(Level.ALL, message)) + } + } + +} \ No newline at end of file