i use the same class LoggingAspect as in this example tutorial https://www.javaguides.net/2019/05/spring-boot-spring-aop-logging-example-tutorial.html, but when i call a controller methode i get only those messages on console
11-05-2020 13:30:27.742 [http-nio-8080-exec-7] INFO o.a.c.c.C.[.[localhost].[/appged].log - Initializing Spring DispatcherServlet 'dispatcherServlet'
11-05-2020 13:30:27.742 [http-nio-8080-exec-7] INFO o.s.web.servlet.DispatcherServlet.initServletBean - Initializing Servlet 'dispatcherServlet'
11-05-2020 13:30:27.771 [http-nio-8080-exec-7] INFO o.s.web.servlet.DispatcherServlet.initServletBean - Completed initialization in 29 ms
la classe le Logging Aspect : package com.app.ged.api.aspect;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* Aspect for logging execution of service and repository Spring components.
*
*
*/
@Aspect
@Component
public class LoggingAspect {
private final Logger log = LoggerFactory.getLogger(this.getClass());
/**
* Pointcut that matches all repositories, services and Web REST endpoints.
*/
@Pointcut("within(@org.springframework.stereotype.Repository *)" +
" || within(@org.springframework.stereotype.Service *)" +
" || within(@org.springframework.web.bind.annotation.RestController *)")
public void springBeanPointcut() {
// Method is empty as this is just a Pointcut, the implementations are in the advices.
}
/**
* Pointcut that matches all Spring beans in the application's main packages.
*/
@Pointcut("within(com.app.ged.api.repository.*)"+
" || within(com.app.ged.api.service.*)"+
" || within(com.app.ged.api.controller.*)")
public void applicationPackagePointcut() {
// Method is empty as this is just a Pointcut, the implementations are in the advices.
}
/**
* Advice that logs methods throwing exceptions.
*
* @param joinPoint join point for advice
* @param e exception
*/
@AfterThrowing(pointcut = "applicationPackagePointcut() && springBeanPointcut()", throwing = "e")
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
log.error("Exception in {}.{}() with cause = {}", joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(), e.getCause() != null? e.getCause() : "NULL");
}
/**
* Advice that logs when a method is entered and exited.
*
* @param joinPoint join point for advice
* @return result
* @throws Throwable throws IllegalArgumentException
*/
@Around("applicationPackagePointcut() && springBeanPointcut()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
if (log.isDebugEnabled()) {
log.debug("Enter: {}.{}() with argument[s] = {}", joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs()));
}
try {
Object result = joinPoint.proceed();
if (log.isDebugEnabled()) {
log.debug("Exit: {}.{}() with result = {}", joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(), result);
}
return result;
} catch (IllegalArgumentException e) {
log.error("Illegal argument: {} in {}.{}()", Arrays.toString(joinPoint.getArgs()),
joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
throw e;
}
}
}
my logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
</pattern>
</encoder>
</appender>
<appender name="SAVE-TO-FILE" class="ch.qos.logback.core.FileAppender">
<file>${LOG_PATH}/log-appGed.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
</Pattern>
</encoder>
</appender>
<springProfile name="pc">
<root level="info">
<appender-ref ref="STDOUT"/>
<appender-ref ref="SAVE-TO-FILE"/>
</root>
<logger name="com.app.ged.api.controller" additivity="false" level="debug">
<appender-ref ref="STDOUT"/>
<appender-ref ref="SAVE-TO-FILE"/>
</logger>
<logger name="com.app.ged.api.service" additivity="false" level="debug">
<appender-ref ref="STDOUT"/>
<appender-ref ref="SAVE-TO-FILE"/>
</logger>
</springProfile>
<springProfile name="dev">
<root level="info">
<appender-ref ref="STDOUT"/>
<appender-ref ref="SAVE-TO-FILE"/>
</root>
<logger name="com.app.api.service" additivity="false" level="debug">
<appender-ref ref="STDOUT"/>
<appender-ref ref="SAVE-TO-FILE"/>
</logger>
</springProfile>
<springProfile name="prod">
<root level="info">
<appender-ref ref="SAVE-TO-FILE"/>
</root>
<logger name="com.app.ged.api.service" additivity="false" level="error">
<appender-ref ref="SAVE-TO-FILE"/>
</logger>
</springProfile>
</configuration>
application.properties
#logging
spring.profiles.active=pc
logging.path=C:/logs
Active profile for the run is pc
and aspect logs are either for error
or for debug
The current root level configuration for profile pc
is info
for the application.
Either you may set the root log level as debug
instead of info
as follows . This is an application wide setting
<springProfile name="pc">
<root level="debug">
<appender-ref ref="STDOUT"/>
<appender-ref ref="SAVE-TO-FILE"/>
</root>
..
</springProfile>
or set the log level to debug
for the aspect package classes
<springProfile name="pc">
<root level="info">
<appender-ref ref="STDOUT"/>
<appender-ref ref="SAVE-TO-FILE"/>
</root>
<logger name="com.app.ged.api.aspect" additivity="false" level="debug">
<appender-ref ref="STDOUT"/>
<appender-ref ref="SAVE-TO-FILE"/>
</logger>
<logger name="com.app.ged.api.controller" additivity="false" level="debug">
<appender-ref ref="STDOUT"/>
<appender-ref ref="SAVE-TO-FILE"/>
</logger>
<logger name="com.app.ged.api.service" additivity="false" level="debug">
<appender-ref ref="STDOUT"/>
<appender-ref ref="SAVE-TO-FILE"/>
</logger>
</springProfile>