Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Logger Aspects

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.

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

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:

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.

Please advise.

like image 685
Rick Avatar asked May 19 '15 20:05

Rick


People also ask

What is logging aspect in spring boot?

Spring AOP provides the way to dynamically add the cross-cutting concern before, after, or around the actual logic using simple pluggable configurations. It makes it easy to maintain code in the present and future as well. Examples of cross-cutting concerns: Logging.

How do you configure logger in spring boot?

Spring Boot has a LoggingSystem abstraction that attempts to configure logging based on the content of the classpath. If Logback is available, it is the first choice. You can also set the location of a file to which to write the log (in addition to the console) by using "logging. file".

What is aspect in spring AOP?

Aspect: An aspect is a class that implements enterprise application concerns that cut across multiple classes, such as transaction management. Aspects can be a normal class configured through Spring XML configuration or we can use Spring AspectJ integration to define a class as Aspect using @Aspect annotation.

What is AOP logging?

AOP is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does this by adding additional behavior to existing code without modifying the code itself. Instead, we can declare the new code and the new behaviors separately.


1 Answers

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

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

like image 105
M. Deinum Avatar answered Oct 23 '22 04:10

M. Deinum