Skip to content

Commit

Permalink
Merge pull request #1 from paslavsky/0.0.3
Browse files Browse the repository at this point in the history
0.0.3
  • Loading branch information
paslavsky authored Nov 24, 2018
2 parents 5bed427 + 44f9364 commit 1d6a11c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.paslavsky</groupId>
<artifactId>slf4kotlin</artifactId>
<version>0.0.2</version>
<version>0.0.3</version>
<packaging>jar</packaging>

<name>Kotlin Wrapper for slf4j library</name>
Expand Down
43 changes: 43 additions & 0 deletions src/main/kotlin/com/github/paslavsky/Logging.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@ package com.github.paslavsky
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import kotlin.reflect.KClass
import java.util.concurrent.TimeUnit

typealias LogMethod = Logger.(String) -> Unit

private val iError: LogMethod = { error(it) }
private val iWarning: LogMethod = { warn(it) }
private val iInfo: LogMethod = { info(it) }
private val iDebug: LogMethod = { debug(it) }
private val iTrace: LogMethod = { trace(it) }

private val loggingMethods: Map<LogLevel, LogMethod> by lazy {
mapOf(
LogLevel.Error to iError,
LogLevel.Warning to iWarning,
LogLevel.Info to iInfo,
LogLevel.Debug to iDebug,
LogLevel.Trace to iTrace
)
}

enum class LogLevel { Error, Warning, Info, Debug, Trace }

inline val Any.logger: Logger get() = logger(javaClass)

Expand Down Expand Up @@ -75,3 +96,25 @@ fun <T> Any.todo(message: String, @Suppress("UNUSED_PARAMETER") block: () -> T)
this.logger.warn("TODO $message \n\t ${Exception().stackTrace[1]}")
}
}

fun <T> Any.logTime(name: String, level: LogLevel = LogLevel.Debug, block: () -> T): T {
val start = System.currentTimeMillis()
return try {
block().also {
loggingMethods[level]!!.invoke(logger, "The execution of the $name took ${tookFrom(start)}")
}
} catch (e: Exception) {
logWarning { "$name execution failed after ${tookFrom(start)}" }
throw e
}
}

private fun tookFrom(start: Long) = (System.currentTimeMillis() - start).let {
when {
it >= TimeUnit.DAYS.toMillis(1) -> "${TimeUnit.MILLISECONDS.toDays(it)} day(s) and ${TimeUnit.MILLISECONDS.toHours(it)} hour(s)"
it >= TimeUnit.HOURS.toMillis(1) -> "${TimeUnit.MILLISECONDS.toHours(it)} hour(s) and ${TimeUnit.MILLISECONDS.toMinutes(it)} minute(s)"
it >= TimeUnit.MINUTES.toMillis(1) -> "${TimeUnit.MILLISECONDS.toMinutes(it)} minute(s) and ${TimeUnit.MILLISECONDS.toSeconds(it)} second(s)"
it >= TimeUnit.SECONDS.toMillis(1) -> "${TimeUnit.MILLISECONDS.toSeconds(it)} second(s) and ${it % 1000} milliseconds"
else -> "$it milliseconds"
}
}
56 changes: 52 additions & 4 deletions src/test/kotlin/com/github/paslavsky/LoggingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import org.junit.Test
import org.junit.Before
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.lang.RuntimeException
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
import java.util.concurrent.TimeUnit
import kotlin.test.*

class LoggingTest {
private lateinit var output: ByteArrayOutputStream
Expand Down Expand Up @@ -221,4 +219,54 @@ class LoggingTest {
wroteText.contains(">>>TODO-MESSAGE<<<")
}
}

@Test
fun logTime() {
logTime("PROCESS_NAME") {
// Some dummy actions
TimeUnit.SECONDS.sleep(1)
}

val wroteText = output.toString()
assertTrue("Actual:\t$wroteText,\nExpected:\t * DEBUG * PROCESS_NAME * took 1 second(s) *") {
wroteText.contains("DEBUG") &&
wroteText.contains("PROCESS_NAME") &&
wroteText.contains("took 1 second(s)")
}
}

@Test
fun logTimeInfo() {
logTime("PROCESS_NAME", LogLevel.Info) {
// Some dummy actions
TimeUnit.SECONDS.sleep(1)
}

val wroteText = output.toString()
assertTrue("Actual:\t$wroteText,\nExpected:\t * INFO * PROCESS_NAME * took 1 second(s) *") {
wroteText.contains("INFO") &&
wroteText.contains("PROCESS_NAME") &&
wroteText.contains("took 1 second(s)")
}
}

@Test
fun logTimeFail() {
try {
logTime("PROCESS_NAME", LogLevel.Info) {
// Some dummy actions
TimeUnit.SECONDS.sleep(1)
throw UnsupportedOperationException()
}
@Suppress("UNREACHABLE_CODE")
fail()
} catch (e: UnsupportedOperationException) {
val wroteText = output.toString()
assertTrue("Actual:\t$wroteText,\nExpected:\t * WARN * PROCESS_NAME * execution failed after 1 second(s) *") {
wroteText.contains("WARN") &&
wroteText.contains("PROCESS_NAME") &&
wroteText.contains("execution failed after 1 second(s)")
}
}
}
}

0 comments on commit 1d6a11c

Please sign in to comment.