From e908824d8255a4e7be454049ae3933eb65e053a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Monta=C3=B1ez?= Date: Wed, 4 Sep 2024 14:21:46 +0200 Subject: [PATCH] added logger in in-memory-metrics --- .../xebia/functional/xef/metrics/InMemoryCounterMetric.kt | 5 ++++- .../kotlin/com/xebia/functional/xef/metrics/LogsMetric.kt | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/InMemoryCounterMetric.kt b/core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/InMemoryCounterMetric.kt index dcd03c898..f032e95b1 100644 --- a/core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/InMemoryCounterMetric.kt +++ b/core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/InMemoryCounterMetric.kt @@ -1,15 +1,18 @@ package com.xebia.functional.xef.metrics import arrow.atomic.AtomicLong +import io.github.oshai.kotlinlogging.KLogger -class InMemoryCounterMetric : CounterMetric { +class InMemoryCounterMetric(val name: String, val logger: KLogger) : CounterMetric { private val count = AtomicLong(0) override fun increment(n: Long) { count.incrementAndGet() + logger.info { "Counter $name incremented to ${count.get()}" } } override fun decrement(n: Long) { count.decrementAndGet() + logger.info { "Counter $name decremented to ${count.get()}" } } } diff --git a/core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/LogsMetric.kt b/core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/LogsMetric.kt index 9304df7bc..ac18ca81f 100644 --- a/core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/LogsMetric.kt +++ b/core/src/commonMain/kotlin/com/xebia/functional/xef/metrics/LogsMetric.kt @@ -115,14 +115,14 @@ class LogsMetric(private val level: Level = Level.INFO) : Metric { override suspend fun createCounter(name: String): CounterMetric { logger.at(level) { message = "${writeIndent(numberOfBlocks.get())}> Created counter: $name" } - val counter = InMemoryCounterMetric() + val counter = InMemoryCounterMetric(name, logger) countersMap[name] = counter return counter } override suspend fun getCounter(name: String): CounterMetric { logger.at(level) { message = "${writeIndent(numberOfBlocks.get())}> Get counter: $name" } - return countersMap[name] ?: InMemoryCounterMetric() + return countersMap[name] ?: InMemoryCounterMetric(name, logger) } private fun writeIndent(times: Int = 1) = (1..indentSize * times).fold("") { a, _ -> "$a " }