Java 每次方法调用的日志打印次数不断增加

Java 每次方法调用的日志打印次数不断增加,java,log4j,Java,Log4j,我有一个spring方法来记录方法调用,还有一个JUnit测试,它有6个测试。在每次第n次测试中,日志会打印n次相关信息。因此,当第一个测试运行时,它的信息按原样打印一次,当第二个测试运行时,它的信息打印两次,依此类推。奇怪的是,这种情况只发生在Eclipse控制台上——日志文件本身完全正常。 怎么了 代码如下: 日志记录的spring方面 public class LogAspect { /** * The LOG object to which all the detai

我有一个spring方法来记录方法调用,还有一个JUnit测试,它有6个测试。在每次第n次测试中,日志会打印n次相关信息。因此,当第一个测试运行时,它的信息按原样打印一次,当第二个测试运行时,它的信息打印两次,依此类推。奇怪的是,这种情况只发生在Eclipse控制台上——日志文件本身完全正常。 怎么了

代码如下: 日志记录的spring方面

public class LogAspect {

    /**
     * The LOG object to which all the details are sent.
     */
    private static final Logger LOG = Logger.getLogger(LogAspect.class);

    /**
     * Pointcut to identify methods annotated with @LogWithReturnValue.
     */
    @Pointcut("@annotation(com.skyfall.aspects.LogWithReturnValue)")
    public void logWithReturnValuePointcut() {
    }

    /**
     * Pointcut to identify methods annotated with @Log.
     */
    @Pointcut("@annotation(com.skyfall.aspects.Log)")
    public void logPointcut() {
    }

    /**
     * Pointcut to identify methods annotated with @LogWithArguments.
     */
    @Pointcut("@annotation(com.skyfall.aspects.LogWithArguments)")
    public void logWithArgumentsPointcut() {
    }

    /**
     * Pointcut to identify methods annotated with @LogWithArgumentsAndReturnValue.
     */
    @Pointcut("@annotation(com.skyfall.aspects.LogWithArgumentsAndReturnValue)")
    public void logWithArgumentsAndReturnValuePointcut() {
    }

    /**
     * Pointcut to identify methods annotated with @Metrics.
     * 
     * @param metrics
     *            Used to access the parameters of the annotation.
     */
    @Pointcut("@annotation(metrics)")
    public void metricsPointcut(com.skyfall.aspects.Metrics metrics) {
    }

    /**
     * Logs "Entering Method : className.methodName"
     * 
     * @param joinPoint
     */
    @Before("logPointcut()")
    public void logBefore(JoinPoint joinPoint) {
        LOG.info("Entering Method : ".concat(getClassAndName(joinPoint)));
    }

    /**
     * Logs "Entering Method : className.methodName". Also, the list of parameters as : param1 : value1, param2 :
     * value2, ...
     * 
     * @param joinPoint
     */
    @Before("logWithArgumentsPointcut()")
    public void logWithArgumentsBefore(JoinPoint joinPoint) {
        LOG.info("Entering Method : ".concat(getClassAndName(joinPoint)));
        LOG.info(getParams(joinPoint));
    }

    /**
     * Logs "Exiting Method : className.methodName"
     * 
     * @param joinPoint
     */
    @After(" logPointcut() || logWithArgumentsPointcut()")
    public void logAfter(JoinPoint joinPoint) {
        LOG.info("Exiting Method : ".concat(getClassAndName(joinPoint)).concat("\n"));
    }

    /**
     * Logs calls to methods annotated with @LogWithReturnValue
     * 
     * @param joinPoint
     * @return Returns whatever object is returned by the annotated method.
     * @throws Throwable
     *             In case the method throws an exception.
     */
    @Around("logWithReturnValuePointcut()")
    public Object logWithReturnValue(ProceedingJoinPoint joinPoint) throws Throwable {
        LOG.info("Entering Method : ".concat(getClassAndName(joinPoint)));
        Object retVal = joinPoint.proceed();
        LOG.info("Method returned : ".concat(retVal.toString()));
        LOG.info("Exiting Method : ".concat(getClassAndName(joinPoint)).concat("\n"));
        return retVal;
    }

    /**
     * Logs calls to methods annotated with @LogWithArgumentsAndReturnValue
     * 
     * @param joinPoint
     * @return Returns whatever object is returned by the annotated method.
     * @throws Throwable
     *             In case the method throws an exception.
     */
    @Around("logWithArgumentsAndReturnValuePointcut()")
    public Object logWithArgumentsAndReturnValue(ProceedingJoinPoint joinPoint) throws Throwable {
        LOG.info("Entering Method : ".concat(getClassAndName(joinPoint)));
        LOG.info(getParams(joinPoint));
        Object retVal = joinPoint.proceed();
        LOG.info("Method returned : ".concat(retVal.toString()));
        LOG.info("Exiting Method : ".concat(getClassAndName(joinPoint)).concat("\n"));
        return retVal;
    }

    /**
     * Logs methods annotated with @Metrics, that exit successfully. Also calculates their running time, and creates a
     * UserEvent object to enter into the database. However, that part is not working for now.
     * 
     * @param joinPoint
     * @param metrics
     *            Contains the parameters, if any, passed to the annotation.
     * @return Returns whatever object is returned by the annotated method.
     * @throws Throwable
     *             In case the method throws an exception.
     */
    @Around("metricsPointcut(metrics)")
    public Object metricsTime(ProceedingJoinPoint joinPoint, com.skyfall.aspects.Metrics metrics) throws Throwable {
        LOG.info("Entering Method : ".concat(getClassAndName(joinPoint)));
        LOG.info(("EventEnum : ").concat(metrics.eventEnum().toString()));
        long startTime = System.currentTimeMillis();
        Object retVal = joinPoint.proceed();
        long endTime = System.currentTimeMillis();
        long executionTime = endTime - startTime;
        StringBuffer logMessage = new StringBuffer();
        logMessage.append("Execution time : ");
        logMessage.append(executionTime);
        logMessage.append(" ms");
        LOG.info(logMessage.toString());
        LOG.info("Exiting Method : ".concat(getClassAndName(joinPoint)).concat("\n"));

        if (metrics.args() == 0) {
            UserEvent userEvent = userEventMaker(joinPoint.getClass().getSimpleName(), joinPoint.getSignature()
                    .getName(), metrics.eventEnum(), 0, null, executionTime, 1, retVal);
        } else {
            UserEvent userEvent = userEventMaker(joinPoint.getClass().getSimpleName(), joinPoint.getSignature()
                    .getName(), metrics.eventEnum(), 1, getParamsList(joinPoint), executionTime, 1, retVal);
        }

        return retVal;
    }

    /**
     * Logs a method annotated with @Metrics in case it throws an exception.
     * 
     * @param joinPoint
     * @param metrics
     *            Contains the parameters, if any, passed to the annotation.
     * @param ex
     *            The exception that was thrown by the method.
     */
    @AfterThrowing(pointcut = "metricsPointcut(metrics)", throwing = "ex")
    public void metricsExceptions(JoinPoint joinPoint, com.skyfall.aspects.Metrics metrics, Exception ex) {
        if (metrics.args() == 0) {
            UserEvent userEvent = userEventMaker(joinPoint.getClass().getSimpleName(), joinPoint.getSignature()
                    .getName(), metrics.eventEnum(), 0, null, 0, 0, null, ex.toString());
        } else {
            UserEvent userEvent = userEventMaker(joinPoint.getClass().getSimpleName(), joinPoint.getSignature()
                    .getName(), metrics.eventEnum(), 1, getParamsList(joinPoint), 0, 0, null, ex.toString());
        }
        LOG.info(getClassAndName(joinPoint).concat(" has thrown an exception: " + ex.toString()).concat("\n"));
    }
JUnit测试:

public class AspectTest extends BasePersistenceTest {

    @Before
    public void before() {

    }

    @Test
    public void testLog() {
        logAspectTester.logF(42);
    }

    @Test
    public void testLogWA() {
        logAspectTester.logWAF(42, 7, "string");
    }

    @Test
    public void testLogWRV() {
        logAspectTester.logWRVF(42);
    }

    @Test
    public void testLogWAARV() {
        logAspectTester.logWAARVF(42, 7, "string");
    }

    @Test
    public void testMetrics() {
        logAspectTester.metricsF(42);
    }

    @Test(expected = Exception.class)
    public void testMetricsE() {
        logAspectTester.metricsEF(42);
    }

}
以及log4j.properties文件:

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=logs/skyfall.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %c{1}:%L - %m%n


# Root logger option
# Change log level to DEBUG instead of INFO if you want debug logs as well
# Append stdout in the end like (INFO, file, stdout) if you want to output logs message on the console as well (but don't commit)
log4j.rootLogger=INFO, file

# Log everything. Good for troubleshooting
log4j.logger.org.hibernate=WARN
log4j.logger.org.apache.struts2 = WARN

# Log all JDBC parameters
#log4j.logger.org.hibernate.type=ALL

您使用的是哪种控制台追加器?听起来appender是按照测试方法配置的。你能在这里附加源代码吗?这样我们就可以看一下了?@heikkim我已经添加了log4j.properties文件