1、在resource目录里新增log4j.properties
##
#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
##
2、在pom里引入aop
3、创建切面类
其中Logger 引入的是import org.slf4j.Logger;
HttpServletRequest引入的是import javax.servlet.http.HttpServletRequest;
##
package com.itmayidu.app;
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 javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
/**
* Created by Damon on 2018/6/26_9:45.
*
* @Descrption:
*/
@Component
@Aspect
public class WebLogAspect {
//引入log
private Loggerlogger = LoggerFactory.getLogger(getClass());
//定义切点
@Pointcut("execution(public * com.itmayidu.controller.IndexController.*(..))")
public void webLog(){}
//服务器处理请求之前操作日志
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) {
//接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
//记录请求全路径
logger.info("URL={}", request.getRequestURL());
//Get还是Post等
logger.info("HTTP_METHOD={}", request.getMethod());
//请求地址
logger.info("IP={}" ,request.getRemoteAddr());
//RemoteUser
logger.info("RemoteUser={}", request.getRemoteUser());
//远程地址
logger.info("RemoteHost={}",request.getRemoteHost());
//RemotePort
logger.info("RemotePort={}",request.getRemotePort());
//请求中的参数
logger.info("args={}",joinPoint.getArgs());
//拼出切面类和具体的方法
logger.info("class_method={}" ,joinPoint.getSignature().getDeclaringTypeName() +"."+joinPoint.getSignature().getName());
//用枚举列出参数集合,遍历
Enumeration enu = request.getParameterNames();
while(enu.hasMoreElements()){
String name = enu.nextElement();
logger.info("name={},value={}",name,request.getParameter(name));
}
}
//
@After("webLog()")
public void doAfter(){
logger.info("服务器全部完事了....");
}
//
@AfterReturning(returning ="object" , pointcut ="webLog()")
public void doAfterReturning(Object object) {
logger.info("Response={}" ,object);
}
}
##
4、这样访问IndexController就会进入到日志管理