Spring Boot 记录器方面

2024-02-10

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

我所有的课程都位于org.my.package包,即org.my.package.controller, org.my.package.model, etc.

这是我的应用程序类:

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);
    }
}

这是我的配置类:

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();
    }
}

这是我的方面类:

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() + " *****");
    }
}

这些是我的 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") 
}

我是否遗漏了某些内容或错误配置了我的 Aspect 类?在与其他类似的 Stack Overflow 问题和在线教程进行验证后,我找不到任何错误。

请指教。


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

你可能想要的是execution( * org.my.package..*.*(..))注意..代替..

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Boot 记录器方面 的相关文章

随机推荐