Spring Boot Logger方面 [英] Spring Boot Logger Aspects

查看:146
本文介绍了Spring Boot Logger方面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当访问特定包的类中的方法时,我在记录日志信息时遇到问题。换句话说,发生不记录。我甚至绝望并添加了System.out.println语句,没有运气。

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.

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

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

这是我的Application类:

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

这是我的配置类:

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

这是我的Aspect类:

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

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

These are my Gradle build dependencies:

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问题和在线教程进行验证后,我找不到任何错误。

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.

请提供建议。

推荐答案

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

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

你可能想要的是执行(* org.my.package .. *。*(..))注意 .. 而不是

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

这篇关于Spring Boot Logger方面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆