Skip to content

Commit

Permalink
✨ (web) 默认访问日志过滤器输出日志时的级别支持配置
Browse files Browse the repository at this point in the history
  • Loading branch information
Hccake committed Mar 6, 2024
1 parent 878b98f commit e39f88f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.ballcat.web.accesslog.AccessLogRule;
import org.ballcat.web.accesslog.DefaultAccessLogFilter;
import org.ballcat.web.accesslog.annotation.AccessLogRuleFinder;
import org.slf4j.event.Level;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand Down Expand Up @@ -54,17 +55,22 @@ public class AccessLogAutoConfiguration {
@Bean
@ConditionalOnMissingBean(AccessLogFilter.class)
public AccessLogFilter defaultAccessLogFilter() {
List<AccessLogRule> propertiesRules = this.accessLogProperties.getAccessLogRules();
Integer maxBodyLength = this.accessLogProperties.getMaxBodyLength();
Integer filterOrder = this.accessLogProperties.getFilterOrder();
Level defaultFilterLogLevel = this.accessLogProperties.getDefaultFilterLogLevel();
AccessLogRecordOptions defaultRecordOptions = this.accessLogProperties.getDefaultAccessLogRecordOptions();

// 合并 annotationRules 和 propertiesRules, 注解高于配置
List<AccessLogRule> annotationRules = AccessLogRuleFinder
.findRulesFormAnnotation(this.requestMappingHandlerMapping);
List<AccessLogRule> propertiesRules = this.accessLogProperties.getAccessLogRules();
List<AccessLogRule> accessLogRules = AccessLogRuleFinder.mergeRules(annotationRules, propertiesRules);

AccessLogRecordOptions defaultRecordOptions = this.accessLogProperties.getDefaultAccessLogRecordOptions();

AbstractAccessLogFilter accessLogFilter = new DefaultAccessLogFilter(defaultRecordOptions, accessLogRules);
accessLogFilter.setMaxBodyLength(this.accessLogProperties.getMaxBodyLength());
accessLogFilter.setOrder(this.accessLogProperties.getFilterOrder());
// 创建默认访问日志过滤器
AbstractAccessLogFilter accessLogFilter = new DefaultAccessLogFilter(defaultRecordOptions, accessLogRules,
defaultFilterLogLevel);
accessLogFilter.setMaxBodyLength(maxBodyLength);
accessLogFilter.setOrder(filterOrder);
return accessLogFilter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.ballcat.web.accesslog.AbstractAccessLogFilter;
import org.ballcat.web.accesslog.AccessLogRecordOptions;
import org.ballcat.web.accesslog.AccessLogRule;
import org.slf4j.event.Level;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
Expand All @@ -44,6 +45,11 @@ public class AccessLogProperties {
*/
private boolean enabled = false;

/**
* 默认的访问日志过滤器写日志时的级别
*/
private Level defaultFilterLogLevel = Level.DEBUG;

/**
* access log filter 的优先级
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,46 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.ballcat.common.util.IpUtils;
import org.slf4j.event.Level;

/**
* 默认的访问日志过滤器,使用 log 输出
*
* @author Hccake
*/
@Getter
@Setter
@Slf4j
public class DefaultAccessLogFilter extends AbstractAccessLogFilter {

public DefaultAccessLogFilter(AccessLogRecordOptions defaultRecordOptions, List<AccessLogRule> logRules) {
private Level logLevel;

public DefaultAccessLogFilter(AccessLogRecordOptions defaultRecordOptions, List<AccessLogRule> logRules,
Level logLevel) {
super(defaultRecordOptions, logRules);
this.logLevel = logLevel != null ? logLevel : Level.DEBUG;
}

@Override
protected boolean shouldLog(HttpServletRequest request) {
return log.isDebugEnabled();
switch (this.logLevel) {
case TRACE:
return log.isTraceEnabled();
case DEBUG:
return log.isDebugEnabled();
case INFO:
return log.isInfoEnabled();
case WARN:
return log.isWarnEnabled();
case ERROR:
return log.isErrorEnabled();
default:
return false;
}
}

@Override
Expand All @@ -60,7 +82,7 @@ protected void beforeRequest(HttpServletRequest request, AccessLogRecordOptions

msg.append("]");

log.debug(msg.toString());
writeLogWithSpecialLevel(msg);
}

@Override
Expand Down Expand Up @@ -97,7 +119,30 @@ protected void afterRequest(HttpServletRequest request, HttpServletResponse resp

msg.append("]");

log.debug(msg.toString());
writeLogWithSpecialLevel(msg);
}

/**
* 根据指定的日志级别进行日志输出。
*/
private void writeLogWithSpecialLevel(StringBuilder msg) {
switch (this.logLevel) {
case TRACE:
log.trace(msg.toString());
break;
case DEBUG:
log.debug(msg.toString());
break;
case INFO:
log.info(msg.toString());
break;
case WARN:
log.warn(msg.toString());
break;
case ERROR:
log.error(msg.toString());
break;
}
}

}

0 comments on commit e39f88f

Please sign in to comment.