jvmMain.com.caesarealabs.loggy.lib.jvm.MongoDbLogSink.kt Maven / Gradle / Ivy
The newest version!
@file:Suppress("FunctionName")
package com.caesarealabs.loggy.lib.jvm
import com.caesarealabs.loggy.jvm.LoggyMongodb
import com.caesarealabs.loggy.lib.LogPrintMode
import com.caesarealabs.loggy.lib.LogSink
import com.caesarealabs.loggy.lib.Loggy
import com.caesarealabs.loggy.shared.Invocation
import com.caesarealabs.rpc4k.generated.LoggyServiceEventInvoker
import com.caesarealabs.rpc4k.runtime.jvm.user.components.mongo.MongoDb
public class MongodbLogSink(mongo: MongoDb) : LogSink {
private val loggy = LoggyMongodb(mongo)
override suspend fun add(invocation: Invocation) {
try {
loggy.logsCollection.insertOne(invocation)
} catch (e: Throwable) {
throw IllegalStateException("Failed to write invocation log: $invocation", e)
}
}
}
/**
* The entry point to using Loggy with MongoDB.
* First construct a static instance of this, and then use [Loggy.wrapCall] to wrap your endpoint handlers
* @param mongo A local or cloud mongodb instance
* @param invoker Optional. If provided, adding invocations will send events to the client, and update the logs content live.
*/
public fun MongoLoggy(
mongo: MongoDb,
invoker: LoggyServiceEventInvoker?,
printMode: LogPrintMode = LogPrintMode.AfterInvocation
): Loggy = Loggy(MongodbLogSink(mongo), invoker, printMode)