我想在我的应用程序(Spring Integration),debug.log和main.log中有两个日志文件。我想在INFO级别运行main.log,在DEBUG级别运行debug.log。这对于appender上的过滤器是可行的。我想根据源记录不同级别的appender。换句话说
<logger name="org.springframework" level="ERROR">
<appender-ref ref="main" />
</logger>
<logger name="org.springframework" level="DEBUG">
<appender-ref ref="debug" />
</logger>
<logger name="com.myapp" level="INFO">
<appender-ref ref="main" />
</logger>
<logger name="com.myapp" level="DEBUG">
<appender-ref ref="debug" />
</logger>
总结一下:
因此我必须让记录器在DEBUG上运行,并且appender上的阈值过滤器不够精细。</ p>
更新为问题添加了清晰度
答案 0 :(得分:50)
创建一个ThresholdLoggerFilter类,可以放在一个appender上,如:
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<filter class="com.myapp.ThresholdLoggerFilter">
<logger>org.springframework</logger>
<level>ERROR</level>
</filter>
</appender>
以下代码可以使用
package com.myapp;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
public class ThresholdLoggerFilter extends Filter<ILoggingEvent> {
private Level level;
private String logger;
@Override
public FilterReply decide(ILoggingEvent event) {
if (!isStarted()) {
return FilterReply.NEUTRAL;
}
if (!event.getLoggerName().startsWith(logger))
return FilterReply.NEUTRAL;
if (event.getLevel().isGreaterOrEqual(level)) {
return FilterReply.NEUTRAL;
} else {
return FilterReply.DENY;
}
}
public void setLevel(Level level) {
this.level = level;
}
public void setLogger(String logger) {
this.logger = logger;
}
public void start() {
if (this.level != null && this.logger != null) {
super.start();
}
}
}
答案 1 :(得分:16)
如果您愿意从根记录器继承,例如,您也可以更简单地执行此操作。这里我们为错误添加了一个额外的记录器,记录到stderr。它仅适用于特定记录器。
<configuration>
<appender name="CONSOLE-stdout" class="ch.qos.logback.core.ConsoleAppender">
<target>System.out</target>
<encoder>
<pattern>%d %-5level [%thread] %logger{0}: %msg%n</pattern>
</encoder>
</appender>
<appender name="CONSOLE-stderr" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<target>System.err</target>
<encoder>
<pattern>%d %-5level [%thread] %logger{0}: %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="CONSOLE-stdout" />
</root>
<!-- We want error logging from this logger to go to an extra appender
It still inherits CONSOLE-stdout from the root logger -->
<logger name="org.springframework" level="INFO">
<appender-ref ref="CONSOLE-stderr" />
</logger>
</configuration>
答案 2 :(得分:3)
这些解决方案都不适合我,因为我没有使用像Spark或Spring这样的框架。所以我做了一些更简单的事情,看起来功能很好。虽然这个解决方案可能对OP不起作用,但对于那些想要不那么笨重的人来说,这可能是有用的。
<property name="pattern" value="%d{yyyy.MMM.dd HH:mm:ss.SSS} [ProgramName] %level - %msg%n" />
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/path/to/your/program.log</file>
<append>true</append>
<encoder>
<pattern>${pattern}</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<target>System.out</target>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<encoder>
<pattern>${pattern}</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
使用这种配置,我可以保持控制台相当干净,同时将DEBUG语句输出到日志文件。
答案 3 :(得分:2)
刚刚找到了一个使用logback元素的实用解决方案,但效果非常好,基本上你需要有两个appender,一个带有默认配置,另一个带有过滤器(在我的例子中我使用的是控制台):< / p>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="WARN_FILTER_STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.apache.spark" level="INFO" additivity="false">
<appender-ref ref="SPARK" /><!-- this line is not necessary, just here to ilustrate the need for the filter -->
<appender-ref ref="WARN_FILTER_STDOUT" />
</logger>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
答案 4 :(得分:0)
对不同的消息使用多个记录器将是这样的:
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.FileAppender;
public class ConfigureLogBack
{
public static void programmaticConfiguration()
{
Logger camel = getLogger("MyRoute", C:\\Users\\amrut.malaji\\Desktop\\Oracle\\logback\\camel-Log.txt");
Logger services = getLogger("webservices", "C:\\Users\\amrut.malaji\\Desktop\\Oracle\\logback\\services-log.txt");
}
private static Logger getLogger(String string, String file) {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
PatternLayoutEncoder ple = new PatternLayoutEncoder();
ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
ple.setContext(lc);
ple.start();
FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
fileAppender.setFile(file);
fileAppender.setEncoder(ple);
fileAppender.setContext(lc);
fileAppender.start();
Logger logger = (Logger) LoggerFactory.getLogger(string);
logger.addAppender(fileAppender);
logger.setLevel(Level.INFO);
logger.setAdditive(false); /* set to true if root should log too */
return logger;
}
答案 5 :(得分:0)
追加器上的阈值过滤器的粒度不够细
您可以使用EvaluatorFilter。 JaninoEventEvaluator将需要对janino(.jar)的引用,而logback.xml示例将是:
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">
<expression>
level <= ERROR && logger.equals("com.myapp.ThresholdLoggerFilter")
</expression>
</evaluator>
<OnMismatch>DENY</OnMismatch>
<OnMatch>NEUTRAL</OnMatch>
</filter>
</appender>
此方法在expression标记中使用Java表达式(必须转义xml)来评估日志记录事件,并且不需要编写自定义Java类。