本文不介紹細節(jié)沈跨,只是將實際應(yīng)用中的日志配置量九,在這里做個記錄黍檩,方便自己大家學(xué)習(xí)借鑒
服務(wù)中的日志配置一直都是開發(fā)中比較重要的部分申钩。先看下我認為的日志分類:
-
access訪問日志:
調(diào)用接口的時候次绘,對于每一個調(diào)用都記錄一下訪問日志。方便后面的請求追蹤 - 常規(guī)日志:代碼中的日志,就是我們在代碼中打印的各種日志
-
性能日志:性能日志是為了方便優(yōu)化代碼邮偎,監(jiān)控性能的日志管跺。在我所在的公司記錄了三種有關(guān)于性能的日志
-
controller性能日志
記錄在Controller
層次的放溫暖耗時信息 -
service性能日志
記錄調(diào)用Service
方法中的訪問耗時信息 -
dao性能日志
記錄dao層
訪問數(shù)據(jù)庫的耗時信息
-
下面介紹兩種實現(xiàn)日志記錄的方式:
- 第一種:
Spring Sleuth
+Logback
- 借助于
Spring Cloud
組件Spring sleuth
實現(xiàn)分布式服務(wù)追蹤,使用 Logback 定制日志格式禾进, - 這種方式這里主要是配置了 常規(guī)日志 與 access日志豁跑。性能日志我不知道能不能配置,不知道能實現(xiàn)不泻云?這里就不說了
- 借助于
- 第二種:
AOP
+Inteceptor
+Logback
- 使用攔截器攔截請求艇拍,打印
access日志
, 目的是為了生成能夠追蹤常規(guī)日志的traceId
- 使用 AOP 監(jiān)控
controller
宠纯、service
卸夕、dao
層中的方法,提取性能日志需要的時間內(nèi)信息
- 使用攔截器攔截請求艇拍,打印
1. Spring Sleuth + Logback 實現(xiàn)
Spring Sleuth
是 Spring Cloud
提供的分布式服務(wù)追蹤框架征椒,使用這個框架可以很方便的打印日志娇哆。通過 traceId
與 spanId
還可以在分布式服務(wù)中方便追蹤請求.
Spring boot 版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/>
</parent>
依賴 jar 包:
<dependency>
<groupId>net.rakugakibox.spring.boot</groupId>
<artifactId>logback-access-spring-boot-starter</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
logback 配置:
環(huán)境變量配置:
SpringBoot
自動映射了一些值到 Logback
。如:
logging.path --> LOG_PATH
logging.file --> LOG_FILE
spring.application.name=tp-forum-api
# Log
logging.path: ${LOG_PATH:${LOG_TEMP:logs}}
logging.file=${logging.path}/app.log
- logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--添加配置文件-->
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<!--獲取 spring 配置中的 key 為 spring.application.name 的 value 值-->
<springProperty scope="context" name="applicationName" source="spring.application.name"/>
<!--property 用于定義變量勃救。log 在日志中輸出位置-->
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
<!--appender打印配置-->
<!--RollingFileAppender:每天輸出到一個新的日志文件-->
<!--TimeBasedRollingPolicy:根據(jù)時間進行滾動碍讨。設(shè)置日志總大小為1G,時間只保存30天內(nèi)的日志-->
<appender name="APP-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history capped at 3GB total size -->
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<!--date | level | application name, traceId, SpanId, SpanExport | thread | logger | %F.%M:%L |msg -->
<!-- :- 表示前面不存在蒙秒,就顯示 - -->
<pattern>[%date{ISO8601}]|%-5level|[${applicationName},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-}%X{X-Span-Export:-}]|[%thread]|%logger{0}|%F.%M:%L|%msg%n</pattern>
</encoder>
</appender>
<!--日志打印 logger-->
<logger name="org.springframework.web" level="INFO"/>
<!--根 logger,沒有指定logger時的默認值-->
<root level="INFO">
<appender-ref ref="APP-LOG" />
</root>
</configuration>
按照下面的配置勃黍,常規(guī)日志打印出來的格式應(yīng)該如下:
對應(yīng)的格式是:
data | level | application name, traceId, SpanId, SpanExport | thread | logger | %F.%M:%L |msg
[2019-01-15 19:44:44,608]|WARN |[tp-forum-api,903df707120b2927,903df707120b2927,false]|[http-nio-8080-exec-1]|HelloWrold|HelloWrold.java.hello:21|This is a warn message
[2019-01-15 19:44:44,609]|INFO |[tp-forum-api,903df707120b2927,903df707120b2927,false]|[http-nio-8080-exec-1]|HelloWrold|HelloWrold.java.hello:22|This is a info message
[2019-01-15 19:44:44,609]|ERROR|[tp-forum-api,903df707120b2927,903df707120b2927,false]|[http-nio-8080-exec-1]|HelloWrold|HelloWrold.java.hello:23|This is a error message
logback-access-spring.xml
<configuration>
<!-- always a good activate OnConsoleStatusListener -->
<!-- 監(jiān)聽Logback狀態(tài)變化,立即輸出到控制臺 -->
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
<!-- properties 配置 -->
<springProperty scope="context" name="applicationName" source="spring.application.name"/>
<!--配置的目錄-->
<property name="ACCESS_LOG_FILE" value="${ACCESS_LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/access.log}"/>
<!--appender規(guī)則-->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${ACCESS_LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${ACCESS_LOG_FILE}.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>[%t{ISO8601}]|[${applicationName},%i{X-B3-TraceId},%i{X-B3-SpanId:-},%i{X-Span-Export:-}]|%A|%h|%i{X-Real-IP}|%u|"%r"|%s|%b|%D|"%i{Referer}"|"%i{User-Agent}"</pattern>
</encoder>
</appender>
<appender-ref ref="FILE" />
</configuration>
下面是打印的情況:
可以看出 TraceId
與 SpanId
沒有打印出來,猜測是因為access日志打印的時候晕讲,還沒有生成該Id覆获。因此沒有好的方法讓access
日志與常規(guī)代碼日志對應(yīng)起來模狭,所以后來沒有采用該方法小压。
[2019-01-15 19:44:44,640]|[tp-forum-api,-,-,-]|10.2.202.203|0:0:0:0:0:0:0:1|-|-|"GET /hello HTTP/1.1"|200|5|96|"-"|"PostmanRuntime/7.4.0"
AOP + Inteceptor + Logback
- 環(huán)境配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath />
</parent>
- 依賴
jar
包
<dependency>
<groupId>org.perf4j</groupId>
<artifactId>perf4j</artifactId>
<version>0.9.16</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
- logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--常量。定義文件的輸出位置-->
<property name="log.dir" value="logs"/>
<!--日志最大的歷史 30 天-->
<property name="maxHistory" value="30"/>
<!--添加配置文件-->
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<!--添加Logger范圍內(nèi)的全局變量-->
<springProperty scope="context" name="applicationName" source="spring.application.name"/>
<!--文件存放的位置绵疲。':' 代表前面不存在就取后面的-->
<!--LOG_FILE 對應(yīng)配置文件中 logging.file LOG_PATH 對應(yīng) logging.path -->
<property name="LOG_FILE"
value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
<!-- 默認定義,輸出到文件 LOG_FILE -->
<appender name="app" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history capped at 3GB total size -->
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>
[%d{yyyy-MM-dd-HH:mm:ss}]|%-5level|%X{X-B3-TraceId:-}|[%thread]|%logger{36}.%M:%L| %msg%n
</pattern>
</encoder>
</appender>
<!-- 輸出到 Console 控制臺 -->
<appender name="STDOUT_CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<Target>System.out</Target>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-40.40c [%5.5thread] %-5p - %m%n</pattern>
</encoder>
</appender>
<!--定義 access 日志的地址-->
<property name="ACCESS_LOG_FILE"
value="${ACCESS_LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/access/access.log}"/>
<!-- 定義 access 訪問日志勤婚,輸出到 ACCESS_LOG_FILE -->
<appender name="accesslog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${ACCESS_LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${ACCESS_LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>
%d{yyyy-MM-dd-HH:mm:ss} %m %n
</pattern>
</encoder>
</appender>
<!-- service 性能日志 -->
<appender name="statFileAppender_service"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log.dir}/perf_service/perfStats.log.%d{yyyy-MM-dd}.log
</FileNamePattern>
<maxHistory>${maxHistory}</maxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<!-- controller 性能日志 -->
<appender name="statFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log.dir}/perf/perfStats.log.%d{yyyy-MM-dd}.log</FileNamePattern>
<maxHistory>${maxHistory}</maxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<!--DAO 性能日志-->
<appender name="statFileAppender_dao"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log.dir}/perf_dao/perfStats.log.%d{yyyy-MM-dd}.log
</FileNamePattern>
<maxHistory>${maxHistory}</maxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<!--配置周期匯總-->
<!--service-->
<appender name="CoalescingStatistics_Service"
class="org.perf4j.logback.AsyncCoalescingStatisticsAppender">
<!-- TimeSlice配置多少時間間隔去做一次匯總寫入文件中 -->
<timeSlice>10000</timeSlice>
<appender-ref ref="statFileAppender_service"/>
</appender>
<!--Controller-->
<appender name="CoalescingStatistics"
class="org.perf4j.logback.AsyncCoalescingStatisticsAppender">
<!-- TimeSlice配置多少時間間隔去做一次匯總寫入文件中 默認值是 30000 ms -->
<timeSlice>10000</timeSlice>
<appender-ref ref="statFileAppender"/>
</appender>
<!--DAO-->
<appender name="CoalescingStatistics_dao"
class="org.perf4j.logback.AsyncCoalescingStatisticsAppender">
<!-- TimeSlice配置多少時間間隔去做一次匯總寫入文件中 默認值是 30000 ms -->
<timeSlice>10000</timeSlice>
<appender-ref ref="statFileAppender_dao"/>
</appender>
<!-- access performance logger -->
<logger name="access_log" additivity="false">
<level value="info"/>
<appender-ref ref="accesslog"/>
</logger>
<!-- Service performance logger -->
<logger name="service.perf.logger" level="info" additivity="false">
<appender-ref ref="CoalescingStatistics_Service"/>
</logger>
<!-- Controller performance logger -->
<logger name="org.perf4j.TimingLogger" level="info" additivity="false">
<appender-ref ref="CoalescingStatistics"/>
</logger>
<!-- dao performance log -->
<logger name="dao.perf.logger" level="info" additivity="false">
<appender-ref ref="CoalescingStatistics_dao"/>
</logger>
<!-- org.springframework包下日志-->
<logger name="org.springframework" level="info" additivity="true">
<appender-ref ref="app"/>
<appender-ref ref="STDOUT_CONSOLE"/>
</logger>
<!-- 根 logger摹量。默認 logger -->
<root level="info">
<appender-ref ref="STDOUT_CONSOLE"/>
<appender-ref ref="app"/>
</root>
<!-- org.springframework.web 包下日志-->
<logger name="org.springframework.web" level="info"/>
</configuration>
完成上面的配置,應(yīng)該是只能打印出常規(guī)日志馒胆。性能日志與access
日志還不能打印出來缨称。
為了打印出性能,日志需要結(jié)合上面的配置 logger
與 監(jiān)聽文件的AOP切面
祝迂,來提取響應(yīng)時間.
- 配置性能日志的切面:
service
的性能日志切面:
@Aspect
@Component
public class Perf4jLogAspectService {
//與 logback-spring.xml 中的 name 保持一致
private static final String STOP_WATCH_LOGGER_NAME = "service.perf.logger";
private static final Log STOP_WATCH_LOGGER = LogFactory.getLog(STOP_WATCH_LOGGER_NAME);
//環(huán)繞監(jiān)聽 service文件下的方法調(diào)用
@Around("execution(public * com.renren.truckerpathapi.service..*.*(..))")
public Object logPer4jLogs(ProceedingJoinPoint point) throws Throwable {
String className = point.getSignature().getDeclaringTypeName();
String methodName = point.getSignature().getName();
String name = className + "." + methodName;
StopWatch stopwatch = new CommonsLogStopWatch(STOP_WATCH_LOGGER);
final Object returnValue = point.proceed();
stopwatch.stop(name);
return returnValue;
}
}
controller
睦尽、dao
層的類似:
@Aspect
@Component
public class Perf4jLogAspectController {
private static final String STOP_WATCH_LOGGER_NAME = "org.perf4j.TimingLogger";
private static final Log STOP_WATCH_LOGGER = LogFactory.getLog(STOP_WATCH_LOGGER_NAME);
@Around("execution(public * com.renren.truckerpathapi.controller..*.*(..))")
public Object logPer4jLogs(ProceedingJoinPoint point) throws Throwable {
String className = point.getSignature().getDeclaringTypeName();
String methodName = point.getSignature().getName();
String name = className + "." + methodName;
StopWatch stopwatch = new CommonsLogStopWatch(STOP_WATCH_LOGGER);
final Object returnValue = point.proceed();
stopwatch.stop(name);
return returnValue;
}
}
@Component
@Aspect
public class Perf4jLogAspectDao {
private static final String STOP_WATCH_LOGGER_NAME = "dao.perf.logger";
private static final Log STOP_WATCH_LOGGER = LogFactory.getLog(STOP_WATCH_LOGGER_NAME);
@Around("execution(public * com.renren.truckerpathapi.dao..*.*(..))")
public Object logPer4jLogs(ProceedingJoinPoint point) throws Throwable {
String className = point.getSignature().getDeclaringTypeName();
String methodName = point.getSignature().getName();
String name = className + "." + methodName;
StopWatch stopwatch = new CommonsLogStopWatch(STOP_WATCH_LOGGER);
final Object returnValue = point.proceed();
stopwatch.stop(name);
return returnValue;
}
}
對于access日志
,還記得上面所說的問題嗎型雳?Spring Sleuth
在 access
中不能生成 traceId
的問題当凡。
這里采用的處理方式山害,是自己添加一個請求攔截器,在請求返回時進行攔截(無論成功失敗宁玫,請求都是會返回的)粗恢,調(diào)用 logback-spring.xm
l 中配置的 logger
進行打印。實現(xiàn)如下:
AccessLogInterceptor.java
@Component
public class AccessLogInterceptor implements HandlerInterceptor {
@Autowired
private GetUserByTokenService getUserByTokenService;
private static final Log LOGGER = LogFactory.getLog(AccessLogInterceptor.class);
private static final Log ACCESS_LOG = LogFactory.getLog("access_log");
private static final String SEPARATOR = "|";
private static final String EMPTY_STR = "";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) {
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) {
//domain & uri & refer
String domain = request.getServerName();
String uri = request.getRequestURI();
String refer = request.getHeader("Referer");
final String token = request.getHeader("x-auth-token");
final String installation_ID = request.getHeader("Installation-ID");
final Users loginUser = getUserByTokenService.getUserByToken(token);
//traceId
String traceId = request.getHeader("X-B3-TraceId");
if (StringUtils.isEmpty(traceId)) {
traceId = UUID.randomUUID().toString();
}
MDC.put("X-B3-TraceId", traceId);
String remoteIp = getRemoteAddr(request);
String userId = loginUser == null ? installation_ID : String.valueOf(loginUser.getId());
String userName = loginUser == null ? "" :
loginUser.getFirstName() + " " + loginUser.getLastName();
String userAgent = request.getHeader("User-Agent");
Map<String, String> paramPair = getRequestParamValueMap(request);
printAccesslog(traceId, remoteIp, userId, userName, domain, uri, refer, userAgent, paramPair);
}
//打印access log
private void printAccesslog(String traceId, String remoteIp, String userId, String userName,
String domain, String uri, String refer,
String userAgent, Map<String, String> paramPair) {
StringBuilder sb = new StringBuilder();
long timestamp = System.currentTimeMillis();
sb.append(SEPARATOR).append(timestamp);
sb.append(SEPARATOR).append(remoteIp);
sb.append(SEPARATOR).append(userId);
sb.append(SEPARATOR).append(userName);
//RequestId用于定位access log與業(yè)務(wù)log
sb.append(SEPARATOR).append(traceId);
sb.append(SEPARATOR).append(domain);
sb.append(SEPARATOR).append(uri);
sb.append(SEPARATOR).append(refer);
sb.append(SEPARATOR).append(userAgent);
//將參數(shù)map打印成json格式眷射,利于統(tǒng)計分析
sb.append(SEPARATOR).append(JsonUtils.toJson(paramPair));
ACCESS_LOG.info(sb.toString());
}
private String getRemoteAddr(HttpServletRequest request) {
if (LOGGER.isDebugEnabled()) {// 打印所有日志
LOGGER.debug("X-Forwarded-For:" + request.getHeader
("X-Forwarded-For") +
"\tProxy-Client-IP:" + request.getHeader("Proxy-Client-IP") +
"\t:WL-Proxy-Client-IP:" +
request.getHeader("WL-Proxy-Client-IP") + "\tRemoteAddr:" + request
.getRemoteAddr());
}
String ip;
@SuppressWarnings("unchecked")
Enumeration<String> xffs = request.getHeaders("X-Forwarded-For");
if (xffs.hasMoreElements()) {
String xff = xffs.nextElement();
ip = resolveClientIPFromXFF(xff);
if (isValidIP(ip)) {
return ip;
}
}
ip = request.getHeader("Proxy-Client-IP");
if (isValidIP(ip)) {
return ip;
}
ip = request.getHeader("WL-Proxy-Client-IP");
if (isValidIP(ip)) {
return ip;
}
return request.getRemoteAddr();
}
/**
* 從X-Forwarded-For頭部中獲取客戶端的真實IP佛掖。 X-Forwarded-For并不是RFC定義的標準HTTP請求Header
* ,可以參考http://en.wikipedia.org/wiki/X-Forwarded-For
*
* @param xff X-Forwarded-For頭部的值
* @return 如果能夠解析到client IP芥被,則返回表示該IP的字符串,否則返回null
*/
private String resolveClientIPFromXFF(String xff) {
if (xff == null || xff.isEmpty()) {
return null;
}
String[] ss = xff.split(",");
for (int i = ss.length - 1; i >= 0; i--) {// x-forward-for鏈反向遍歷
String ip = ss[i].trim();
if (isValidIP(ip)) {
return ip;
}
}
return null;
}
private static final Pattern ipPattern = Pattern.compile("([0-9]{1,3}\\.){3}[0-9]{1,3}");
private boolean isValidIP(String ip) {
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
return false;
}
return ipPattern.matcher(ip).matches();
}
private Map<String, String> getRequestParamValueMap(HttpServletRequest request) {
Map<String,String> param2value = new HashMap<>();
Enumeration e = request.getParameterNames();
String param;
while(e.hasMoreElements()) {
param = (String)e.nextElement();
if(param != null) {
String value = request.getParameter(param);
if(value != null) {
param2value.put(param, value);
}
}
}
return param2value;
}
}
最后冗茸,需要應(yīng)用這個攔截器:
@Configuration
@EnableWebMvc
public class ApiWebMvcConfig implements WebMvcConfigurer {
@Bean
public HandlerInterceptor getAccessLogInterceptor(){
return new AccessLogInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(getAccessLogInterceptor()).addPathPatterns("/**");
}
}
總結(jié):
以上,就是我這里實踐的添加日志的兩種的方式∠氖現(xiàn)在公司項目使用的是第二種添加方式顶捷,畢竟打印的日志全一點。