【问题标题】:Groovy + write log to file + Inject Logging Using AnnotationsGroovy + 将日志写入文件 + 使用注解注入日志
【发布时间】:2014-06-25 15:12:16
【问题描述】:

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

// 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 上)

 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 脚本中添加什么才能启用它?

例如:

日志文件

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

【问题讨论】:

    标签: groovy


    【解决方案1】:

    这对我有用:

    @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()
    

    【讨论】:

    • 嗨,蒂姆——(第一个 +1 给你),我有一个问题——你的附加代码是否包括文件日志轮换?如果不是,如果日志容量很大,我们如何解决这个问题?
    • 不,你需要一个 RollingFileAppender。 Soz,这不是你的问题,所以我把它排除在外!等我有电脑的时候我会想办法
    • 找到这个可能有帮助? stackoverflow.com/questions/9060545/…
    • 不幸的是,我无法理解如何将这个解决方案推送到您的代码中,这并不简单(因为我是 groovy 的新手),或者您可以在您的代码中解决该解决方案 - 然后它会帮我分配
    • 嗨,蒂姆,根据我们的代码实现,我还有另一个问题,这里缺少一些东西,我想应该是一些文件配置,负责我们想要的日志,例如,如果我们只想查看日志中的DEBIG行,然后需要设置配置文件才能得到,所以请告诉我们如何设置这个文件以及如何配置它?
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 2017-09-25
    • 2012-04-06
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多