-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This DSL allows you to configure logging levels. Setting levels with `logging.level` configuration properties will override the DSL setting.
- Loading branch information
1 parent
954cc05
commit 26a952d
Showing
9 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
layout: default | ||
title: Logging | ||
nav_order: 8 | ||
has_children: false | ||
parent: Built-in DSLs | ||
--- | ||
|
||
# Logging DSL | ||
|
||
This DSL allows you to configure logging levels. Setting levels with `logging.level` configuration properties will override the DSL setting. | ||
|
||
## Usage | ||
|
||
```kotlin | ||
class TestKotlinApplication : SpringFunkApplication { | ||
override fun dsl(): SpringDslContainer.() -> Unit = { | ||
logging { | ||
root(LogLevel.INFO) | ||
level<MyClass>(LogLevel.OFF) | ||
level("com.example.package", LogLevel.DEBUG) | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
spring-funk-base/src/main/kotlin/com/github/wakingrufus/funk/logging/LoggingDsl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.github.wakingrufus.funk.logging | ||
|
||
import com.github.wakingrufus.funk.core.SpringDsl | ||
import com.github.wakingrufus.funk.core.SpringDslContainer | ||
import com.github.wakingrufus.funk.core.SpringDslMarker | ||
import org.springframework.boot.logging.LogLevel | ||
|
||
@SpringDslMarker | ||
class LoggingDsl : SpringDsl { | ||
internal val levels: MutableMap<String, LogLevel> = mutableMapOf() | ||
|
||
@SpringDslMarker | ||
fun root(level: LogLevel) { | ||
levels["ROOT"] = level | ||
} | ||
|
||
@SpringDslMarker | ||
fun level(packageName: String, level: LogLevel) { | ||
levels[packageName] = level | ||
} | ||
|
||
@SpringDslMarker | ||
inline fun <reified T> level(level: LogLevel) { | ||
T::class.qualifiedName?.also { level(it, level) } | ||
} | ||
} | ||
|
||
@SpringDslMarker | ||
fun SpringDslContainer.logging(config: LoggingDsl.() -> Unit) { | ||
register(LoggingDsl().apply(config)) | ||
} |
15 changes: 15 additions & 0 deletions
15
...ng-funk-base/src/main/kotlin/com/github/wakingrufus/funk/logging/LoggingDslInitializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.github.wakingrufus.funk.logging | ||
|
||
import com.github.wakingrufus.funk.base.getDsl | ||
import org.springframework.boot.logging.LoggingSystem | ||
import org.springframework.context.ApplicationContextInitializer | ||
import org.springframework.context.support.GenericApplicationContext | ||
|
||
class LoggingDslInitializer : ApplicationContextInitializer<GenericApplicationContext> { | ||
override fun initialize(applicationContext: GenericApplicationContext) { | ||
val loggingSystem: LoggingSystem = LoggingSystem.get(LoggingDslInitializer::class.java.classLoader) | ||
applicationContext.getDsl<LoggingDsl>()?.levels?.forEach { (name, level) -> | ||
loggingSystem.setLogLevel(name, level) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
org.springframework.context.ApplicationContextInitializer=\ | ||
com.github.wakingrufus.funk.beans.BeanDslInitializer | ||
com.github.wakingrufus.funk.beans.BeanDslInitializer,\ | ||
com.github.wakingrufus.funk.logging.LoggingDslInitializer |
87 changes: 87 additions & 0 deletions
87
spring-funk-base/src/test/kotlin/com/github/wakingrufus/funk/logging/LoggingDslTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.github.wakingrufus.funk.logging | ||
|
||
import ch.qos.logback.classic.Logger | ||
import ch.qos.logback.classic.spi.ILoggingEvent | ||
import ch.qos.logback.core.read.ListAppender | ||
import com.github.wakingrufus.funk.test.testDslApplication | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.boot.logging.LogLevel | ||
import org.springframework.boot.logging.LoggingSystem | ||
|
||
class LoggingDslTest { | ||
private val log = KotlinLogging.logger {} | ||
private lateinit var logAppender: ListAppender<ILoggingEvent> | ||
|
||
@BeforeEach | ||
fun setup() { | ||
logAppender = ListAppender<ILoggingEvent>() | ||
logAppender.start() | ||
} | ||
|
||
@AfterEach | ||
fun clean() { | ||
logAppender.stop() | ||
} | ||
|
||
@Test | ||
fun test() { | ||
val logger = LoggerFactory.getLogger(LoggingDslTest::class.java) as Logger | ||
logger.addAppender(logAppender) | ||
|
||
testDslApplication(LoggingDslInitializer()) { | ||
application { | ||
|
||
} | ||
environment { | ||
} | ||
test { | ||
log.info { "test" } | ||
assertThat(logAppender.list).hasSize(1) | ||
assertThat(logAppender.list[0].message).isEqualTo("test") | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `test root level`() { | ||
val logger = LoggerFactory.getLogger(LoggingDslTest::class.java) as Logger | ||
logger.addAppender(logAppender) | ||
|
||
testDslApplication(LoggingDslInitializer()) { | ||
application { | ||
logging { | ||
root(LogLevel.DEBUG) | ||
} | ||
} | ||
environment { | ||
} | ||
test { | ||
val loggingSystem = LoggingSystem.get(LoggingDslTest::class.java.classLoader) | ||
assertThat(loggingSystem.getLoggerConfiguration("ROOT").effectiveLevel) | ||
.isEqualTo(LogLevel.DEBUG) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `test logger off`() { | ||
testDslApplication(LoggingDslInitializer()) { | ||
application { | ||
logging { | ||
level<LoggingDslTest>(LogLevel.OFF) | ||
} | ||
} | ||
environment { | ||
} | ||
test { | ||
log.info { "test" } | ||
assertThat(logAppender.list).hasSize(0) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters