标签:技术 日志 sources element lease map mes ping console
@(springboot配置Log4j日志)
首先要导入log4j属性文件 Log4j.properties
在resources文件夹下导入log4j.properties文件,复制下面的内容时,每行后面如果有空格的话一定要去掉,log4j.appender.File.File为log日志存放的路径。
log4j.rootLogger=INFO,Console,File
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c - %L]%m%n
log4j.appender.File = org.apache.log4j.RollingFileAppender
log4j.appender.File.File = C:/Users/10301/Desktop/test/logs/info/info.log
log4j.appender.File.MaxFileSize = 10MB
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c - %L]%m%n
去除springboot的log,添加log4j,因为springboot自带的log4j不起作用。
<!-- 排除springboot自带的logging 日志-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 重新导入log4j -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
版本号一定要注意,springboot下的log4j的版本最新是1.3.8,如果你的springboot的parent版本过高,那么在添加log4j时就添加自己的版本。
@RestController //声明Rest风格的控制器
@RequestMapping("user")
public class UserController {
@Autowired
private UserServiceImpl userService;
Logger logger=Logger.getLogger(UserController.class);
@RequestMapping("register")
@ResponseBody
public String register(String username,String password){
//记录客户端请求参数
logger.info("username"+username+"password"+password);
//把数据保存到test1数据库
userService.register(username,password);
return "success";
}
}
然后日志就会在设置的路径目录里生成log日志,并记录下客户端的请求参数。
<!-- AOP -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
在java文件夹下新建包aspect,包里新建切面类,这个类在访问指定的controller之前会拦截并进入该类执行里面的方法。
package com.gyf.aspect;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
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.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@Aspect
@Component//配置bean
public class WebLogAspect {
private Logger logger = Logger.getLogger(getClass());
@Pointcut("execution(public * com.gyf.web.controller..*.*(..))")
public void webLog() {
}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
logger.info("---------------request----------------");
logger.info("URL : " + request.getRequestURL().toString());
logger.info("HTTP_METHOD : " + request.getMethod());
logger.info("IP : " + request.getRemoteAddr());
Enumeration<String> enu = request.getParameterNames();
while (enu.hasMoreElements()) {
String name = (String) enu.nextElement();
logger.info("name:" + name + " - value:" + request.getParameter(name));
}
}
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
logger.info("---------------response----------------");
// 处理完请求,返回内容
logger.info("RESPONSE : " + ret);
}
}
然后controller中就不需要加下面蓝色的内容了,因为这些已经在刚才的切面类中配过了。
这就已经配置好了,重启springboot项目,访问控制层的方法,会记录相应日志(报错不会记录)
标签:技术 日志 sources element lease map mes ping console
原文地址:https://www.cnblogs.com/fantongxue/p/12443405.html