白季飞龙的个人主页

Logback大杂烩

使用Logback输出SpringBoot样式的控制台日志

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
</configuration>

使用Java代码配置Logback

以Kotlin代码做演示,输出TRACE级别日志,并自定义日志格式

package bj

import ch.qos.logback.classic.Level
import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.core.ConsoleAppender
import ch.qos.logback.core.Context
import org.slf4j.LoggerFactory

/**
 * Created by BaiJiFeiLong@gmail.com at 18-9-28 下午2:13
 */

fun main(args: Array<String>) {
    (LoggerFactory.getLogger("ROOT") as Logger).apply {
        level = Level.TRACE
        (getAppender("console") as ConsoleAppender).encoder = PatternLayoutEncoder().apply {
            context = LoggerFactory.getILoggerFactory() as Context
            pattern = "[%date] %highlight([%level]) [%logger{10} %file:%line] %msg%n"
            start()
        }
    }
    val logger = LoggerFactory.getLogger("MyLogger")
    logger.trace("TRACE")
    logger.debug("DEBUG")
    logger.info("INFO")
    logger.warn("WARN")
    logger.error("ERROR")
}

输出

[2018-09-28 15:59:18,096] [TRACE] [MyLogger FooApp.kt:24] TRACE
[2018-09-28 15:59:18,102] [DEBUG] [MyLogger FooApp.kt:25] DEBUG
[2018-09-28 15:59:18,102] [INFO] [MyLogger FooApp.kt:26] INFO
[2018-09-28 15:59:18,102] [WARN] [MyLogger FooApp.kt:27] WARN
[2018-09-28 15:59:18,102] [ERROR] [MyLogger FooApp.kt:28] ERROR

ConversionRule的配置

package bj

import ch.qos.logback.classic.Level
import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.core.ConsoleAppender
import ch.qos.logback.core.Context
import ch.qos.logback.core.CoreConstants
import org.slf4j.LoggerFactory
import org.springframework.boot.logging.logback.ColorConverter
import org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter
import org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter

/**
 * Created by BaiJiFeiLong@gmail.com at 18-9-28 下午2:13
 */

fun main(args: Array<String>) {
    (LoggerFactory.getLogger("ROOT") as Logger).apply {
        level = Level.TRACE
        (getAppender("console") as ConsoleAppender).encoder = PatternLayoutEncoder().apply {
            context = LoggerFactory.getILoggerFactory() as Context
            context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, mapOf(
                    "clr" to ColorConverter::class.qualifiedName,
                    "wex" to WhitespaceThrowableProxyConverter::class.qualifiedName,
                    "wEx" to ExtendedWhitespaceThrowableProxyConverter::class.qualifiedName
            ))
            pattern = "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx"
            start()
        }
    }
    val logger = LoggerFactory.getLogger("MyLogger")
    logger.trace("TRACE")
    logger.debug("DEBUG")
    logger.info("INFO")
    logger.warn("WARN")
    logger.error("ERROR")
}

输出

2018-09-28 16:02:20.124 TRACE --- [           main] MyLogger                                 : TRACE
2018-09-28 16:02:20.128 DEBUG --- [           main] MyLogger                                 : DEBUG
2018-09-28 16:02:20.129  INFO --- [           main] MyLogger                                 : INFO
2018-09-28 16:02:20.129  WARN --- [           main] MyLogger                                 : WARN
2018-09-28 16:02:20.129 ERROR --- [           main] MyLogger                                 : ERROR

漫漫路,莫论逍遥;潜心修,只为悟道