spring boot logback配置

目录结构:
spring boot logback配置_第1张图片

logback-boot.xml:


<configuration>

    <property name="logs.dir" value="logs"/>
    
    <property name="CONSOLE_LOG_PATTERN"
              value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(--){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

    
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
    <conversionRule conversionWord="wex"
    converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
    <conversionRule conversionWord="wEx"
    converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>

    
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                ${CONSOLE_LOG_PATTERN}
            pattern>
            <charset>utf8charset>
        encoder>
    appender>

    
    <appender name = "infoFileLog" class = "ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${logs.dir}/mall.logfile>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logs.dir}/mall.%d{yyyy-MM-dd}.logfileNamePattern>
            <maxHistory>3maxHistory>
        rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUGlevel>
        filter>
        <encoder>
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} -%5p ${PID:-} [%15.15t] %logger.%M:%L : %m%n
            pattern>
        encoder>
    appender>

    
    <appender name = "errorFileLog" class = "ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${logs.dir}/mall_error.logfile>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logs.dir}/mall_error.%d{yyyy-MM-dd}.logfileNamePattern>
            <maxHistory>3maxHistory>
        rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERRORlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <encoder>
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} -%5p ${PID:-} [%15.15t] %logger.%M:%L : %m%n
            pattern>
        encoder>
    appender>

    
    <appender name = "warnFileLog" class = "ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${logs.dir}/mall_warn.logfile>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logs.dir}/mall_warn.%d{yyyy-MM-dd}.logfileNamePattern>
            <maxHistory>3maxHistory>
        rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>WARNlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <encoder>
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} -%5p ${PID:-} [%15.15t] %logger.%M:%L : %m%n
            pattern>
        encoder>
    appender>

    
    
    <logger name = "com.hj.project.rest.mapper" level = "DEBUG">logger>

    <root level = "INFO">
        <appender-ref ref = "STDOUT" />
        <appender-ref ref = "infoFileLog" />
        <appender-ref ref = "warnFileLog" />
        <appender-ref ref = "errorFileLog" />
    root>

configuration>

此处用的是aop处理日志

spring boot logback配置_第2张图片

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 日志配置
 * @Author: hj
 * @Date: 10:40 2017/11/10
 */
@Aspect
@Component
public class LogAopAction {

    final Logger logger = LoggerFactory.getLogger(LogAopAction.class);

    @Pointcut("execution(public * com.hj.project.rest.controller..*(..))")
    private void pointCutMethod(){}

    @Before("pointCutMethod()")
    public void doBefore(JoinPoint joinPoint) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        String uri = request.getRequestURI();  //URI
        String ip = request.getRemoteHost();   //IP
        String method = request.getMethod();   //请求方法类型

        Object[] args = joinPoint.getArgs();   //参数列表
        List params = new ArrayList<>();
        for(Object arg : args){
            if(arg instanceof MultipartFile){
                MultipartFile file = (MultipartFile)arg;
                params.add("FILE:"+file.getOriginalFilename());
                continue;
            }
            params.add(arg.toString());
        }
        logger.info(">>>接口:[{}]收到来自IP:[{}]的[{}]请求,参数为:{}",uri,ip,method,params);
    }

    @AfterReturning(value = "pointCutMethod()",returning = "result")
    public void doAfterReturning(JoinPoint joinPoint,Object result) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        String uri = request.getRequestURI();  //URI
        String method = request.getMethod();   //请求类型
        String ip = request.getRemoteHost();   //IP

        logger.info("<<<接口:[{}][{}]的请求,返回给[{}]数据:[{}]",uri,method,ip,result);
    }

}

特别声明: 接受参数类和返回参数类必须重写toString方法

你可能感兴趣的:(spring-boot)