Skip to content

Commit

Permalink
Simplify config
Browse files Browse the repository at this point in the history
  • Loading branch information
ogesaku committed Jan 7, 2024
1 parent 97bdfd1 commit a98fa9c
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 114 deletions.
2 changes: 1 addition & 1 deletion klog/src/main/kotlin/com/coditory/klog/Klog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Klog(config: KlogConfig) {
Klog(
klogConfig {
stream {
batchPublisher(SystemOutPublisher.plainText(development = true))
asyncPublisher(SystemOutPublisher.plainText(development = true))
}
},
)
Expand Down
41 changes: 9 additions & 32 deletions klog/src/main/kotlin/com/coditory/klog/KlogContext.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.coditory.klog

import com.coditory.klog.config.AsyncLogPublisherConfig
import com.coditory.klog.config.BatchLogPublisherConfig
import com.coditory.klog.config.BlockingLogPublisherConfig
import com.coditory.klog.config.KlogConfig
import com.coditory.klog.config.KlogErrLogger
Expand All @@ -12,7 +11,6 @@ import com.coditory.klog.publish.BlockingLogSink
import com.coditory.klog.publish.BufferedLogSink
import com.coditory.klog.publish.LogPublisher
import com.coditory.klog.publish.LogPublisherListener
import com.coditory.klog.publish.SerialBatchLogPublisher
import com.coditory.klog.publish.SerialLogPublisher
import java.time.Clock

Expand Down Expand Up @@ -71,7 +69,6 @@ internal data class KlogContext(
return when (config) {
is BlockingLogPublisherConfig -> buildBlockingLogPublisher(descriptor, config, klogConfig)
is AsyncLogPublisherConfig -> buildAsyncLogPublisher(descriptor, config, klogConfig)
is BatchLogPublisherConfig -> buildBatchLogPublisher(descriptor, config, klogConfig)
}
}

Expand Down Expand Up @@ -103,38 +100,18 @@ internal data class KlogContext(
klogErrLogger = klogConfig.klogErrLogger,
)
}
return BufferedLogSink(
publisher = serialAsyncLogPublisher,
standardLogBufferCapacity = config.standardLogBufferCapacity,
prioritizedLogBufferCapacity = config.prioritizedLogBufferCapacity,
listener = LogPublisherListener.entry(descriptor, klogConfig.listener),
klogErrLogger = klogConfig.klogErrLogger,
)
}

private fun buildBatchLogPublisher(
descriptor: LogPublisherDescriptor,
config: BatchLogPublisherConfig,
klogConfig: KlogConfig,
): LogPublisher {
val serialAsyncLogPublisher =
if (!config.serialize) {
config.publisher
} else {
SerialBatchLogPublisher(
publisher = config.publisher,
listener = LogPublisherListener.terminal(descriptor, klogConfig.listener),
val batchingAsyncPublisher =
if (config.batchSize > 0) {
BatchingLogPublisher(
publisher = serialAsyncLogPublisher,
batchSize = config.batchSize,
maxBatchStaleness = config.maxBatchStaleness,
listener = LogPublisherListener.middle(descriptor, klogConfig.listener),
klogErrLogger = klogConfig.klogErrLogger,
)
} else {
serialAsyncLogPublisher
}
val batchingAsyncPublisher =
BatchingLogPublisher(
publisher = serialAsyncLogPublisher,
batchSize = config.batchSize,
maxBatchStaleness = config.maxBatchStaleness,
listener = LogPublisherListener.middle(descriptor, klogConfig.listener),
klogErrLogger = klogConfig.klogErrLogger,
)
return BufferedLogSink(
publisher = batchingAsyncPublisher,
standardLogBufferCapacity = config.standardLogBufferCapacity,
Expand Down
12 changes: 6 additions & 6 deletions klog/src/main/kotlin/com/coditory/klog/config/LogFilter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@ package com.coditory.klog.config
import com.coditory.klog.Level

data class LogFilter(
val range: LevelRange,
val levelRange: LevelRange,
val nameMatcher: LoggerNameMatcher,
) {
fun matches(
level: Level,
logger: String,
): Boolean {
return range.contains(level) && nameMatcher.matches(logger)
return levelRange.contains(level) && nameMatcher.matches(logger)
}

companion object {
fun matchAll() =
LogFilter(
range = LevelRange.allLevels(),
levelRange = LevelRange.allLevels(),
nameMatcher = LoggerNameMatcher.matchAll(),
)

fun matchNone() =
LogFilter(
range = LevelRange.allLevels(),
levelRange = LevelRange.allLevels(),
nameMatcher = LoggerNameMatcher.matchAll(),
)

fun filterByMinLevel(level: Level) =
LogFilter(
range = LevelRange.fromMinLevel(level),
levelRange = LevelRange.fromMinLevel(level),
nameMatcher = LoggerNameMatcher.matchAll(),
)

fun filterByBaseLoggerName(name: String): LogFilter =
LogFilter(
range = LevelRange.allLevels(),
levelRange = LevelRange.allLevels(),
nameMatcher = LoggerNameMatcher.fromLoggerBase(name),
)

Expand Down
70 changes: 12 additions & 58 deletions klog/src/main/kotlin/com/coditory/klog/config/LogPublisherConfig.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.coditory.klog.config

import com.coditory.klog.publish.AsyncLogPublisher
import com.coditory.klog.publish.BatchLogPublisher
import com.coditory.klog.publish.BatchingLogPublisher
import com.coditory.klog.publish.BlockingPublisher
import com.coditory.klog.publish.BufferedLogSink
Expand All @@ -13,21 +12,21 @@ data class BlockingLogPublisherConfig(
val publisher: BlockingPublisher,
) : LogPublisherConfig

data class BatchLogPublisherConfig(
val publisher: BatchLogPublisher,
data class AsyncLogPublisherConfig(
val publisher: AsyncLogPublisher,
// batching
val batchSize: Int = BatchingLogPublisher.Defaults.batchSize,
val maxBatchStaleness: Duration = BatchingLogPublisher.Defaults.maxBatchStaleness,
// buffering
val standardLogBufferCapacity: Int = BufferedLogSink.Defaults.standardLogBufferCapacity,
val prioritizedLogBufferCapacity: Int = BufferedLogSink.Defaults.prioritizedLogBufferCapacity,
var standardLogBufferCapacity: Int = BufferedLogSink.Defaults.standardLogBufferCapacity,
var prioritizedLogBufferCapacity: Int = BufferedLogSink.Defaults.prioritizedLogBufferCapacity,
// serializing
val serialize: Boolean = false,
var serialize: Boolean = true,
) : LogPublisherConfig

@ScopedKlogConfig
class BatchLogPublisherConfigBuilder(
private val publisher: BatchLogPublisher,
class AsyncLogPublisherConfigBuilder(
private val publisher: AsyncLogPublisher,
) {
// batching
private var batchSize: Int = BatchingLogPublisher.Defaults.batchSize
Expand All @@ -40,63 +39,16 @@ class BatchLogPublisherConfigBuilder(
// serializing
private var serialize: Boolean = false

fun batchSize(batchSize: Int): BatchLogPublisherConfigBuilder {
fun batchSize(batchSize: Int): AsyncLogPublisherConfigBuilder {
this.batchSize = batchSize
return this
}

fun maxBatchStaleness(maxBatchStaleness: Duration): BatchLogPublisherConfigBuilder {
fun maxBatchStaleness(maxBatchStaleness: Duration): AsyncLogPublisherConfigBuilder {
this.maxBatchStaleness = maxBatchStaleness
return this
}

fun standardLogBufferCapacity(standardLogBufferCapacity: Int): BatchLogPublisherConfigBuilder {
this.standardLogBufferCapacity = standardLogBufferCapacity
return this
}

fun prioritizedLogBufferCapacity(prioritizedLogBufferCapacity: Int): BatchLogPublisherConfigBuilder {
this.prioritizedLogBufferCapacity = prioritizedLogBufferCapacity
return this
}

fun serialize(serialize: Boolean): BatchLogPublisherConfigBuilder {
this.serialize = serialize
return this
}

fun build(): BatchLogPublisherConfig {
return BatchLogPublisherConfig(
publisher = publisher,
batchSize = batchSize,
maxBatchStaleness = maxBatchStaleness,
standardLogBufferCapacity = standardLogBufferCapacity,
prioritizedLogBufferCapacity = prioritizedLogBufferCapacity,
serialize = serialize,
)
}
}

data class AsyncLogPublisherConfig(
val publisher: AsyncLogPublisher,
// buffering
var standardLogBufferCapacity: Int = BufferedLogSink.Defaults.standardLogBufferCapacity,
var prioritizedLogBufferCapacity: Int = BufferedLogSink.Defaults.prioritizedLogBufferCapacity,
// serializing
var serialize: Boolean = true,
) : LogPublisherConfig

@ScopedKlogConfig
class AsyncLogPublisherConfigBuilder(
private val publisher: AsyncLogPublisher,
) {
// buffering
private var standardLogBufferCapacity: Int = BufferedLogSink.Defaults.standardLogBufferCapacity
private var prioritizedLogBufferCapacity: Int = BufferedLogSink.Defaults.prioritizedLogBufferCapacity

// serializing
private var serialize: Boolean = true

fun standardLogBufferCapacity(standardLogBufferCapacity: Int): AsyncLogPublisherConfigBuilder {
this.standardLogBufferCapacity = standardLogBufferCapacity
return this
Expand All @@ -112,9 +64,11 @@ class AsyncLogPublisherConfigBuilder(
return this
}

internal fun build(): AsyncLogPublisherConfig {
fun build(): AsyncLogPublisherConfig {
return AsyncLogPublisherConfig(
publisher = publisher,
batchSize = batchSize,
maxBatchStaleness = maxBatchStaleness,
standardLogBufferCapacity = standardLogBufferCapacity,
prioritizedLogBufferCapacity = prioritizedLogBufferCapacity,
serialize = serialize,
Expand Down
19 changes: 8 additions & 11 deletions klog/src/main/kotlin/com/coditory/klog/config/LogStreamConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.coditory.klog.config

import com.coditory.klog.Level
import com.coditory.klog.publish.AsyncLogPublisher
import com.coditory.klog.publish.BatchLogPublisher
import com.coditory.klog.publish.BlockingPublisher

data class LogStreamConfig(
Expand Down Expand Up @@ -30,6 +29,14 @@ class LogStreamConfigBuilder {
return this
}

fun filter(
levelRange: LevelRange,
nameMatcher: LoggerNameMatcher,
): LogStreamConfigBuilder {
this.filter = LogFilter(levelRange, nameMatcher)
return this
}

fun stopOnMatch(stopOnMatch: Boolean): LogStreamConfigBuilder {
this.stopOnMatch = stopOnMatch
return this
Expand All @@ -55,16 +62,6 @@ class LogStreamConfigBuilder {
return this
}

fun batchPublisher(
publisher: BatchLogPublisher,
configure: BatchLogPublisherConfigBuilder.() -> Unit = {},
): LogStreamConfigBuilder {
val builder = BatchLogPublisherConfigBuilder(publisher)
configure(builder)
this.publishers.add(builder.build())
return this
}

fun blockingPublisher(blockingPublisher: BlockingPublisher): LogStreamConfigBuilder {
this.publishers.add(BlockingLogPublisherConfig(blockingPublisher))
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

internal class BatchingLogPublisher(
private val publisher: BatchLogPublisher,
private val publisher: AsyncLogPublisher,
private val batchSize: Int = Defaults.batchSize,
private val maxBatchStaleness: Duration = Defaults.maxBatchStaleness,
private val klogErrLogger: KlogErrLogger = KlogErrLogger.STDERR,
Expand Down Expand Up @@ -84,6 +84,10 @@ internal class BatchingLogPublisher(
}
}

override suspend fun publishBatchAsync(events: List<LogEvent>) {
events.forEach { publishAsync(it) }
}

override fun publishBlocking(event: LogEvent) =
runBlocking {
publisher.publishBlocking(event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ interface LogPublisher : FlushablePublisher, BlockingPublisher {

interface AsyncLogPublisher : FlushablePublisher, BlockingPublisher {
suspend fun publishAsync(event: LogEvent)
}

interface BatchLogPublisher : FlushablePublisher, BlockingPublisher {
suspend fun publishBatchAsync(events: List<LogEvent>)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock

internal class SerialBatchLogPublisher(
private val publisher: BatchLogPublisher,
private val publisher: AsyncLogPublisher,
private val listener: LogPublisherListener = LogPublisherListener.NOOP,
private val klogErrLogger: KlogErrLogger = KlogErrLogger.STDERR,
) : BatchLogPublisher {
) : AsyncLogPublisher {
private val mutex = Mutex()

override suspend fun stopAndFlush() {
Expand All @@ -24,6 +24,10 @@ internal class SerialBatchLogPublisher(
publisher.publishSuspending(event)
}

override suspend fun publishAsync(event: LogEvent) {
publisher.publishBatchAsync(listOf(event))
}

override suspend fun publishBatchAsync(events: List<LogEvent>) {
listener.received(events)
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ internal class SerialLogPublisher(
listener.dropped(event, e)
}
}

override suspend fun publishBatchAsync(events: List<LogEvent>) {
events.forEach { publishAsync(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.coditory.klog.text.plain.PlainTextLogEventSerializer

class SystemOutPublisher(
private val formatter: TextLogEventSerializer,
) : AsyncLogPublisher, BatchLogPublisher {
) : AsyncLogPublisher {
override suspend fun publishAsync(event: LogEvent) {
// performance tests reveal that there is no use in switching to IO dispatcher
publishBlocking(event)
Expand Down

0 comments on commit a98fa9c

Please sign in to comment.