如何使用groovy通过logback.groovy配置logback [英] how to use logback configured via logback.groovy with groovy

查看:207
本文介绍了如何使用groovy通过logback.groovy配置logback的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果在一个目录中我有一个脚本文件叫做例如FooBar.groovy,而这个脚本文件叫做例如FooBar.groovy,文件logback.groovy
当我运行 groovy FooBar.groovy groovy也尝试编译logback配置文件,我的脚本也无法运行,我必须回退到用于logback(logback.xml)的默认xml配置文件。



我如何使此功能起作用?我可以调用 groovy somefiles.groovy 并配置groovy忽略logback.groovy?



感谢您的帮助 p>

解决方案

你的问题的原因是不应该编译logback配置文件。它在运行时从LogBack通过 GroovyShell 或类似的机制读取。



解决方案取决于您的项目设置。以下您将会看到Maven Gradle 的项目构建解决方案。 maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.htmlrel =noreferrer>标准目录布局:

第一个文件是 src / main / groovy / Test.groovy
$ b

  import org.slf4j.Logger 
import org.slf4j.LoggerFactory
$ b $ class Test {
static Logger LOG = LoggerFactory.getLogger(Test .class)

static void main(String [] args){
LOG.debug(Test)
}
}

第二个文件是 src / main / resources / logback.groovy



  import static ch.qos.logback.classic.Level.INFO 
import static ch。 qos.logback.classic.Level.DEBUG

导入ch.qos.logback.classic.encoder.Pat ternLayoutEncoder
导入ch.qos.logback.core.ConsoleAppender

appender(CONSOLE,ConsoleAppender){
encoder(PatternLayoutEncoder){
pattern =% 4relative [%thread] - %msg%n
}
}
root(DEBUG,[CONSOLE])

我省略了Gradle构建文件( build.gradle )。



标准目录布局保证编译 src / main / groovy 中的每个文件,而 src / main / resources code>包含在类路径中。因此,LogBack能够在运行时找到该文件。



更新:没有仔细阅读您的问题。当两个文件都在同一个目录下时,我无法解决问题,并通过 groovy Test.groovy 来启动它。我认为这是不可能的。 groovy命令总是编译当前目录和给定类路径中的所有groovy文件。


I am not able to use logback configured via logback.groovy with groovy.

If in a directory i have a script file called for example FooBar.groovy and the file logback.groovy when i run groovy FooBar.groovy groovy tries to compiles the logback configuration file too and my script doesn't work, i have to fallback to the default xml configuration file for logback (logback.xml).

How can i make this to works? can i call groovy somefiles.groovy and configure groovy to ignore logback.groovy?

Thanks for the help

解决方案

The reason for your problems is that the logback configuration file should never be compiled. It is read at runtime from LogBack via GroovyShell or a similar mechanism.

The solution depends on your project setup. Following you will find a solution for a project build with Gradle following the Maven Standard Directory Layout:

First file is src/main/groovy/Test.groovy:

import org.slf4j.Logger
import org.slf4j.LoggerFactory

class Test {
    static Logger LOG = LoggerFactory.getLogger(Test.class)

    static void main(String[] args) {
        LOG.debug("Test")
    }
}

Second file is src/main/resources/logback.groovy:

import static ch.qos.logback.classic.Level.INFO
import static ch.qos.logback.classic.Level.DEBUG

import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.core.ConsoleAppender

appender("CONSOLE", ConsoleAppender) {
  encoder(PatternLayoutEncoder) {
    pattern = "%-4relative [%thread] - %msg%n"
  }
}
root(DEBUG, ["CONSOLE"])

I omitted the Gradle build file (build.gradle).

The standard directory layout guarantees that every file in src/main/groovy is compiled, while everything in src/main/resources is included in the class path. So LogBack is able to find that file at runtime.

Update: Didn't read your problem carefully enough. I could not manage to solve the problem when both files are in the same directory and I start it via groovy Test.groovy. I think this is not possible. The groovy command always compiles all groovy files in the current directory and in the given classpath.

这篇关于如何使用groovy通过logback.groovy配置logback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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