log 4 j 2データベースへの情報の記録


注記を追加した断面Spring Aopの設定userIdなどを定義します.
カスタム注記:
package com.pinyu.system.global.ann;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 *       
 */
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MethodLog {

    /**
     *                 
     */
    String remark() default "";
    String operType() default "";
}

切断面:
package com.pinyu.system.global.aspect;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.pinyu.system.dao.OperationLogDao;
import com.pinyu.system.entity.UserEntity;
import com.pinyu.system.global.ann.MethodLog;
import com.pinyu.system.utils.SessionUtils;

/**
 * @author ypp     :2018 10 9    10:37:34
 * @Description: TODO(    )
 */
@Component
@Aspect
public class OperationLogAspect {

	protected Logger logger = LogManager.getLogger(OperationLogAspect.class);

	@Autowired
	private OperationLogDao logDao;

	public OperationLogAspect() {
		logger.info("      ");
	}

	/**
	 *   
	 */
	@Pointcut("@annotation(com.pinyu.system.global.ann.MethodLog)")
	public void methodCachePointcut() {
	}

	/**
	 *   
	 *
	 * @param point
	 * @return
	 * @throws Throwable
	 */
	@Around("methodCachePointcut()")
	public Object around(ProceedingJoinPoint point) throws Throwable {

		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		String ip = getIp(request);
		UserEntity loginUser = SessionUtils.getLoginUser(request);
		ThreadContext.put("userId",String.valueOf(loginUser.getId()));
		ThreadContext.put("creater", loginUser.getRealName());
		ThreadContext.put("createUserName", loginUser.getUserName());
		ThreadContext.put("ip", ip);
		String methodRemark = getMthodRemark(point);
		Object[] method_param;

		Object object;
//		OperationLogEntity optLog = new OperationLogEntity();
//		optLog.setExFlag(OperationLogEntity.EX_FALSE);
//		try {
//			method_param = point.getArgs(); //       
//			// String param=(String) point.proceed(point.getArgs());
			object = point.proceed();
//		} catch (Exception e) {
//			//         ..log.error(e);
//			StringWriter stringWriter = new StringWriter();
//			PrintWriter writer = new PrintWriter(stringWriter);
//			e.printStackTrace(writer);
//			StringBuffer buffer = stringWriter.getBuffer();
//			optLog.setExFlag(OperationLogEntity.EX_TRUE);
//			optLog.setExceptionMsg(buffer.toString());
//			throw e;
//		}
//		optLog.setCreaterId(loginUser.getId());
//		optLog.setIp(ip);
//		optLog.setCreater(loginUser.getRealName());
//		optLog.setCreateUserName(loginUser.getUserName());
//		optLog.setOperation(methodRemark);
//		optLog.setCreateDate(new Date());
//		logDao.add(optLog);
		return object;

	}

	/**
	 *     ip
	 *
	 * @param request
	 * @return
	 */
	public static String getIp(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
//		String ip = request.getHeader("X-Forwarded-For");
//		if (StringUtils.isBlank(ip) || ip.length() == 0 || ip.equals("unKnown")) {
//
//		}
//
//		if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
//			int index = ip.indexOf(",");
//			if (index != -1) {
//				return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip.substring(0, index);
//			} else {
//				return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
//			}
//		}
//		ip = request.getHeader("X-Real-IP");
//		if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
//			return ip;
//		}
//		return request.getRemoteAddr().equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
	}

	/**
	 *           
	 *
	 * @param joinPoint
	 * @return
	 * @throws Exception
	 */
	public static String getMthodRemark(ProceedingJoinPoint joinPoint) throws Exception {

		String targetName = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		Object[] arguments = joinPoint.getArgs();

		Class targetClass = Class.forName(targetName);
		Method[] method = targetClass.getMethods();
		String methode = "";
		for (Method m : method) {
			if (m.getName().equals(methodName)) {
				Class[] tmpCs = m.getParameterTypes();
				if (tmpCs.length == arguments.length) {
					MethodLog methodCache = m.getAnnotation(MethodLog.class);
					if (methodCache != null) {
						methode = methodCache.remark();
					}
					break;
				}
			}
		}
		return methode;
	}
}

SQL:
/*
Navicat MySQL Data Transfer

Source Server         : 211.149.199.68
Source Server Version : 50723
Source Host           : 211.149.199.68:3306
Source Database       : mypinyu

Target Server Type    : MYSQL
Target Server Version : 50723
File Encoding         : 65001

Date: 2018-11-01 17:20:59
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for sys_logs
-- ----------------------------
DROP TABLE IF EXISTS `sys_logs`;
CREATE TABLE `sys_logs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `class` varchar(100) DEFAULT NULL COMMENT '   ',
  `function` varchar(100) DEFAULT NULL COMMENT '      ',
  `level` varchar(100) DEFAULT NULL COMMENT '    ',
  `logger` varchar(100) DEFAULT NULL,
  `message` text COMMENT '    ',
  `create_date` timestamp NULL DEFAULT NULL COMMENT '  ',
  `ip` varchar(255) DEFAULT NULL COMMENT 'IP  ',
  `user_id` varchar(255) DEFAULT NULL,
  `create_user_name` varchar(255) DEFAULT NULL,
  `creater` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3519 DEFAULT CHARSET=utf8 COMMENT='    ';

コード:
package com.pinyu.system.global.config.log4j2;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnection;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.springframework.stereotype.Component;

import com.pinyu.system.utils.SystemPropertiesUtils;


/** 
* @author ypp
*     :2018 11 1    2:33:44 
* @Description: TODO(log4j2        ) 
*/
@Component
public class ConnectionFactory {
	
		private static interface Singleton {  
	       final ConnectionFactory INSTANCE = new ConnectionFactory();  
	    }  
	   
	   private final DataSource dataSource;  
	   
	   private ConnectionFactory() {  
	             try {  
	           Class.forName(SystemPropertiesUtils.getDataSourceDriverClassName());  
	       } catch (ClassNotFoundException e) {  
	           e.printStackTrace();  
	           System.exit(0);  
	       }  
	              
	       Properties properties = new Properties();  
	       properties.setProperty("user", SystemPropertiesUtils.getDataSourceUserName());  
	       properties.setProperty("password", SystemPropertiesUtils.getDataSourcePassword());   
	   
	       GenericObjectPoolpool = new GenericObjectPool();  
	       DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(  
	    		   SystemPropertiesUtils.getDataSourceUrl(),properties  
	       );  
	       new PoolableConnectionFactory(connectionFactory, pool, null, null, false, true);  
	       this.dataSource = new PoolingDataSource(pool);  
	    }  
	   
	   public static Connection getDatabaseConnection() throws SQLException {  
	       return Singleton.INSTANCE.dataSource.getConnection();  
	   }  
}

使用するdbcp接続プール
log 4 j 2プロファイル





	
	
		D:/apache-tomcat-8.5.33/logs
		
		%d{yyyy-MM-dd HH:mm:ss} %-5level %class{36} %L
			%M - %msg%xEx%n
	

	
		
		
			
		


		
		
			
			
				
				
			

			
				${PATTERN_FORMAT}
			

			
				
				
				
				
			
		

		
		
			
			
				
				
			

			
				${PATTERN_FORMAT}
			

			
				
				
				
				
			
		

		
		
			
			

			
				${PATTERN_FORMAT}
			

			
				
				
				
				
			
		
		
		
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
		
	

	

		
		
		

		
		

		
		
			
			
			
			
			
		
	


注意Rootは上のデータベースDatabaseAppenderの情報を構成し、他のものは簡単です.しかし、すべてのログがデータベースに記録されており、実際のニーズを満たすことができないという問題があります.データベースはログ情報を保存できます