Spring Cloud Spring Boot mybatis distributed microservice cloud architecture (forty-three) using log4j to implement http request logs into mongodb (2)

Configure log4j.properties

Set up a logger named mongodb:

  • Log INFO level logs
  • The appender is implemented as com.didispace.log.MongoAppende
  • mongodb connection address: mongodb://localhost:27017
  • mongodb database name: logs
  • mongodb collection name: logs_request
    log4j.logger.mongodb=INFO, mongodb
    # mongodb输出
    log4j.appender.mongodb=com.didispace.log.MongoAppender
    log4j.appender.mongodb.connectionUrl=mongodb://localhost:27017
    log4j.appender.mongodb.databaseName=logs
    log4j.appender.mongodb.collectionName=logs_request

    Use mongodb logger in aspect

    The modified code is as follows, with the following modifications:

  • logger named mongodb
  • Obtain the request information from the HttpServletRequest and JoinPoint objects through the getBasicDBObject function, and assemble them into a BasicDBObject
    • getHeadersInfo function gets header information from HttpServletRequest
  • Through logger.info(), output the information of the BasicDBObject object to mongodb
    @Aspect
    @Order(1)
    @Component
    public class WebLogAspect {
    
        private Logger logger = Logger.getLogger("mongodb");
    
        @Pointcut("execution(public * com.didispace.web..*.*(..))")
        public void webLog(){}
    
        @Before("webLog()")
        public void doBefore(JoinPoint joinPoint) throws Throwable {
            // 获取HttpServletRequest
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            // 获取要记录的日志内容
            BasicDBObject logInfo = getBasicDBObject(request, joinPoint);
            logger.info(logInfo);
        }
    
    
        private BasicDBObject getBasicDBObject(HttpServletRequest request, JoinPoint joinPoint) {
            // 基本信息
            BasicDBObject r = new BasicDBObject();
            r.append("requestURL", request.getRequestURL().toString());
            r.append("requestURI", request.getRequestURI());
            r.append("queryString", request.getQueryString());
            r.append("remoteAddr", request.getRemoteAddr());
            r.append("remoteHost", request.getRemoteHost());
            r.append("remotePort", request.getRemotePort());
            r.append("localAddr", request.getLocalAddr());
            r.append("localName", request.getLocalName());
            r.append("method", request.getMethod());
            r.append("headers", getHeadersInfo(request));
            r.append("parameters", request.getParameterMap());
            r.append("classMethod", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
            r.append("args", Arrays.toString(joinPoint.getArgs()));
            return r;
        }
    
        private Map<String, String> getHeadersInfo(HttpServletRequest request) {
            Map<String, String> map = new HashMap<>();
            Enumeration headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String key = (String) headerNames.nextElement();
                String value = request.getHeader(key);
                map.put(key, value);
            }
            return map;
        }
    
    }
    

    other supplements

    The above content mainly provides an idea to realize the output and management of custom logs. We can log to mongodb through jdbc, or log to mongodb through spring-data-mongo. Of course, we can also output to other databases, or output to message queues for other subsequent processing.

    For logging to mongodb, it is more convenient and faster to use log4mongo directly.

  • source code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325856444&siteId=291194637