我一直在尝试在 Eclipse Equinox RT 应用程序中设置 logback 过滤器,如下所示:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<configuration debug="true">
<appender name="myappender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./logs/myapp.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>./logs/myapp_%i.log</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>10MB</maxFileSize>
</triggeringPolicy>
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator>
<expression>return message.contains("buy: foo") || message.contains("sell: bar");</expression>
</evaluator>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>%d{dd/MM/yyyy HH:mm:ss} | %-5level | %msg%n%ex{short}</pattern>
</encoder>
</appender>
<root level="ALL">
<appender-ref ref="myappender"/>
</root>
</configuration>
我在 gradle 子项目中有此配置,我将其称为“osgi-log”,以供参考。
plugins {
id 'java-library' version '1.0.0'
}
group 'myapp'
version '2.0.0'
dependencies {
api group: 'org.slf4j', name: 'slf4j-api', version: '2.0.5'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.3.5'
implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.3.5'
implementation group: 'org.codehaus.janino', name: 'janino', version: '3.1.12'
implementation group: 'org.ow2.asm', name: 'asm', version: '5.2'
implementation group: 'org.ow2.asm', name: 'asm-commons', version: '5.2'
implementation group: 'org.ow2.asm', name: 'asm-util', version: '5.2'
implementation group: 'org.apache.aries.spifly', name: 'org.apache.aries.spifly.dynamic.bundle', version: '1.3.5'
}
“osgi-log”正在其他 gradle 子项目中设置,如下所示:
dependencies
{
implementation group: 'myapp', name: 'osgi-log'
}
当我运行应用程序时,出现以下异常:
ERROR in ch.qos.logback.classic.boolex.JaninoEventEvaluator@75243b22 - Could not start evaluator with expression [return message.contains("buy: foo") || message.contains("sell: bar");] org.codehaus.commons.compiler.CompileException: Line 1, Column 1: A class "ch.qos.logback.classic.Level" could not be found
at org.codehaus.commons.compiler.CompileException: Line 1, Column 1: A class "ch.qos.logback.classic.Level" could not be found
观察:
-Dlogback.configurationFile=file:logback.xml
。任何其他建议或更简单的方法来实现相同的结果,将受到欢迎 - 我想过滤事件并只注册那些匹配某些字符组的事件。
加:
如果有人能帮助我理解为什么 Apache SpiFly 不能动态解析 Janino 实现的所有依赖项,我将不胜感激?
我实际上找到了解决办法。
这似乎是 Janino 依赖端的一个已知问题。
为了使其工作,需要调整
MANIFEST.MF
和 janino
中的 commons-compiler
添加以下行:
DynamicImport-Package: ch.qos.logback.*,org.slf4j
参考资料: