Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
- uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: 8
java-version: 11

- name: Restore Gradle cache
id: cache-gradle
Expand Down
17 changes: 17 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# CONTRIBUTING

Pull requests are welcome!
Before submitting a PR, it is usually better to discuss your intentions either by opening a new issue or in [slack](https://kotlinlang.slack.com/messages/kotlin-logging/).

# Building locally

`./gradlew clean build`

To check formatting:

`./gradlew ktfmtCheck`

To fix formatting:

`./gradlew ktfmtFormat`

3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins {
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
signing
id("io.gitlab.arturbosch.detekt") version "1.18.0"
id("com.ncorti.ktfmt.gradle") version "0.11.0"
}


Expand Down Expand Up @@ -37,7 +38,7 @@ kotlin {
kotlinOptions {
apiVersion = "1.4"
languageVersion = "1.4"
jvmTarget = "1.8"
jvmTarget = "11"
}
}
}
Expand Down
197 changes: 73 additions & 124 deletions src/commonMain/kotlin/mu/KLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,129 +2,78 @@ package mu

public expect interface KLogger {

/**
* Lazy add a log message if isTraceEnabled is true
*/
public fun trace(msg: () -> Any?)

/**
* Lazy add a log message if isDebugEnabled is true
*/
public fun debug(msg: () -> Any?)

/**
* Lazy add a log message if isInfoEnabled is true
*/
public fun info(msg: () -> Any?)

/**
* Lazy add a log message if isWarnEnabled is true
*/
public fun warn(msg: () -> Any?)

/**
* Lazy add a log message if isErrorEnabled is true
*/
public fun error(msg: () -> Any?)

/**
* Lazy add a log message with throwable payload if isTraceEnabled is true
*/
public fun trace(t: Throwable?, msg: () -> Any?)

/**
* Lazy add a log message with throwable payload if isDebugEnabled is true
*/
public fun debug(t: Throwable?, msg: () -> Any?)

/**
* Lazy add a log message with throwable payload if isInfoEnabled is true
*/
public fun info(t: Throwable?, msg: () -> Any?)

/**
* Lazy add a log message with throwable payload if isWarnEnabled is true
*/
public fun warn(t: Throwable?, msg: () -> Any?)

/**
* Lazy add a log message with throwable payload if isErrorEnabled is true
*/
public fun error(t: Throwable?, msg: () -> Any?)

/**
* Lazy add a log message with a marker if isTraceEnabled is true
*/
public fun trace(marker: Marker?, msg: () -> Any?)

/**
* Lazy add a log message with a marker if isDebugEnabled is true
*/
public fun debug(marker: Marker?, msg: () -> Any?)

/**
* Lazy add a log message with a marker if isInfoEnabled is true
*/
public fun info(marker: Marker?, msg: () -> Any?)

/**
* Lazy add a log message with a marker if isWarnEnabled is true
*/
public fun warn(marker: Marker?, msg: () -> Any?)

/**
* Lazy add a log message with a marker if isErrorEnabled is true
*/
public fun error(marker: Marker?, msg: () -> Any?)

/**
* Lazy add a log message with a marker and throwable payload if isTraceEnabled is true
*/
public fun trace(marker: Marker?, t: Throwable?, msg: () -> Any?)

/**
* Lazy add a log message with a marker and throwable payload if isDebugEnabled is true
*/
public fun debug(marker: Marker?, t: Throwable?, msg: () -> Any?)

/**
* Lazy add a log message with a marker and throwable payload if isInfoEnabled is true
*/
public fun info(marker: Marker?, t: Throwable?, msg: () -> Any?)

/**
* Lazy add a log message with a marker and throwable payload if isWarnEnabled is true
*/
public fun warn(marker: Marker?, t: Throwable?, msg: () -> Any?)

/**
* Lazy add a log message with a marker and throwable payload if isErrorEnabled is true
*/
public fun error(marker: Marker?, t: Throwable?, msg: () -> Any?)

/**
* Add a log message with all the supplied parameters along with method name
*/
public fun entry(vararg argArray: Any?)

/**
* Add log message indicating exit of a method
*/
public fun exit()

/**
* Add a log message with the return value of a method
*/
public fun <T> exit(result: T): T where T : Any?

/**
* Add a log message indicating an exception will be thrown along with the stack trace.
*/
public fun <T> throwing(throwable: T): T where T : Throwable

/**
* Add a log message indicating an exception is caught along with the stack trace.
*/
public fun <T> catching(throwable: T) where T : Throwable
/** Lazy add a log message if isTraceEnabled is true */
public fun trace(msg: () -> Any?)

/** Lazy add a log message if isDebugEnabled is true */
public fun debug(msg: () -> Any?)

/** Lazy add a log message if isInfoEnabled is true */
public fun info(msg: () -> Any?)

/** Lazy add a log message if isWarnEnabled is true */
public fun warn(msg: () -> Any?)

/** Lazy add a log message if isErrorEnabled is true */
public fun error(msg: () -> Any?)

/** Lazy add a log message with throwable payload if isTraceEnabled is true */
public fun trace(t: Throwable?, msg: () -> Any?)

/** Lazy add a log message with throwable payload if isDebugEnabled is true */
public fun debug(t: Throwable?, msg: () -> Any?)

/** Lazy add a log message with throwable payload if isInfoEnabled is true */
public fun info(t: Throwable?, msg: () -> Any?)

/** Lazy add a log message with throwable payload if isWarnEnabled is true */
public fun warn(t: Throwable?, msg: () -> Any?)

/** Lazy add a log message with throwable payload if isErrorEnabled is true */
public fun error(t: Throwable?, msg: () -> Any?)

/** Lazy add a log message with a marker if isTraceEnabled is true */
public fun trace(marker: Marker?, msg: () -> Any?)

/** Lazy add a log message with a marker if isDebugEnabled is true */
public fun debug(marker: Marker?, msg: () -> Any?)

/** Lazy add a log message with a marker if isInfoEnabled is true */
public fun info(marker: Marker?, msg: () -> Any?)

/** Lazy add a log message with a marker if isWarnEnabled is true */
public fun warn(marker: Marker?, msg: () -> Any?)

/** Lazy add a log message with a marker if isErrorEnabled is true */
public fun error(marker: Marker?, msg: () -> Any?)

/** Lazy add a log message with a marker and throwable payload if isTraceEnabled is true */
public fun trace(marker: Marker?, t: Throwable?, msg: () -> Any?)

/** Lazy add a log message with a marker and throwable payload if isDebugEnabled is true */
public fun debug(marker: Marker?, t: Throwable?, msg: () -> Any?)

/** Lazy add a log message with a marker and throwable payload if isInfoEnabled is true */
public fun info(marker: Marker?, t: Throwable?, msg: () -> Any?)

/** Lazy add a log message with a marker and throwable payload if isWarnEnabled is true */
public fun warn(marker: Marker?, t: Throwable?, msg: () -> Any?)

/** Lazy add a log message with a marker and throwable payload if isErrorEnabled is true */
public fun error(marker: Marker?, t: Throwable?, msg: () -> Any?)

/** Add a log message with all the supplied parameters along with method name */
public fun entry(vararg argArray: Any?)

/** Add log message indicating exit of a method */
public fun exit()

/** Add a log message with the return value of a method */
public fun <T> exit(result: T): T where T : Any?

/** Add a log message indicating an exception will be thrown along with the stack trace. */
public fun <T> throwing(throwable: T): T where T : Throwable

/** Add a log message indicating an exception is caught along with the stack trace. */
public fun <T> catching(throwable: T) where T : Throwable
}
6 changes: 2 additions & 4 deletions src/commonMain/kotlin/mu/KMarkerFactory.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package mu

/**
* A platform independent factory to create markers.
*/
/** A platform independent factory to create markers. */
public expect object KMarkerFactory {

public fun getMarker(name: String): Marker
public fun getMarker(name: String): Marker
}
17 changes: 8 additions & 9 deletions src/commonMain/kotlin/mu/KotlinLogging.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package mu


public expect object KotlinLogging {
/**
* This method allow defining the logger in a file in the following way:
* ```
* val logger = KotlinLogging.logger {}
* ```
*/
public fun logger(func: () -> Unit): KLogger
/**
* This method allow defining the logger in a file in the following way:
* ```
* val logger = KotlinLogging.logger {}
* ```
*/
public fun logger(func: () -> Unit): KLogger

public fun logger(name: String): KLogger
public fun logger(name: String): KLogger
}
6 changes: 2 additions & 4 deletions src/commonMain/kotlin/mu/Marker.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package mu

/**
* A platform independent marker to enrich log statements.
*/
/** A platform independent marker to enrich log statements. */
public expect interface Marker {

public fun getName(): String
public fun getName(): String
}
12 changes: 6 additions & 6 deletions src/commonMain/kotlin/mu/internal/MessageInvoker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package mu.internal

@Suppress("NOTHING_TO_INLINE")
internal inline fun (() -> Any?).toStringSafe(): String {
return try {
invoke().toString()
} catch (e: Exception) {
ErrorMessageProducer.getErrorLog(e)
}
return try {
invoke().toString()
} catch (e: Exception) {
ErrorMessageProducer.getErrorLog(e)
}
}

public expect object ErrorMessageProducer {
public fun getErrorLog(e: Exception): String
public fun getErrorLog(e: Exception): String
}
13 changes: 6 additions & 7 deletions src/commonTest/kotlin/mu/internal/MessageInvokerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package mu.internal
import kotlin.test.Test
import kotlin.test.assertEquals


class MessageInvokerTest {

@Test
fun toStringSafeChecks() {
assertEquals(Unit.toString(), {}.toStringSafe())
assertEquals("null", { null }.toStringSafe())
assertEquals("hi", { "hi" }.toStringSafe())
}
@Test
fun toStringSafeChecks() {
assertEquals(Unit.toString(), {}.toStringSafe())
assertEquals("null", { null }.toStringSafe())
assertEquals("hi", { "hi" }.toStringSafe())
}
}
51 changes: 25 additions & 26 deletions src/darwinMain/kotlin/mu/OSLogAppender.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,38 @@ import platform.darwin._os_log_internal
import platform.darwin.os_log_t
import platform.darwin.os_log_type_enabled
import platform.darwin.os_log_type_t
import kotlin.native.concurrent.AtomicReference

public open class OSLogAppender: Appender {
override val includePrefix: Boolean = true
public open class OSLogAppender : Appender {
override val includePrefix: Boolean = true

protected open fun logger(loggerName: String): os_log_t {
return OS_LOG_DEFAULT
}
protected open fun logger(loggerName: String): os_log_t {
return OS_LOG_DEFAULT
}

private fun log(level: os_log_type_t, loggerName: String, message: String) {
val logger = logger(loggerName)
if (os_log_type_enabled(logger, level)) {
_os_log_internal(__dso_handle.ptr, logger, level, message)
}
private fun log(level: os_log_type_t, loggerName: String, message: String) {
val logger = logger(loggerName)
if (os_log_type_enabled(logger, level)) {
_os_log_internal(__dso_handle.ptr, logger, level, message)
}
}

override fun trace(loggerName: String, message: String) {
log(OS_LOG_TYPE_DEBUG, loggerName, message)
}
override fun trace(loggerName: String, message: String) {
log(OS_LOG_TYPE_DEBUG, loggerName, message)
}

override fun debug(loggerName: String, message: String) {
log(OS_LOG_TYPE_DEBUG, loggerName, message)
}
override fun debug(loggerName: String, message: String) {
log(OS_LOG_TYPE_DEBUG, loggerName, message)
}

override fun info(loggerName: String, message: String) {
log(OS_LOG_TYPE_INFO, loggerName, message)
}
override fun info(loggerName: String, message: String) {
log(OS_LOG_TYPE_INFO, loggerName, message)
}

override fun warn(loggerName: String, message: String) {
log(OS_LOG_TYPE_DEFAULT, loggerName, message)
}
override fun warn(loggerName: String, message: String) {
log(OS_LOG_TYPE_DEFAULT, loggerName, message)
}

override fun error(loggerName: String, message: String) {
log(OS_LOG_TYPE_ERROR, loggerName, message)
}
override fun error(loggerName: String, message: String) {
log(OS_LOG_TYPE_ERROR, loggerName, message)
}
}
Loading