无法在每个方法 JDK6 和 Spring 4.0.6.RELEASE 上使用 AspectJ 添加日志

问题描述

我有一个旧应用程序,它使用 java 6 和 Spring 4.0.6。

我在使用 log4j 添加日志时遇到很多问题,日志永远不会......记录。 所以我只在代码中使用了 System.out.println,而且效果很好,所有日志都重定向到 server.log 文件中。

我想自动添加一些日志来跟踪每个方法的每次调用。 所以我在我的 pom.xml 中添加了这个:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.9</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.9</version>
    </dependency>

我创建了一个方面类:

package com.al6.business.log;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Component
@Aspect
public class MyAspect {

    private final Log log = LogFactory.getLog(this.getClass());
    
    @Around("execution(* com.al6.business.commande.CommandeBusiness.*(..))")
    public void logAroundAllMethods(ProceedingJoinPoint joinPoint) throws Throwable 
    {
        log.info("Test with logger");
        System.out.println("****LoggingAspect.logAroundAllMethods() : " + joinPoint.getSignature().getName() + ": Before Method Execution");
        try {
            joinPoint.proceed();
        } finally {
            //Do Something useful,If you have
        }
        System.out.println("****LoggingAspect.logAroundAllMethods() : " + joinPoint.getSignature().getName() + ": After Method Execution");
    }


}

在我的 xml beans 文件添加

<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx" 
 xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

  <aop:aspectj-autoproxy/>
  
 
  <bean id="tracerInvocation" class="com.al6.business.log.MyAspect">
  
  </bean>

...


</beans>

这是我需要监控的代码示例:

package com.al6.business.commande;

import ...
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Subqueries;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


    /**
     * Manages order business service
     *
     */
    public class CommandeBusiness extends MainBusinessService implements ICommandeService,ITransfertService {
    
    
    @Override
    public void creerEnteteCommande(Commande commande,String idCommandeAPurger,Utilisateur utilisateur) {
        System.out.println("creerEnteteCommande " + commande.getId() + " par " + utilisateur + " Type doc : " + commande.getTypeDocument());
        ...
    }
...
}

在我的 server.log 文件中,方法中的 Sysout 被正确记录,但我的方面日志不在这里...:

2021-04-28 18:03:25,565 INFO  [STDOUT] creerEnteteCommande 81797581 par UTILISATEUR SUPER (admindsi) Type doc : CC

如果我在 server.log 中搜索 Aspect,我会看到:

2021-04-28 17:04:24,563 INFO  [STDOUT] 989  [ajp-0.0.0.0-8009-2] DEBUG org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory  - Found AspectJ method: public java.lang.Object com.al6.business.log.MyAspect.logAroundAllMethods(org.aspectj.lang.ProceedingJoinPoint) throws java.lang.Throwable

那为什么我的日志不在这里? 我服务器上有一个jboss-log4j.xml文件,是不是配置会导致aspectJ日志效率低下?

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)