springboot整合log4j

一、默认名log4j2-spring.xml,就省下了在application.yml中配置




























































二、使用方法1

public class LogExampleOther {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExampleOther.class);

public static void main(String... args) {
log.error("Something else is wrong here");
}
}

三、使用方法2:使用lombok后下面的代码等效于上述的代码

@Slf4j
public class LogExampleOther {
public static void main(String... args) {
log.error("Something else is wrong here");
}
}

四、使用AOP统一处理Web请求日志

@Component
@Aspect
@Slf4j
public class WebLogAspect {

@Pointcut("execution(public * cn.itcast.account.controller..(..))")
public void webLog() {
}

@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
log.info("URL : " + request.getRequestURL().toString());
log.info("HTTP_METHOD : " + request.getMethod());
log.info("IP : " + request.getRemoteAddr());
Enumeration enu = request.getParameterNames();
while (enu.hasMoreElements()) {
String name = (String) enu.nextElement();
log.info("name:{},value:{}", name, request.getParameter(name));
}
}

@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
// 处理完请求,返回内容
log.info("RESPONSE : " + ret);
}

}

posted @ 2020-08-30 01:57  jock_javaEE  阅读(243)  评论(0)    收藏  举报