Skip to content
This repository has been archived by the owner on Mar 2, 2022. It is now read-only.

Commit

Permalink
[minor] Adds mechanism for having multiple logging targets
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbrooks12 committed Aug 21, 2020
1 parent 6e9c3b3 commit d835616
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.3.0 - 2020-08-21

- Adds helper methods to disable logging in production, and to configure multiple logging targets

## 3.2.0 - 2020-08-20

- Enables SLF4J support on Android platform
Expand Down
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,33 @@ implementation for each supported platform:
### Turn off logging in production

```kotlin
Clog.configureLoggingInProduction(BuildConfig.DEBUG)
val isDebug = ...
Clog.configureLoggingInProduction(isDebug)
```

### Use a custom logger

Replaces the current logging target with a custom one.

```kotlin
val customLogger = object : ClogLogger {
override fun log(priority: Clog.Priority, tag: String?, message: String) {
...
}
}
Clog.updateProfile { it.copy(logger = customLogger) }
```

### Using multiple logging targets

Add an additional logger to the current instance. Calling `addLogger` multiple times will continue adding loggers, and
messages will be delegated to all loggers.

```kotlin
val customLogger = object : ClogLogger {
override fun log(priority: Clog.Priority, tag: String?, message: String) {
...
}
}
Clog.addLogger(customLogger)
```
2 changes: 1 addition & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ publishing {
password = ghToken
}
}
maven(url = "https://api.bintray.com/maven/javaeden/Eden/Clog/;publish=1;override=1;") {
maven(url = "https://api.bintray.com/maven/cjbrooks12/maven/clog/;publish=1;override=1;") {
name = "Bintray"
credentials {
username = bintrayUser
Expand Down
12 changes: 12 additions & 0 deletions core/src/commonMain/kotlin/dsl/dsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
package clog.dsl

import clog.Clog
import clog.api.ClogLogger
import clog.api.ClogMessageFormatter
import clog.api.ClogProfile
import clog.impl.DelegatingLogger
import clog.impl.DisableInProductionFilter

// Logging DSL
Expand Down Expand Up @@ -95,3 +97,13 @@ inline fun Clog.configureLoggingInProduction(isDebug: Boolean) {
it.copy(filter = DisableInProductionFilter(isDebug, it.filter))
}
}

/**
* Adds an additional [ClogLogger] as a logging target.
*/
inline fun Clog.addLogger(logger: ClogLogger) {
updateProfile {
val delegatingLogger = it.logger as? DelegatingLogger ?: DelegatingLogger(listOf(it.logger))
it.copy(logger = delegatingLogger + logger)
}
}
24 changes: 24 additions & 0 deletions core/src/commonMain/kotlin/impl/DelegatingLogger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package clog.impl

import clog.Clog
import clog.api.ClogLogger

/**
* Passes a log message to multiple other loggers
*/
data class DelegatingLogger(
val delegates: List<ClogLogger> = emptyList()
) : ClogLogger {
override fun log(priority: Clog.Priority, tag: String?, message: String) {
delegates.forEach { it.log(priority, tag, message) }
}

/**
* Returns a copy of this [DelegatingLogger] with [logger] appended to its list of [delegates].
*/
operator fun plus(logger: ClogLogger): DelegatingLogger {
return DelegatingLogger(
delegates + logger
)
}
}
29 changes: 27 additions & 2 deletions core/src/commonTest/kotlin/ClogSingletonTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package clog

import clog.dsl.addLogger
import clog.dsl.configureLoggingInProduction
import clog.impl.TestLogger
import clog.impl.clogTest
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down Expand Up @@ -100,7 +102,8 @@ class ClogSingletonTest {
@Test
fun testLogInProductionFilter_notProd() {
clogTest { logger ->
Clog.configureLoggingInProduction(false)
val isDebug = true
Clog.configureLoggingInProduction(isDebug)

Clog.v("m1")
assertTrue(logger.messageWasLogged)
Expand All @@ -113,7 +116,8 @@ class ClogSingletonTest {
@Test
fun testLogInProductionFilter_prod() {
clogTest { logger ->
Clog.configureLoggingInProduction(true)
val isDebug = false
Clog.configureLoggingInProduction(isDebug)

Clog.v("m1")
assertFalse(logger.messageWasLogged)
Expand All @@ -122,4 +126,25 @@ class ClogSingletonTest {
assertFalse(logger.messageWasLogged)
}
}

@Test
fun testAddLogger() {
clogTest { logger ->
val logger1 = TestLogger()
val logger2 = TestLogger()

Clog.addLogger(logger1)
Clog.addLogger(logger2)

Clog.v("m1")
assertTrue(logger.messageWasLogged)
assertTrue(logger1.messageWasLogged)
assertTrue(logger2.messageWasLogged)

Clog.e("m2")
assertTrue(logger.messageWasLogged)
assertTrue(logger1.messageWasLogged)
assertTrue(logger2.messageWasLogged)
}
}
}

0 comments on commit d835616

Please sign in to comment.