我有一个基本的 Groovy 脚本,我希望尽可能简单地创建日志。我希望消息发送到标准输出以及日志文件,并且日志文件中的每个条目都有一个时间戳。
我不能使用@Log 表示法,因为它是一个脚本,而且我没有要注入的类。否则我认为这将是理想的。
您可以在脚本中使用以下模式(在 Groovy 编辑器中尝试过)。
import java.util.logging.Logger
Logger logger = Logger.getLogger("")
logger.info ("I am a test info log")
上面的日志输出到 STDOUT。为了将其记录到文件中,您必须使用getLogger
. 为了您的方便,请遵循API 。
使用 Log 注释是在 groovy 中启用日志记录的最简单方法。将其与 Grape 注释结合起来以下拉日志框架,您就可以在一个脚本中拥有所需的一切:
//
// Dependencies
// ============
import groovy.util.logging.Slf4j
@Grapes([
@Grab(group='ch.qos.logback', module='logback-classic', version='1.0.13')
])
//
// Classes
// =======
@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 SpecialGreeting {
def greet() {
log.trace "Hello world"
log.debug "Hello world"
log.warn "Hello world"
log.info "Hello world"
log.error "Hello world"
}
}
@Slf4j
class GreetingRunner {
def greetings = [new StandardGreeting(), new SpecialGreeting()]
def run() {
log.info "Starting to talk"
greetings.each {
it.greet()
}
log.info "Finished talking"
}
}
//
// Main program
// ============
def runner = new GreetingRunner()
runner.run()
这是我为几个 logback 功能创建一个最小示例的尝试,包括日志记录到文件。在上面扩展@Mark O'Connor 的答案
foo.groovy:
import groovy.util.logging.Slf4j
@Grab('ch.qos.logback:logback-classic:1.2.1')
@Slf4j
class Foo {
@Slf4j
static class Bar {
def bar() {
assert log.name == 'Foo$Bar'
assert log.class == ch.qos.logback.classic.Logger
log.trace "bar"
log.debug "bar"
log.warn "bar"
log.info "bar"
log.error "bar"
}
}
def foo() {
assert log.name == "Foo"
assert log.class == ch.qos.logback.classic.Logger
log.trace "foo"
log.debug "foo"
log.warn "foo"
log.info "foo"
log.error "foo"
}
}
@Slf4j
class Baz {
def baz() {
log.name = 'Baz'
assert log.name == 'Baz'
assert log.class == ch.qos.logback.classic.Logger
log.trace "baz"
log.debug "baz"
log.warn "baz"
log.info "baz"
log.error "baz"
}
}
new Foo().foo()
new Foo.Bar().bar()
new Baz().baz()
logback-test.xml 或 logback.xml:
<configuration debug="true"> <!-- debug attr enables status data dump -->
<timestamp key="sec" datePattern="yyyyMMdd_HHmmss"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> <!-- see Chapter 6 Layouts for format specifiers -->
</encoder>
</appender>
<!-- for RollingFileAppender see Chapter 3 Appenders -->
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>foo_${sec}.log</file>
<append>true</append> <!-- true is the default for append -->
<immediateFlush>true</immediateFlush> <!-- true is the default for immediateFlush -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- TRACE < DEBUG < INFO < WARN < ERROR -->
<!-- Read Chapter 2 Architecture of logback doc for effective
level (level inheritance) and accumulation (appender additivity) -->
<root level="debug">
<appender-ref ref="STDOUT"/>
</root>
<logger name="Foo" level="trace" additivity="true">
<appender-ref ref="FILE"/>
</logger>
<!-- if a logger isn't specified for a name, its level="null" and additivity="true", "null" being synonymous to "inherited" -->
<!-- '$' acts as '.' it seems -->
<logger name="Foo$Bar" level="inherited" additivity="true"/> <!-- if additivity false, no appender, otherwise, STDOUT and FILE -->
</configuration>
另请参阅logback 文档
使用@Slf4j
注解需要两行代码:import
语句和注解本身。在这里,用两行来说明如何在 Groovy 脚本中获取记录器:
import org.slf4j.LoggerFactory
def logger = LoggerFactory.getLogger(this.class)
// Use sl4j's placeholders.
logger.info "Reading entries in {}", tsv
// Use a GString.
logger.debug "Extracted file $outputFile exists"