其他分享
首页 > 其他分享> > 夜光带你走进我擅长的领域:SpringBOOT(6)

夜光带你走进我擅长的领域:SpringBOOT(6)

作者:互联网

夜光序言:

 

每次你看我的时候我都假装在看别处,而每次你看别处的时候我都在看着你。冬日的晴天,不及你的温柔,星罗棋布的夜幕,像你眼中的闪烁,世间美好的一切,感觉都体现在你身上了。

 

 

 

 

 

 

正文:

6.1使用log4j记录日志

6.1.2新建log4j配置文件

#log4j.rootLogger=CONSOLE,info,error,DEBUG

log4j.rootLogger=info,error,CONSOLE,DEBUG

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender     

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout     

log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n     

log4j.logger.info=info

log4j.appender.info=org.apache.log4j.DailyRollingFileAppender

log4j.appender.info.layout=org.apache.log4j.PatternLayout     

log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  

log4j.appender.info.datePattern='.'yyyy-MM-dd

log4j.appender.info.Threshold = info   

log4j.appender.info.append=true   

#log4j.appender.info.File=/home/admin/pms-api-services/logs/info/api_services_info

log4j.appender.info.File=/Users/dddd/Documents/testspace/pms-api-services/logs/info/api_services_info

log4j.logger.error=error  

log4j.appender.error=org.apache.log4j.DailyRollingFileAppender

log4j.appender.error.layout=org.apache.log4j.PatternLayout     

log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  

log4j.appender.error.datePattern='.'yyyy-MM-dd

log4j.appender.error.Threshold = error   

log4j.appender.error.append=true   

#log4j.appender.error.File=/home/admin/pms-api-services/logs/error/api_services_error

log4j.appender.error.File=/Users/dddd/Documents/testspace/pms-api-services/logs/error/api_services_error

log4j.logger.DEBUG=DEBUG

log4j.appender.DEBUG=org.apache.log4j.DailyRollingFileAppender

log4j.appender.DEBUG.layout=org.apache.log4j.PatternLayout     

log4j.appender.DEBUG.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  

log4j.appender.DEBUG.datePattern='.'yyyy-MM-dd

log4j.appender.DEBUG.Threshold = DEBUG   

log4j.appender.DEBUG.append=true   

#log4j.appender.DEBUG.File=/home/admin/pms-api-services/logs/debug/api_services_debug

log4j.appender.DEBUG.File=/Users/dddd/Documents/testspace/pms-api-services/logs/debug/api_services_debug

 

 

 

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

6.2.1POM文件新增依赖

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-aop</artifactId>

</dependency>

 

6.2.2POM文件新增依赖

@Aspect

@Component

public class WebLogAspect {

private Logger logger = LoggerFactory.getLogger(getClass());

@Pointcut("execution(public * com.Genius.controller..*.*(..))")

public void webLog() {

}

@Before("webLog()")

public void doBefore(JoinPoint joinPoint) throws Throwable {

// 接收到请求,记录请求内容

ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

HttpServletRequest request = attributes.getRequest();

// 记录下请求内容

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:{},value:{}", name, request.getParameter(name));

}

}

@AfterReturning(returning = "ret", pointcut = "webLog()")

public void doAfterReturning(Object ret) throws Throwable {

// 处理完请求,返回内容

logger.info("RESPONSE : " + ret);

}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:info,SpringBOOT,appender,擅长,api,error,DEBUG,log4j,夜光
来源: https://blog.csdn.net/weixin_41987706/article/details/99701902