java Spring Boot Logger 方面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30335563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:55:35  来源:igfitidea点击:

Spring Boot Logger Aspects

javaspringloggingaopaspect

提问by Rick

I'm having problems getting my logging aspect to log information when methods from classes of a particular package are accessed. In other words, "no" logging occurs. I even got desperate and added System.out.println statements, with no luck.

当访问来自特定包的类的方法时,我在让我的日志记录方面记录信息时遇到问题。换句话说,发生“无”日志记录。我什至绝望并添加了 System.out.println 语句,但没有运气。

All of my classes are located under the org.my.packagepackage, i.e. org.my.package.controller, org.my.package.model, etc.

我所有的类都位于org.my.package包下,即org.my.package.controllerorg.my.package.model等。

Here is my Application class:

这是我的应用程序类:

package org.my.package;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan(basePackages = {"org.my.package.config"})
@EnableAutoConfiguration
@EnableAspectJAutoProxy
public class FirstWebAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(FirstWebAppApplication.class, args);
    }
}

This is my configuration class:

这是我的配置类:

package org.my.package.config;

import org.deloitte.javatraining.daythree.utilities.MyLogger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = {"org.my.package.utilities"})
public class AssetConfig {

    //-----------------------------------------------------------------------------------------------------------------------
    @Bean   
    public MyLogger myLogger(){
       return new MyLogger();
    }
}

This is my Aspect class:

这是我的方面类:

package org.my.package.utilities;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyLogger {

    /** Handle to the log file */
    private final Log log = LogFactory.getLog(getClass());

    public MyLogger () {}

    @AfterReturning("execution(* org.my.package.*.*(..))")
    public void logMethodAccessAfter(JoinPoint joinPoint) {
        log.info("***** Completed: " + joinPoint.getSignature().getName() + " *****");
        System.out.println("***** Completed: " + joinPoint.getSignature().getName() + " *****");
    }

    @Before("execution(* org.my.package.*.*(..))")
    public void logMethodAccessBefore(JoinPoint joinPoint) {
        log.info("***** Starting: " + joinPoint.getSignature().getName() + " *****");
        System.out.println("***** Starting: " + joinPoint.getSignature().getName() + " *****");
    }
}

These are my Gradle build dependencies:

这些是我的 Gradle 构建依赖项:

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile('com.h2database:h2:1.3.156')
    compile('javax.servlet:jstl:1.2')
    compile('org.springframework.boot:spring-boot-starter-aop')
    providedRuntime("org.apache.tomcat.embed:tomcat-embed-jasper")
    testCompile("org.springframework.boot:spring-boot-starter-test") 
}

Am I missing something or otherwise mis-configuring my Aspect class? I can't find anything wrong, after verifying with other, similar Stack Overflow questions and online tutorials.

我是否遗漏了什么或以其他方式错误地配置了我的 Aspect 类?在与其他类似的 Stack Overflow 问题和在线教程验证后,我找不到任何错误。

Please advise.

请指教。

回答by M. Deinum

Your point cut, execution( * org.my.package.*.*(..)), is only matching the execution of methods on classes in the org.my.packagepackage not sub packages.

您的切入点,execution( * org.my.package.*.*(..)),仅匹配org.my.package包中类上的方法的执行,而不是子包。

What you probably want is execution( * org.my.package..*.*(..))notice the ..instead of ..

您可能想要的是execution( * org.my.package..*.*(..))注意..而不是.