正文
日誌檔案在開發中至關重要,是日後檢視異常,排查bug的重要依據。 像我大安卓,也會有日誌記錄類Thread.UncaughtExceptionHandler進行錯誤異常日誌記錄,並回傳後臺進行彙總報告,或者是使用**Bugly** 來進行異常日誌捕獲,那麼伺服器也是會有類似的。 而我們使用的是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());
複製程式碼
就是呼叫了log方法