Kotlin 实现的日志框架扩展 Cloudopt-logger

码农软件 · 软件分类 · 日志工具(Logging) · 2019-09-19 17:12:05

软件介绍

Cloudopt-logger 是一个基于 Kotlin 开发的可扩展、可配置的日志框架扩展,支持 Java、Kotlin 及 Android。

具有以下特性:

  • 支持彩色的日志输出。

  • 支持多种日志框架,如Slf4j、Log4j等等。

  • 易于扩展。

  • 更人性化和方便调试的输出。

中文文档

安装

在Maven中引入:

<dependency>
    <groupId>net.cloudopt.logger</groupId>
    <artifactId>cloudopt-logger</artifactId>
    <version>1.0.1</version>
</dependency>

如果您是使用Slf4j的话,需要引入相应的库,如:

<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的方法包裹即可。目前已经内置了八种颜色。

如何扩展

目前已经内置了对Slf4j的支持,通过Slf4j可以支持logback、log4j、log4j2等等,如果您需要直接支持或者支持其他日志框架,您可以参考Slf4jLoggerProvider,自行实现。

本文地址:https://codercto.com/soft/d/14953.html

写给大家看的设计书(第3版)

写给大家看的设计书(第3版)

[美] Robin Williams / 苏金国、刘亮 / 人民邮电出版社 / 2009-1 / 49.00元

这本书出自一位世界级设计师之手。复杂的设计原理在书中凝炼为亲密性、对齐、重复和对比4 个基本原则。作者以其简洁明快的风格,将优秀设计所必须遵循的这4 个基本原则及其背后的原理通俗易懂地展现在读者面前。本书包含大量的示例,让你了解怎样才能按照自己的方式设计出美观且内容丰富的产品。 此书适用于各行各业需要从事设计工作的读者,也适用于有经验的设计人员。一起来看看 《写给大家看的设计书(第3版)》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试