Skip to content

Commit

Permalink
Added output formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
diareuse committed Apr 15, 2022
1 parent 2086f5f commit f257758
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/src/main/java/taggy/composition/LogModule.kt
Original file line number Diff line number Diff line change
@@ -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

}
13 changes: 13 additions & 0 deletions app/src/main/java/taggy/composition/LogModuleDefault.kt
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import java.util.logging.Logger

class LoggerColored(
handler: Handler
) : Logger("taggy.logger.colored", null) {
) : Logger("taggy", null) {

init {
addHandler(handler)
Expand Down
12 changes: 12 additions & 0 deletions feature-logging/src/main/java/taggy/logging/SimpleFormatter.kt
Original file line number Diff line number Diff line change
@@ -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()
}

}
26 changes: 26 additions & 0 deletions feature-logging/src/test/java/taggy/logging/SimpleFormatterTest.kt
Original file line number Diff line number Diff line change
@@ -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))
}
}

}

0 comments on commit f257758

Please sign in to comment.