转载

SpringBoot(三) logback配置

日志文件在开发中至关重要,是日后查看异常,排查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>

存放位置

SpringBoot(三) logback配置

与application同级

配置方法

配合 插件

SpringBoot(三) logback配置

在使用的类前面加入注解:

@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方法

原文  https://juejin.im/post/5a77bf195188257a856f1a57
正文到此结束
Loading...