Groovy - 根据环境配置日志记录属性 [英] Groovy - configuring logging properties depending on environment
问题描述
我正在编写一个简单的Groovy应用程序,需要做一些日志记录。事物记录方式的实际属性取决于特定的环境。例如,在开发过程中,我只想登录控制台,在测试和生产过程中,我可能想要写入文件,而在生产中,我可能想发送电子邮件来处理最严重的事件。
现在我所做的是这样的:
import org.apache.log4j.Logger
import org.apache.log4j.PropertyConfigurator
$ b $ class BaseClass {
protected config
static Logger logger = Logger.getLogger(BaseClient.class)
def BaseClass(env){
def configFilePath = //任意
config = new JsonSlurper()。parseText(configFile.text)[options.env]
def logConfigFilePath = ['somelogdir',config.log_file] .join(File.separator)
PropertyConfigurator.configure(logConfigFilePath)
}
}
然后所有需要做日志记录的类继承自 BaseClass
。
通过这种方式,我可以指定每个环境都有一个不同的文件名,我可以从那里读取日志配置文件。但它似乎有很多样板,并且迫使我使用可能不理想的层次结构。另一方面, www.canoo.com/blog/2010/09/20/log-groovys-new-and-extensible-logging-conveniences/rel =nofollow>这里我看到我可以获得一个记录器简单的注释。
有没有一种方法可以根据环境获取不同的记录器 - 可以在运行时设置?
建立在tim_yates的答案上,使用groovy文件来配置log4j。例如:
// log4j.groovy
log4j {
rootLogger =DEBUG,A1
appender.A1 =org.apache.log4j.ConsoleAppender
appender.'A1.layout'=org.apache.log4j.PatternLayout
if(System.properties ['env'] =='prod'){
appender.'A1.layout.ConversionPattern'=prod%-4r [%t]%-5p%c%x - %m%n
$ else {
appender.'A1.layout.ConversionPattern'=dev%-4r [%t]%-5p%c%x - %m%n
}
}
然后让你的脚本使用它:
@GrabConfig(systemClassLoader = true)
@Grab(group ='log4j',module ='log4j',version ='1.2.17')
import groovy.util.logging.Log4j
import org.apache.log4j.PropertyConfigurator
@ Log4j
class Test {
def dosome(){
log.info('Logging!')
}
static main(args){
def config = new ConfigSlurper()。parse(n ew File('log4j.groovy').toURL())
PropertyConfigurator.configure(config.toProperties())
new Test()。dosome()
}
$ / code>
最后,用系统属性中的环境启动程序:
groovy -Denv = prod Test.groovy
I am writing a simple Groovy application that needs to do some logging. The actual properties of how things are logged will depend on the particular environment. For instance, while developing I just want to log to the console, in test and production I may want to write to a file and in production I may want to send email for the most severe events.
Right now what I am doing looks like this:
import org.apache.log4j.Logger
import org.apache.log4j.PropertyConfigurator
class BaseClass {
protected config
static Logger logger = Logger.getLogger(BaseClient.class)
def BaseClass(env) {
def configFilePath = // whatever
config = new JsonSlurper().parseText(configFile.text)[options.env]
def logConfigFilePath = ['somelogdir', config.log_file].join(File.separator)
PropertyConfigurator.configure(logConfigFilePath)
}
}
and then all my classes that need to do logging inherit from BaseClass
.
In this way, I can specify a different filename for each environment, and I get to read the logging configuratio from there. But it seems a lot of boilerplate, and forces me to use a hierarchy that may not be ideal.
On the other hand, here I see that I can obtain a logger with a simple annotation.
Is there a way to get different loggers depending on the environment - which may be set at runtime?
Building on tim_yates's answer, use a groovy file to configure log4j. For example:
// log4j.groovy
log4j {
rootLogger="DEBUG, A1"
appender.A1 = "org.apache.log4j.ConsoleAppender"
appender.'A1.layout' = "org.apache.log4j.PatternLayout"
if (System.properties['env'] == 'prod') {
appender.'A1.layout.ConversionPattern'="prod %-4r [%t] %-5p %c %x - %m%n"
} else {
appender.'A1.layout.ConversionPattern'="dev %-4r [%t] %-5p %c %x - %m%n"
}
}
Then make your script use it:
@GrabConfig(systemClassLoader=true)
@Grab(group='log4j', module='log4j', version='1.2.17')
import groovy.util.logging.Log4j
import org.apache.log4j.PropertyConfigurator
@Log4j
class Test {
def dosome() {
log.info('Logging!')
}
static main( args ) {
def config = new ConfigSlurper().parse(new File('log4j.groovy').toURL())
PropertyConfigurator.configure(config.toProperties())
new Test().dosome()
}
}
And finally, launch your program with the environment in a system property:
groovy -Denv=prod Test.groovy
这篇关于Groovy - 根据环境配置日志记录属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!