Skip to content

Commit

Permalink
Parameterise log in LogsMetric (#625)
Browse files Browse the repository at this point in the history
  • Loading branch information
nomisRev authored Jan 10, 2024
1 parent f8e10c9 commit 8969e32
Showing 1 changed file with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package com.xebia.functional.xef.metrics
import arrow.atomic.AtomicInt
import com.xebia.functional.xef.prompt.Prompt
import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.oshai.kotlinlogging.Level
import io.ktor.util.date.*

class LogsMetric : Metric {
class LogsMetric(private val level: Level = Level.INFO) : Metric {

private val numberOfBlocks = AtomicInt(0)

Expand All @@ -15,11 +16,11 @@ class LogsMetric : Metric {

override suspend fun <A> customSpan(name: String, block: suspend Metric.() -> A): A {
val millis = getTimeMillis()
logger.info { "${writeIndent(numberOfBlocks.get())}> Custom-Span: $name" }
logger.at(level) { message = "${writeIndent(numberOfBlocks.get())}> Custom-Span: $name" }
numberOfBlocks.incrementAndGet()
val output = block()
logger.info {
"${writeIndent(numberOfBlocks.get())}|-- Finished in ${getTimeMillis() - millis} ms"
logger.at(level) {
message = "${writeIndent(numberOfBlocks.get())}|-- Finished in ${getTimeMillis() - millis} ms"
}
numberOfBlocks.decrementAndGet()
return output
Expand All @@ -28,26 +29,26 @@ class LogsMetric : Metric {
override suspend fun <A, T> promptSpan(prompt: Prompt<T>, block: suspend Metric.() -> A): A {
val millis = getTimeMillis()
val name = prompt.messages.lastOrNull()?.contentAsString() ?: "empty"
logger.info { "${writeIndent(numberOfBlocks.get())}> Prompt-Span: $name" }
logger.at(level) { message = "${writeIndent(numberOfBlocks.get())}> Prompt-Span: $name" }
numberOfBlocks.incrementAndGet()
val output = block()
logger.info {
"${writeIndent(numberOfBlocks.get())}|-- Finished in ${getTimeMillis() - millis} ms"
logger.at(level) {
message = "${writeIndent(numberOfBlocks.get())}|-- Finished in ${getTimeMillis() - millis} ms"
}
numberOfBlocks.decrementAndGet()
return output
}

override suspend fun event(message: String) {
logger.info { "${writeIndent(numberOfBlocks.get())}|-- $message" }
logger.at(level) { this.message = "${writeIndent(numberOfBlocks.get())}|-- $message" }
}

override suspend fun parameter(key: String, value: String) {
logger.info { "${writeIndent(numberOfBlocks.get())}|-- $key = $value" }
logger.at(level) { message = "${writeIndent(numberOfBlocks.get())}|-- $key = $value" }
}

override suspend fun parameter(key: String, values: List<String>) {
logger.info { "${writeIndent(numberOfBlocks.get())}|-- $key = $values" }
logger.at(level) { message = "${writeIndent(numberOfBlocks.get())}|-- $key = $values" }
}

private fun writeIndent(times: Int = 1) = (1..indentSize * times).fold("") { a, b -> "$a " }
Expand Down

0 comments on commit 8969e32

Please sign in to comment.