Skip to content

Commit

Permalink
Add isLoggingOff implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
YarnSphere authored and oshai committed May 31, 2023
1 parent 47102c2 commit c3c6970
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,11 @@ public actual interface KLogger {
* @return True if this Logger is enabled for the ERROR level, false otherwise.
*/
public actual val isErrorEnabled: Boolean

/**
* Is the logger instance OFF?
*
* @return True if this Logger is set to the OFF level, false otherwise.
*/
public actual val isLoggingOff: Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,11 @@ internal class KLoggerAndroid(override val name: String) : KLogger {
* @return True if this Logger is enabled for the ERROR level, false otherwise.
*/
override val isErrorEnabled: Boolean = Log.isLoggable(name, Log.ERROR)

/**
* Is the logger instance OFF?
*
* @return True if this Logger is set to the OFF level, false otherwise.
*/
override val isLoggingOff: Boolean = !Log.isLoggable(name, Log.ASSERT)
}
16 changes: 15 additions & 1 deletion src/commonMain/kotlin/io/github/oshai/kotlinlogging/KLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ public expect interface KLogger {
* @return True if this Logger is enabled for the ERROR level, false otherwise.
*/
public val isErrorEnabled: Boolean

/**
* Is the logger instance OFF?
*
* @return True if this Logger is set to the OFF level, false otherwise.
*/
public val isLoggingOff: Boolean
}

/**
Expand All @@ -138,7 +145,7 @@ public fun KLogger.isEnabledForLevel(level: Level): Boolean {
Levels.INFO_INT -> isInfoEnabled
Levels.WARN_INT -> isWarnEnabled
Levels.ERROR_INT -> isErrorEnabled
Levels.OFF_INT -> false
Levels.OFF_INT -> isLoggingOff
else -> throw IllegalArgumentException("Level [$level] not recognized.")
}
}
Expand Down Expand Up @@ -261,4 +268,11 @@ public interface ActualKLogger {
* @return True if this Logger is enabled for the ERROR level, false otherwise.
*/
public val isErrorEnabled: Boolean

/**
* Is the logger instance OFF?
*
* @return True if this Logger is set to the OFF level, false otherwise.
*/
public val isLoggingOff: Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.github.oshai.kotlinlogging.Level
import io.github.oshai.kotlinlogging.Level.DEBUG
import io.github.oshai.kotlinlogging.Level.ERROR
import io.github.oshai.kotlinlogging.Level.INFO
import io.github.oshai.Level.OFF
import io.github.oshai.kotlinlogging.Level.TRACE
import io.github.oshai.kotlinlogging.Level.WARN
import io.github.oshai.kotlinlogging.Marker
Expand Down Expand Up @@ -166,4 +167,11 @@ internal class KLoggerJS(override val name: String) : KLogger {
* @return True if this Logger is enabled for the ERROR level, false otherwise.
*/
override val isErrorEnabled: Boolean = ERROR.isLoggingEnabled()

/**
* Is the logger instance OFF?
*
* @return True if this Logger is set to the OFF level, false otherwise.
*/
override val isLoggingOff: Boolean = OFF.isLoggingEnabled()
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package io.github.oshai.kotlinlogging

import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.*

private val logger = KotlinLogging.logger("SimpleJsTest")

Expand Down Expand Up @@ -33,6 +30,7 @@ class SimpleJsTest {
@Test
fun offLevelJsTest() {
KotlinLoggingConfiguration.LOG_LEVEL = Level.OFF
assertTrue(logger.isLoggingOff)
logger.error { "error msg" }
assertEquals("NA", appender.lastMessage)
assertEquals("NA", appender.lastLevel)
Expand Down
8 changes: 8 additions & 0 deletions src/jvmMain/kotlin/io/github/oshai/kotlinlogging/KLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -549,4 +549,12 @@ public actual interface KLogger : ActualKLogger {
* @param t the exception (throwable) to log
*/
public fun error(marker: Marker?, msg: String?, t: Throwable?)

/**
* Similar to [.isLoggingOff] method except that the marker data is also taken into consideration.
*
* @param marker The marker data to take into consideration
* @return True if this Logger is set to the OFF level, false otherwise.
*/
public fun isLoggingOff(marker: Marker?): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@ internal class JulLoggerWrapper(override val underlyingLogger: Logger) : KLogger
return underlyingLogger.isLoggable(ERROR.toJULLevel())
}

override val isLoggingOff: Boolean
get() = underlyingLogger.isLoggable(OFF.toJULLevel())

override fun isLoggingOff(marker: Marker?): Boolean {
return underlyingLogger.isLoggable(OFF.toJULLevel())
}

private fun io.github.oshai.kotlinlogging.Level.toJULLevel(): Level {
val julLevel: Level =
when (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ internal abstract class Slf4jLoggerWrapper(override val underlyingLogger: Logger
override fun isErrorEnabled(marker: Marker?): Boolean {
return underlyingLogger.isErrorEnabled(marker?.toSlf4j())
}

override val isLoggingOff: Boolean
get() = !underlyingLogger.isErrorEnabled

override fun isLoggingOff(marker: Marker?): Boolean {
return !underlyingLogger.isErrorEnabled(marker?.toSlf4j())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,11 @@ internal class KLoggerNative(override val name: String) : KLogger {
* @return True if this Logger is enabled for the ERROR level, false otherwise.
*/
override val isErrorEnabled: Boolean = ERROR.isLoggingEnabled()

/**
* Is the logger instance OFF?
*
* @return True if this Logger is set to the OFF level, false otherwise.
*/
override val isLoggingOff: Boolean = OFF.isLoggingEnabled()
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package io.github.oshai.kotlinlogging

import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.*

private val logger = KotlinLogging.logger {}

Expand Down Expand Up @@ -34,6 +31,7 @@ class SimpleNativeTest {
@Test
fun offLevelNativeTest() {
KotlinLoggingConfiguration.logLevel = Level.OFF
assertTrue(logger.isLoggingOff)
logger.error { "error msg" }
assertEquals("NA", appender.lastMessage)
assertEquals("NA", appender.lastLevel)
Expand Down

0 comments on commit c3c6970

Please sign in to comment.