首先在资源目录下创建logback.xml文件
文件的内容
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--======================================= 本地变量 ======================================== -->
<!--在没有定义${LOG_HOME}系统变量的时候,可以设置此本地变量。提交测试、上线时,要将其注释掉,使用系统变量。 -->
<!-- <property name="LOG_HOME" value="D:/data/logs" /> -->
<!-- 应用名称:和统一配置中的项目代码保持一致(小写) -->
<property name="APP_NAME" value="base" />
<!--日志文件保留天数 -->
<property name="LOG_MAX_HISTORY" value="30" />
<!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径 -->
<!--应用日志文件保存路径 -->
<property name="LOG_APP_HOME" value="${APP_NAME}/app" />
<!--=========================== 按照每天生成日志文件:默认配置=================================== -->
<!-- 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<!-- 按照每天生成日志文件:主项目日志 -->
<appender name="APP"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件输出的文件名 -->
<FileNamePattern>${LOG_APP_HOME}/base.%d{yyyy-MM-dd}.log
</FileNamePattern>
<!--日志文件保留天数 -->
<MaxHistory>${LOG_MAX_HISTORY}</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{500} - %msg%n</pattern>
</encoder>
</appender>
<!--=============================== 日志输出: 默认主业务日志 ====================================== -->
<logger name="org.springframework">
<level value="WARN" />
</logger>
<logger name="org.apache.shiro">
<level value="WARN" />
</logger>
<logger name="freemarker">
<level value="WARN" />
</logger>
<logger name="org.hibernate">
<level value="WARN" />
</logger>
<logger name="org.hibernate.SQL">
<level value="DEBUG" />
</logger>
<root level="INFO">
<appender-ref ref="APP" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
创建aop日志类
LogAspectServiceApi
package com.itmayiedu.common.log;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
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 javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
/**
* @author wangjie
* @date 2019/12/2 18:15
* @description 使用aop打印日志
* @company 石文软件有限公司
*/
@Aspect
// 申明是个spring管理的bean
@Component
@Slf4j
public class LogAspectServiceApi {
private JSONObject jsonObject = new JSONObject();
private final Logger log= LoggerFactory.getLogger(LogAspectServiceApi.class);
// 申明一个切点 里面是 execution表达式com.itmayiedu.api.service.impl
@Pointcut("execution(public * com.itmayiedu.api.service.impl.*.*(..))")
private void controllerAspect() {
}
// 请求method前打印内容
@Before(value = "controllerAspect()")
public void methodBefore(JoinPoint joinPoint) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
log.info("===============请求内容===============");
try {
// 打印请求内容
log.info("请求地址:" + request.getRequestURL().toString());
log.info("请求方式:" + request.getMethod());
log.info("请求类方法:" + joinPoint.getSignature());
log.info("请求类方法参数:" + Arrays.toString(joinPoint.getArgs()));
} catch (Exception e) {
log.error("###LogAspectServiceApi.class methodBefore() ### ERROR:", e);
}
log.info("===============请求内容===============");
}
// 在方法执行完结后打印返回内容
@AfterReturning(returning = "o", pointcut = "controllerAspect()")
public void methodAfterReturing(Object o) {
log.info("--------------返回内容----------------");
try {
log.info("Response内容:" + jsonObject.toJSONString(o));
} catch (Exception e) {
log.error("###LogAspectServiceApi.class methodAfterReturing() ### ERROR:", e);
}
log.info("--------------返回内容----------------");
}
}
@Slf4j打印日志
package com.itmayiedu.api.service.impl;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RestController;
import com.itmayiedu.api.service.DemoApiService;
import com.itmayiedu.common.api.BaseApiService;
@Slf4j
@RestController
public class DemoApiServiceImpl extends BaseApiService implements DemoApiService {
@Override
public Map<String, Object> demo() {
return setResutSuccess();
}
}
原文地址:https://www.cnblogs.com/wang66a/p/12069283.html
时间: 2024-10-09 10:29:24