我们在日常工作中,会遇见链路过长、日志量过大、构建数据复杂、复现困难等痛点。我们期望有一种解决方案:可以对规则加白,命中规则的日志会打印出来,被ELK收集,但是可以尽量不影响我们的业务逻辑,可以借助日志的Filter来实现:
实现方式
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
public class MyThresholdFilter extends Filter<ILoggingEvent> {
@Override
public FilterReply decide(ILoggingEvent event) {
if ("com.tellme.controller.liteFlowController".equals(event.getLoggerName())) {
return FilterReply.ACCEPT;
} else {
return FilterReply.DENY;
}
}
}
使用方式:
<property name="core.path" value="core.log"/>
<!--特定日志:输出到文件-->
<appender name="file1" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="com.tellme.logfilter.MyThresholdFilter"/>
<file>${core.path}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>coreback.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<!--格式化输出:%d:表示日期 %thread:表示线程名 %-5level:级别从左显示5个字符宽度 %msg:日志消息 %n:是换行符-->
<pattern>文件记录-%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="file1"/>
</root>
- 我们在日志收集的时候,可以收集debug级别的详细日志;
扩展:我们可以借助Spring的机制,动态加载Filter过滤器
src/main/resources/META-INF/spring.factories类
org.springframework.context.ApplicationListener=\
com.tellme.starter.DynamicLogTurboApplicationListener
监听类,
import com.tellme.logfilter.DynamicLogTurboFilter;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.GenericApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.ConfigurableEnvironment;
public class DynamicLogTurboApplicationListener implements GenericApplicationListener {
public static final int DEFAULT_ORDER = Ordered.LOWEST_PRECEDENCE;
private static final Class<?> EVENT_TYPE = ApplicationPreparedEvent.class;
private int order = DEFAULT_ORDER;
@Override
public boolean supportsEventType(ResolvableType eventType) {
if (eventType.getRawClass() == null) {
return false;
}
return EVENT_TYPE.isAssignableFrom(eventType.getRawClass());
}
@Override
public boolean supportsSourceType(Class<?> sourceType) {
return true;
}
@Override
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
ConfigurableEnvironment environment = ((ApplicationPreparedEvent) event).getApplicationContext().getEnvironment();
DynamicLogTurboFilter.addFilterToContext(false);
}
}
加载到log工厂中:
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.TurboFilterList;
import ch.qos.logback.classic.turbo.TurboFilter;
import ch.qos.logback.core.spi.FilterReply;
import org.slf4j.ILoggerFactory;
import org.slf4j.Marker;
import org.slf4j.impl.StaticLoggerBinder;
public class DynamicLogTurboFilter extends AbstractTurboFilter {
private static final ThreadLocal<Boolean> LOOP_CHECK = ThreadLocal.withInitial(() -> Boolean.FALSE);
DynamicLogTurboFilter(boolean skipWriteFile) {
setName("DynamicLogTurboFilter");
setSkipWriteFile(skipWriteFile);
}
@Override
protected FilterReply doDecide(Marker marker, Logger logger, Level level, String format,
Object[] params, Throwable t) {
if ("com.tellme.controller.liteFlowController".equals(logger.getName())) {
return FilterReply.ACCEPT;
} else {
return FilterReply.DENY;
}
}
@Override
protected void clearInLoopMark() {
LOOP_CHECK.remove();
}
@Override
protected void markInLoop() {
LOOP_CHECK.set(Boolean.TRUE);
}
@Override
protected boolean checkInLoop() {
return Boolean.TRUE.equals(LOOP_CHECK.get());
}
public static synchronized void addFilterToContext(boolean skipWriteFile) {
ILoggerFactory loggerFactory = StaticLoggerBinder.getSingleton().getLoggerFactory();
if (loggerFactory instanceof LoggerContext) {
TurboFilterList turboFilterList = ((LoggerContext) loggerFactory).getTurboFilterList();
for (TurboFilter turboFilter : turboFilterList) {
if (turboFilter.getClass().equals(DynamicLogTurboFilter.class)) {
return;
}
}
((LoggerContext) loggerFactory).addTurboFilter(new DynamicLogTurboFilter(skipWriteFile));
}
}
}
监听父类:
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.turbo.TurboFilter;
import ch.qos.logback.core.spi.FilterReply;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
public abstract class AbstractTurboFilter extends TurboFilter {
private static final Marker SKIP_RATE_MARKER = MarkerFactory.getMarker("SKIP_RATE_MARKER");
private boolean skipWriteFile = false;
@Override
public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
long start = System.currentTimeMillis();
if (checkInLoop()) {
return FilterReply.NEUTRAL;
}
markInLoop();
try {
// 如果跳过写文件, 且调用方不是从 SKIP_RATE_MARKER 过来则不用走这个filter了
if (skipWriteFile && !SKIP_RATE_MARKER.equals(marker)) {
return FilterReply.NEUTRAL;
}
return doDecide(marker, logger, level, format, params, t);
} catch (Exception e) {
} finally {
clearInLoopMark();
}
return FilterReply.NEUTRAL;
}
protected abstract FilterReply doDecide(Marker marker, Logger logger, Level level, String format,
Object[] params, Throwable t);
protected abstract void clearInLoopMark();
protected abstract void markInLoop();
protected abstract boolean checkInLoop();
public AbstractTurboFilter setSkipWriteFile(boolean skipWriteFile) {
this.skipWriteFile = skipWriteFile;
return this;
}
}