Groovy脚本和log4j [英] Groovy Script and log4j

查看:297
本文介绍了Groovy脚本和log4j的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



没有显式的类(它只是一个脚本)。
没有grails。
不用控制台...到文件。

只是一个普通的groovy脚本,带有log4j。



log4j最适合这个(groovy)还是其他日志记录库更好?



某人可以指向我的示例或演示如何完成?我设法把它交给控制台,但不能存档。



如果log4j配置也在config.groovy文件中,那会很好,因为我使用的是配置文件的其他东西。

更新



感谢neversleepz的例子,我有以下工作很好:



config.groovy文件:

  log4j {

appender.stdout =org.apache.log4j.ConsoleAppender
appender。stdout.layout=org.apache.log4j.PatternLayout
appender。 scrlog =org.apache.log4j.FileAppender
appender。scrlog.layout=org.apache.log4j.PatternLayout
appender。scrlog.layout.ConversionPattern=%d% 5p%c {1}:%L - %m%n
appender。scrlog.file=rootscript.log
rootLogger =debug,scrlog,stdout
}

以及我的脚本:

  import org.apache.log4j。* 
导入groovy.util.logging。*

)def config = new ConfigSlurper()。parse(new File('config.groovy')。toURL())
PropertyConfigurator.configure(config.toProperties())

Logger log = Logger.getInstance(getClass())

//需要按照此处所述设置日志级别:
// http://groovy.329449.n5.nabble.com/log4j-annotation -not-working-td4368806.html
log.level = Level.INFO

//这将不会打印/写入,因为loglevel是info
log.debug'执行脚本。'
//这将打印
log.info'简单示例显示日志INFO字段被注入'
log.warn'显示日志WARN字段的简单样本被注入'
log.error'简单示例显示日志ERR字段被注入。'

感谢此!

我也为RollingFileAppender进行了配置,而DailyRollingFileAppender我也将它们放在这里:

  log4j {
//
appender.stdout =org.apache.log4j.ConsoleAppen der
appender。stdout.layout=org.apache.log4j.PatternLayout
//
appender.scrlog =org.apache.log4j.DailyRollingFileAppender
scrlog.DatePattern='。yyyy-MM-dd
appender。scrlog.Append=true
appender。scrlog.File=rootscript.log
appender。scrlog.layout=org.apache.log4j.PatternLayout
appender。scrlog.layout.ConversionPattern=%d%5p%c {1}:%L - % m%n

rootLogger =debug,scrlog,stdout
}

  log4j {
//
appender.stdout =org.apache .log4j.ConsoleAppender
appender。stdout.layout=org.apache.log4j.PatternLayout
//
appender.scrlog =org.apache.log4j.DailyRollingFileAppender
appender。scrlog.DatePattern='。yyyy-MM-dd
appender。scrlog.Append=true
appender。scrlog.File=rootscript .log
appender。scrlog.layout=org.apache.log4j.PatternLayout
appender 。scrlog.layout.ConversionPattern=%d%5p%c {1}:%L - %m%n

rootLogger =debug,scrlog,stdout
logger .ProcessLogger =debug,scrlog
}


解决方案

<下面是一个简单的例子,它使用 @Grab 来引入 log4j 库,然后使用内置的Groovy的 @Log4j 注释来连接 log 变量。



然后,您可以配置记录器脚本并添加一个 FileAppender

  @Grab('log4j:log4j: 1.2.17')

import org.apache.log4j。*
import groovy.util.logging。*

@ Log4j
class HelloWorld {
def execute(){
//需要设置日志级别,如下所述:
// http://groovy.329449.n5.nabble.com/log4j-annotation-not- working-td4368806.html
log.level = Level.INFO
//添加一个appender记录到文件
log.addAppender(new FileAppender(new TTCCLayout(),'myscript.log'));

//这将不会打印/写入,因为loglevel是info
log.debug'执行HelloWorld'
//这将打印
log.info' '
}
}

def helloWorld = new HelloWorld()
helloWorld.execute()

运行时,您会在您的myscript.log中看到这一点

  11 [main] INFO HelloWorld  - 显示日志字段的简单示例被注入。 

不幸的是,config.groovy文件记录器配置是特定于Grails 。由于您不在Grails中,因此DSL不能解释log4j的内容。不过,请查看Groovy中的 ConfigSlurper 看起来它会为您提供一个类似的DSL来引入您的配置。


Been searching here and there looking for a working example of log4j logging to file in a Groovy script.

No explicit class (it's just a script). No grails. Not to console...to file.

Just a plain groovy script with log4j.

Is log4j best for this (groovy) or are other logging libraries better?

Can someone either point me to an example or demo how this is done? I managed to get it to console, but not to file.

Would be nice if the log4j configuration was in a config.groovy file as well, since I am using a config file for other things.

UPDATE

Thanks to neversleepz example, I have the following working nicely:

config.groovy file:

log4j {  

  appender.stdout = "org.apache.log4j.ConsoleAppender"    
  appender."stdout.layout"="org.apache.log4j.PatternLayout"    
  appender.scrlog = "org.apache.log4j.FileAppender"    
  appender."scrlog.layout"="org.apache.log4j.PatternLayout"
  appender."scrlog.layout.ConversionPattern"="%d %5p %c{1}:%L - %m%n"         
  appender."scrlog.file"="rootscript.log" 
  rootLogger = "debug,scrlog,stdout"         
}

And my script:

import org.apache.log4j.*
import groovy.util.logging.*   

def config = new ConfigSlurper().parse(new File('config.groovy').toURL())        
PropertyConfigurator.configure(config.toProperties())

Logger log = Logger.getInstance(getClass())

// Need to set log level as described here: 
// http://groovy.329449.n5.nabble.com/log4j-annotation-not-working-td4368806.html
log.level = Level.INFO

// this will NOT print/write as the loglevel is info
log.debug 'Executing Script.'
// this will print
log.info 'Simple sample to show log INFO field is injected.'
log.warn 'Simple sample to show log WARN field is injected.'
log.error 'Simple sample to show log ERR field is injected.'        

Thanks for this!

I've also configured for a RollingFileAppender, and DailyRollingFileAppender I'll put those here as well:

log4j {
  //   
  appender.stdout = "org.apache.log4j.ConsoleAppender"
  appender."stdout.layout"="org.apache.log4j.PatternLayout"
  // 
  appender.scrlog = "org.apache.log4j.DailyRollingFileAppender"
  appender."scrlog.DatePattern"="'.'yyyy-MM-dd"
  appender."scrlog.Append"="true"
  appender."scrlog.File"="rootscript.log"
  appender."scrlog.layout"="org.apache.log4j.PatternLayout"
  appender."scrlog.layout.ConversionPattern"="%d %5p %c{1}:%L - %m%n"

  rootLogger="debug,scrlog,stdout"
}

and

log4j {
  //   
  appender.stdout = "org.apache.log4j.ConsoleAppender"
  appender."stdout.layout"="org.apache.log4j.PatternLayout"
  // 
  appender.scrlog = "org.apache.log4j.DailyRollingFileAppender"
  appender."scrlog.DatePattern"="'.'yyyy-MM-dd"
  appender."scrlog.Append"="true"
  appender."scrlog.File"="rootscript.log"
  appender."scrlog.layout"="org.apache.log4j.PatternLayout"
  appender."scrlog.layout.ConversionPattern"="%d %5p %c{1}:%L - %m%n"

  rootLogger="debug,scrlog,stdout"
  logger.ProcessLogger="debug,scrlog"
}

解决方案

Here's a simple example using @Grab to pull in the log4j lib, then using the inbuilt Groovy's @Log4j annotation to wire in a log variable.

You can then configure your logger in the script and add a FileAppender

@Grab('log4j:log4j:1.2.17')

import org.apache.log4j.*
import groovy.util.logging.*

@Log4j
class HelloWorld{
    def execute() {
        // Need to set log level as described here: 
        // http://groovy.329449.n5.nabble.com/log4j-annotation-not-working-td4368806.html
        log.level = Level.INFO
        // add an appender to log to file
        log.addAppender(new FileAppender(new TTCCLayout(), 'myscript.log'));

        // this will NOT print/write as the loglevel is info
        log.debug 'Execute HelloWorld.'
        // this will print
        log.info 'Simple sample to show log field is injected.'
    }
}

def helloWorld = new HelloWorld()
helloWorld.execute()

When run you'll see this in your myscript.log

    11 [main] INFO HelloWorld - Simple sample to show log field is injected.

Unfortunately, the config.groovy file logger configuration is specific to Grails. Since you aren't in Grails, the DSL isn't there to interpret the log4j stuff. However take a look at ConfigSlurper in Groovy that looks like it will provide you a similar DSL to bring in your config.

这篇关于Groovy脚本和log4j的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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