Groovy 中使用 logback 记录日志

文章目录
  1. 测试代码
  2. 配置代码

参考文档:

测试代码

引入依赖:

1
2
3
4
@Grapes([
@GrabConfig(systemClassLoader=true),
@Grab(group='ch.qos.logback', module='logback-classic', version='1.2.3')
])

注意:

1、如果是使用 logback.groovy 作为配置文件,需要设置 systemClassLoader=true,否则执行脚本时会报异常:GroovyCastException: Cannot cast object

2、可以使用如下参数指定配置文件:-DLogback.configurationFile=/path/to/logback.groovy

测试方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import groovy.util.logging.Slf4j
@Slf4j
class StandardGreeting {
def greet() {
log.trace "Hello world"
log.debug "Hello world"
log.warn "Hello world"
log.info "Hello world"
log.error "Hello world"
}
}

@Slf4j
class IceGreeting {
def greet(msg) {
log.trace "Hello ${msg}"
log.debug "Hello ${msg}"
log.warn "Hello ${msg}"
log.info "Hello ${msg}"
log.error "Hello ${msg}"
}
}

(1..100).each{
new StandardGreeting().greet()
new IceGreeting().greet("ice")
}

配置代码

使用 logback.groovy 配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
@Grapes([
@Grab(group='ch.qos.logback', module='logback-classic', version='1.2.3')
])

import static ch.qos.logback.classic.Level.TRACE
import static ch.qos.logback.classic.Level.DEBUG
import static ch.qos.logback.classic.Level.INFO
import static ch.qos.logback.classic.Level.WARN
import static ch.qos.logback.classic.Level.ERROR

import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.core.ConsoleAppender
import ch.qos.logback.classic.AsyncAppender
//import ch.qos.logback.classic.PatternLayout

scan("30 seconds")
def LOG_PATH="logs"
def LOG_ARCHIVE="${LOG_PATH}/archive"

appender("Console-Appender", ConsoleAppender) {
encoder(PatternLayoutEncoder) {
pattern = "[%d{yyyy-MM-dd HH:mm:ss.SSS Z}] [%logger{16}] [%thread] [%level] %msg%n"
}
}
appender("File-Appender", FileAppender) {
file = "${LOG_PATH}/logfile.log"
encoder(PatternLayoutEncoder) {
pattern = "%d{ISO8601,+0800} %logger{16} [%thread] %msg%n"
outputPatternAsHeader = true
}
}
appender("RollingFile-Appender", RollingFileAppender) {
file = "${LOG_PATH}/rollingfile.log"
rollingPolicy(TimeBasedRollingPolicy) {
fileNamePattern = "${LOG_ARCHIVE}/rollingfile.log%d{yyyy-MM-dd}.log"
maxHistory = 30
totalSizeCap = "1KB"
}
encoder(PatternLayoutEncoder) {
pattern = "[%d{yyyy-MM-dd HH:mm:ss.SSS Z}] [%logger{16}] [%thread] [%level] %msg%n"
}
}
appender("Async-Appender",AsyncAppender){
appenderRef("RollingFile-Appender")
}

logger("StandardGreeting",DEBUG,["File-Appender"])
logger("IceGreeting", DEBUG, ["Console-Appender", "File-Appender", "Async-Appender"], false)
root(INFO,["Console-Appender"])@Grapes([
@Grab(group='ch.qos.logback', module='logback-classic', version='1.2.3')
])

import static ch.qos.logback.classic.Level.INFO
import static ch.qos.logback.classic.Level.DEBUG

import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.core.ConsoleAppender

appender("CONSOLE", ConsoleAppender) {
encoder(PatternLayoutEncoder) {
pattern = "%-4relative [%thread] - %msg%n"
}
}
root(DEBUG, ["CONSOLE"])