Cloudopt-loggerKotlin 實現(xiàn)的日志框架擴展
Cloudopt-logger 是一個基于 Kotlin 開發(fā)的可擴展、可配置的日志框架擴展,支持 Java、Kotlin 及 Android。
具有以下特性:
支持彩色的日志輸出。
支持多種日志框架,如Slf4j、Log4j等等。
易于擴展。
更人性化和方便調(diào)試的輸出。
安裝
在Maven中引入:
<dependency> <groupId>net.cloudopt.logger</groupId> <artifactId>cloudopt-logger</artifactId> <version>1.0.1</version> </dependency>
如果您是使用Slf4j的話,需要引入相應(yīng)的庫,如:
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency>
如何使用
使用起來非常簡單,只需要引入Logger類即可,下面是幾個示例:
package net.cloudopt.logger
import org.junit.Test
class TestCase {
private val logger = Logger.getLogger(TestCase::class.java)
@Test
fun example1() {
logger.debug("Start init....")
logger.info("Operation successful!")
logger.warn("The value must be not nul.")
logger.error("Unable to acquire lock!")
}
@Test
fun example2() {
logger.info("Please Wait.... ${Colorer.blue("100")}")
logger.info("Please Wait.... ${Colorer.yellow("200")}")
logger.info("Please Wait.... ${Colorer.red("300")}")
}
@Test
fun example3() {
val configuration = LoggerConfiguration()
configuration.run {
this.color = false
}
Logger.configuration = configuration
example1()
}
@Test
fun example4() {
val configuration = LoggerConfiguration()
configuration.run {
this.debugPrefix = "DEBUG"
this.infoPrefix = "INFO"
this.warnPrefix = "WARN"
this.errorPrefix = "ERROR"
}
Logger.configuration = configuration
example1()
}
}
如果您想要修改任何輸出的字符的顏色,只需要通過Colorer.xxx的方法包裹即可。目前已經(jīng)內(nèi)置了八種顏色。
如何擴展
目前已經(jīng)內(nèi)置了對Slf4j的支持,通過Slf4j可以支持logback、log4j、log4j2等等,如果您需要直接支持或者支持其他日志框架,您可以參考Slf4jLoggerProvider,自行實現(xiàn)。
評論
圖片
表情
