正文
日志文件在開發(fā)中至關(guān)重要,是日后查看異常,排查bug的重要依據(jù)。
像我大安卓,也會有日志記錄類Thread.UncaughtExceptionHandler進(jìn)行錯誤異常日志記錄奄抽,并回傳后臺進(jìn)行匯總報告,或者是使用Bugly 來進(jìn)行異常日志捕獲甩鳄,那么服務(wù)器也是會有類似的逞度。
而我們使用的是logback
使用
配置文件:logback-spring.xml
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%d - %msg%n
</pattern>
</layout>
</appender>
<appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<encoder>
<pattern>
%msg%n
</pattern>
</encoder>
<!--滾動策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--路徑-->
<fileNamePattern>log/tomcat/sell/info.%d.log</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>
%msg%n
</pattern>
</encoder>
<!--滾動策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--路徑-->
<fileNamePattern>log/tomcat/sell/error.%d.log</fileNamePattern>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="consoleLog" />
<appender-ref ref="fileInfoLog" />
<appender-ref ref="fileErrorLog" />
</root>
</configuration>
存放位置
與application同級
配置方法
配合 插件
在使用的類前面加入注解:
@Slf4j
具體如下:
@RestController
@RequestMapping("api/home")
@Api(value = "首頁模塊")
@Slf4j
public class AwayController {
...
}
使用方法
@GetMapping("shopList")
@ApiOperation("獲取店鋪列表")
public ResultVO shopList(@RequestParam(value = "page", defaultValue = "0") Integer page,
@RequestParam(value = "size", defaultValue = "10") Integer size) {
Sort orders = new Sort(Sort.Direction.ASC, "id");
PageRequest pageRequest = new PageRequest(page, size, orders);
Page<Shop> shopPage = shopService.findShopList(pageRequest);
List<ShopResultVo> shopResultVos = shopPage.getContent()
.stream()
.map(shop -> {
ShopResultVo shopResultVo = new ShopResultVo();
List<Discount> discounts = discountService.findAllByBelongingId(shop.getId());
BeanUtils.copyNonNullProperties(shop, shopResultVo);
shopResultVo.setDiscounts(discounts);
return shopResultVo;
}).collect(Collectors.toList());
log.error("shopResultVos is {}",shopResultVos.toString());
return ResultVoUtils.success(shopResultVos);
}
其中
log.error("shopResultVos is {}",shopResultVos.toString());
就是調(diào)用了log方法