How to Access Underlying SLF4J Logger from oshai.kotlin-logging’s KLogger in Spring?

  Kiến thức lập trình

I’m migrating my project to use oshai’s kotlin-logging. My external class AbstractConnector expects an SLF4J Logger, but KLogger in oshai’s kotlin-logging doesn’t inherit it more and doesn’t expose an underlyingLogger property.

Here’s the existing external abstract class:

abstract class AbstractConnector(
private val log: org.slf4j.Logger
)

The previous implementation of my Connector looked like this:

import mu.KotlinLogging

class MyConnector(
private val client: HttpGraphQlClient,
private val mapper: MyMapper
) : AbstractGraphQlConnector(
 log
) {
    companion object {
        private val log = KotlinLogging.logger {}
    }

}

I need to migrate to oshai’s kotlin-logging, which doesn’t inherit org.slf4j.Logger more.

Here is the new library:

import io.github.oshai.kotlinlogging.KotlinLogging

My Problem:

I need to access the SLF4J Logger from the KLogger instance to pass it to the AbstractConnector. The previous method using mu.KotlinLogging allowed accessing the underlying SLF4J logger. How can I achieve this with oshai’s kotlin-logging?

Alternatively, could I exclusively use SLF4J in my Kotlin class? What are the implications?

Attempted Solutions:
Tried accessing underlyingLogger property, but it doesn’t exist in the new KLogger.
Explored the library documentation but couldn’t find a direct method to unwrap the KLogger.

New contributor

HViktor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT