Groovy +将日志写入文件+使用注释注入日志 [英] Groovy + write log to file + Inject Logging Using Annotations

查看:560
本文介绍了Groovy +将日志写入文件+使用注释注入日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建以下groovy脚本,以展示如何通过简单的注释将日志字段注入我们的类中

I create the following groovy script in order to show how to inject a log field into our classes with a simple annotation

// File: LogSlf4j.groovy
// Add dependencies for Slf4j API and Logback

@Grapes([

       @Grab(group='org.slf4j', module='slf4j-api', version='1.6.1'),
       @Grab(group='ch.qos.logback', module='logback-classic', version='0.9.28')
])

 import org.slf4j.*
 import groovy.util.logging.Slf4j

 // Use annotation to inject log field into the class.
 @Slf4j

 class faimily{

 def father() {


    log.debug 'car engine is hot'
    log.error 'my car is stuck'
}


    def mother() {


    log.debug 'dont have a water in the kitchen'
    log.error 'Cant make a cake'
}


}

 def helloWorld = new faimily()
 helloWorld.father()
 helloWorld.mother()

运行groovy脚本时,我得到以下结果(在GROOVY CONSOLE上)

when I run the groovy script I get the following results ( on GROOVY CONSOLE )

 17:58:50.938 [Thread-59] DEBUG faimily - car engine is hot
 17:58:50.938 [Thread-59] ERROR faimily - my car is stuck
 17:58:50.938 [Thread-59] DEBUG faimily - dont have a water in the kitchen
 17:58:50.938 [Thread-59] ERROR faimily - Cant make a cake

请提供建议,我们如何将结果打印到WIN计算机中的日志文件中,以及如何将其添加到groovy脚本中才能启用它?

please advice how we can print the results to a log file in the WIN machine , and what need to add to my groovy script in order to enable that?

例如:

日志文件

C:\ Program Files \ LOGS \ my.groovy.log

C:\Program Files\LOGS\my.groovy.log

(应包含结果:)

 17:58:50.938 [Thread-59] DEBUG faimily - car engine is hot
 17:58:50.938 [Thread-59] ERROR faimily - my car is stuck
 17:58:50.938 [Thread-59] DEBUG faimily - dont have a water in the kitchen
 17:58:50.938 [Thread-59] ERROR faimily - Cant make a cake

推荐答案

这对我有用:

@Grab('org.slf4j:slf4j-api:1.6.1')
@Grab('ch.qos.logback:logback-classic:0.9.28')

import org.slf4j.*
import groovy.util.logging.Slf4j
import ch.qos.logback.core.*
import ch.qos.logback.classic.encoder.*

// Use annotation to inject log field into the class.
@Slf4j
class Family {
    static {
        new FileAppender().with {
            name = 'file appender'
            file = 'C:\\tmp\\groovy.log'
            context = LoggerFactory.getILoggerFactory()
            encoder = new PatternLayoutEncoder().with {
                context = LoggerFactory.getILoggerFactory()
                pattern = "%date{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg%n"
                start()
                it
            }
            start()
            log.addAppender(it)
        }
    }

    def father() {
        log.debug 'car engine is hot'
        log.error 'my car is stuck'
    }

    def mother() {
        log.debug 'dont have a water in the kitchen'
        log.error 'Cant make a cake'
    }
}

def helloWorld = new Family()
helloWorld.father()
helloWorld.mother()

这篇关于Groovy +将日志写入文件+使用注释注入日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆