logback - Logback 中的条件功能如何工作?

标签 logback janino

我使用这个logback配置文件:

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%-10.-10thread] %-5level %-30logger{1} - %msg%n</pattern>
    </encoder>
  </appender>

<if condition='isDefined("fileout-dir")'><then>
  <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
     <discriminator  class="edu.kit.sdq.storagebenchmarkharness.logging.SBHThreadDiscriminator"/>
    <sift>
      <appender name="FILE-${thread}" class="ch.qos.logback.core.FileAppender">
        <file>${fileout-dir:-}${thread}.log</file>
        <append>true</append>
        <layout class="ch.qos.logback.classic.PatternLayout">
          <pattern>%d{HH:mm:ss.SSS} %-5level %-30logger{1} - %msg%n</pattern>
        </layout>
      </appender>
    </sift>
  </appender>
  <root>
    <appender-ref>SIFT</appender-ref>
  </root>
 </then> </if>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

运行我的应用程序时,出现以下错误:

org.codehaus.commons.compiler.CompileException: Line 1, Column 45: A method named "isDefined" is not declared in any enclosing class nor any supertype, nor through a static import
    at org.codehaus.commons.compiler.CompileException: Line 1, Column 45: A method named "isDefined" is not declared in any enclosing class nor any supertype, nor through a static import
    at  at org.codehaus.janino.UnitCompiler.compileError(UnitCompiler.java:9014)
    at  at org.codehaus.janino.UnitCompiler.findIMethod(UnitCompiler.java:6549)
    at  at org.codehaus.janino.UnitCompiler.compileGet2(UnitCompiler.java:3429)
    at  at org.codehaus.janino.UnitCompiler.access$6300(UnitCompiler.java:104)
    at  at org.codehaus.janino.UnitCompiler$11.visitMethodInvocation(UnitCompiler.java:2869)
    at  at org.codehaus.janino.Java$MethodInvocation.accept(Java.java:2831)
    at  at org.codehaus.janino.UnitCompiler.compileGet(UnitCompiler.java:2890)
    at  at org.codehaus.janino.UnitCompiler.compileGetValue(UnitCompiler.java:3897)
    at  at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:1637)
    at  at org.codehaus.janino.UnitCompiler.access$1700(UnitCompiler.java:104)
    at  at org.codehaus.janino.UnitCompiler$5.visitReturnStatement(UnitCompiler.java:877)
    at  at org.codehaus.janino.Java$ReturnStatement.accept(Java.java:1803)
    at  at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:888)
    at  at org.codehaus.janino.UnitCompiler.compileStatements(UnitCompiler.java:914)
    at  at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:1999)
    at  at org.codehaus.janino.UnitCompiler.compileDeclaredMethods(UnitCompiler.java:789)
    at  at org.codehaus.janino.UnitCompiler.compileDeclaredMethods(UnitCompiler.java:770)
    at  at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:464)
    at  at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:357)
    at  at org.codehaus.janino.UnitCompiler$3.visitPackageMemberClassDeclaration(UnitCompiler.java:312)
    at  at org.codehaus.janino.Java$PackageMemberClassDeclaration.accept(Java.java:770)
    at  at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:319)
    at  at org.codehaus.janino.UnitCompiler.compileUnit(UnitCompiler.java:288)
    at  at org.codehaus.janino.SimpleCompiler.compileToClassLoader(SimpleCompiler.java:393)
    at  at org.codehaus.janino.ClassBodyEvaluator.compileToClass(ClassBodyEvaluator.java:311)
    at  at org.codehaus.janino.ClassBodyEvaluator.cook(ClassBodyEvaluator.java:224)
    at  at org.codehaus.janino.SimpleCompiler.cook(SimpleCompiler.java:194)
    at  at org.codehaus.commons.compiler.Cookable.cook(Cookable.java:80)
    at  at org.codehaus.commons.compiler.Cookable.cook(Cookable.java:72)
    at  at ch.qos.logback.core.joran.conditional.PropertyEvalScriptBuilder.build(PropertyEvalScriptBuilder.java:34)
    at  at ch.qos.logback.core.joran.conditional.IfAction.begin(IfAction.java:43)
    at  at ch.qos.logback.core.joran.spi.Interpreter.callBeginAction(Interpreter.java:273)
    at  at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:145)
    at  at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:127)
    at  at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:40)
    at  at ch.qos.logback.core.joran.spi.Interpreter.play(Interpreter.java:332)
    at  at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:126)
    at  at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:93)
    at  at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:52)
    at  at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:60)
    at  at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:121)
    at  at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:85)
    at  at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:55)
    at  at org.slf4j.LoggerFactory.bind(LoggerFactory.java:121)
    at  at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:111)
    at  at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:268)
    at  at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:241)
    at  at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:254)
    at  at edu.kit.sdq.storagebenchmarkharness.Logger.getLogger(Logger.java:44)
    at  at edu.kit.sdq.storagebenchmarkharness.BenchmarkController.<clinit>(BenchmarkController.java:66)

为什么方法isDefined是未知的? logback docs声明它应该存在,并且至少 one stack overflow线程似乎使用它。

我在类路径中包含了最新版本的 janino,因为表达式的求值需要它。正如您在错误消息中看到的那样,janino 编译器已加载。

最佳答案

确保您使用的是最新版本的 logback。另外,在logback中使用条件requires the Janino library .

将其添加到您的 pom.xml 文件中以获取依赖项:

    <!-- The org.codehaus.janino:commons-compiler:2.6.1 dependency -->
    <!-- will be automatically pulled in by Maven's transitivity rules -->
    <dependency>
        <groupId>org.codehaus.janino</groupId>
        <artifactId>janino</artifactId>
        <version>2.6.1</version>
    </dependency>

关于logback - Logback 中的条件功能如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11017148/

相关文章:

java - 是否可以从 Xtend 字符串动态生成 Java 代码字符串?

java - Scriptella:如何从 clob 对象动态获取数据,然后将其插入数据库?

logback - 将 XML 中的字符串与 Janino 进行比较时缺少关闭单引号

gradle - Logback筛选器-限制性错误日志消息

java - Logback线程池

java - 使用 SLF4J 桥接 JUL 不起作用

java - Spring Boot 和 Logback : Exception thrown from ApplicationListener handling ContextClosedEvent

logback - 如何防止logback发出重复的 "Empty watch file list. Disabling"消息

tomcat - 重新部署时使用 Janino + Logback + Tomcat 的 sun.reflect.MethodAccessorImpl 的 ClassNotFoundException