Skip to content

Commit eec30fe

Browse files
committed
Added logging for public methods of classes that extend DaoSupport and refactored common AOP config into common pointcut
1 parent b83f29d commit eec30fe

File tree

11 files changed

+326
-21
lines changed

11 files changed

+326
-21
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
[![build_status](https://github.com/spt-development/spt-development-logging-spring/actions/workflows/build.yml/badge.svg)](https://github.com/spt-development/spt-development-logging-spring/actions)
1212

1313
A library for adding logging (at the start, end and on exception) to public methods of classes annotated with
14-
`@RestController`, `@Service` or `@Repository` or methods annotated with `@JmsListener`.
14+
`@RestController`, `@Service` or `@Repository` or methods annotated with `@JmsListener` or methods of sub-classes
15+
of `org.springframework.dao.support.DaoSupport`.
1516

1617
Usage
1718
=====
@@ -21,6 +22,10 @@ Register the Aspects as Spring Beans manually or by adding the
2122
starter to your project's pom.
2223

2324
```java
25+
import com.spt.development.logging.spring.DaoSupportLogger;
26+
27+
import java.beans.BeanProperty;
28+
2429
@Bean
2530
public RestControllerLogger restControllerLogger() {
2631
return new RestControllerLogger();
@@ -40,8 +45,18 @@ public ServiceLogger serviceLogger() {
4045
public RepositoryLogger repositoryLogger() {
4146
return new RepositoryLogger();
4247
}
48+
49+
@Bean
50+
public DaoSupportLogger daoSupportLogger() {
51+
return new DaoSupportLogger();
52+
}
4353
```
4454

55+
*NOTE* The `DaoSupportLogger` will result in warnings such as the following being logged by `CglibAopProxy`:
56+
57+
`Unable to proxy interface-implementing method [public final void org.springframework.dao.support.DaoSupport.afterPropertiesSet() throws java.lang.IllegalArgumentException,org.springframework.beans.factory.BeanInitializationException]
58+
because it is marked as final, consider using interface-based JDK proxies instead.`
59+
4560
Building locally
4661
================
4762

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## New Features
2+
3+
* Added logging for public methods of classes that extend `org.springframework.dao.support.DaoSupport`.
4+
5+
## Dependencies
6+
7+
* Aligned dependencies with [Spring Boot 3.2.5](https://github.com/spring-projects/spring-boot/releases/tag/v3.2.5)

pom.xml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>com.spt-development</groupId>
55
<artifactId>spt-development-logging-spring</artifactId>
6-
<version>3.0.17-SNAPSHOT</version>
6+
<version>3.1.0-SNAPSHOT</version>
77

88
<name>logging-spring</name>
99
<description>A very simple library for getting/setting the current correlation ID, utilising ThreadLocal.</description>
@@ -439,7 +439,7 @@
439439
Any classes / packages excluded here, must have specific rules for them below,
440440
with reasons.
441441
-->
442-
<exclude>com.spt.development.audit.spring.security.AuthenticationAdapterFactory</exclude>
442+
<exclude>com.spt.development.logging.spring.LoggerAspect</exclude>
443443
</excludes>
444444
</rule>
445445
<rule>
@@ -448,15 +448,12 @@
448448
<limit>
449449
<counter>LINE</counter>
450450
<value>COVEREDRATIO</value>
451-
<minimum>0.82</minimum>
451+
<minimum>0.97</minimum>
452452
</limit>
453453
</limits>
454454
<includes>
455-
<!--
456-
Difficult to force the ClassNotFoundException when checking if authentication
457-
object is an instance of OAuth2Authentication
458-
-->
459-
<include>com.spt.development.audit.spring.security.AuthenticationAdapterFactory</include>
455+
<!-- Placeholder method for pointcut does not need to be explicitly covered by tests -->
456+
<include>com.spt.development.logging.spring.LoggerAspect</include>
460457
</includes>
461458
</rule>
462459
</rules>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.spt.development.logging.spring;
2+
3+
import org.aspectj.lang.ProceedingJoinPoint;
4+
import org.aspectj.lang.annotation.Around;
5+
import org.aspectj.lang.annotation.Aspect;
6+
7+
/**
8+
* Logs calls to all public methods belonging to classes with the <code>org.springframework.stereotype.Repository</code>
9+
* annotation.
10+
*/
11+
@Aspect
12+
public class DaoSupportLogger extends LoggerAspect {
13+
14+
/**
15+
* Creates a new instance of the logger aspect. The log statements added by the aspect will include the current
16+
* correlation ID; see {@link DaoSupportLogger#DaoSupportLogger(boolean)} to disable this behaviour.
17+
*/
18+
public DaoSupportLogger() {
19+
this(true);
20+
}
21+
22+
/**
23+
* Creates a new instance of the logger aspect.
24+
*
25+
* @param includeCorrelationIdInLogs a flag to determine whether the correlation ID should be explicitly included
26+
* in the log statements added by the aspect.
27+
*/
28+
public DaoSupportLogger(final boolean includeCorrelationIdInLogs) {
29+
super(includeCorrelationIdInLogs);
30+
}
31+
32+
/**
33+
* Outputs DEBUG level logging when a public method belonging to a class, extending
34+
* <code>org.springframework.dao.support.DaoSupport</code> is called and when it returns (without
35+
* exception). If TRACE level logging is enabled and the method has a non-<code>void</code> return type, the
36+
* return value will be included in the logging. For example:
37+
*
38+
* <pre>
39+
* [40872057-a1b6-4fdd-bce1-7882929bbce6] MyDao.read(4)
40+
* ...
41+
* [40872057-a1b6-4fdd-bce1-7882929bbce6] MyDao.read Returned: MyEntity(id=4, name=test)
42+
* </pre>
43+
*
44+
* @param point the aspect join point required for implementing a {@link Around} aspect.
45+
*
46+
* @return the value returned from the method logged.
47+
*
48+
* @throws Throwable thrown if the method logged throws a {@link Throwable}.
49+
*/
50+
@Override
51+
@Around("execution(public * org.springframework.dao.support.DaoSupport+.*(..)) && !loggingDisabled()")
52+
public Object log(final ProceedingJoinPoint point) throws Throwable {
53+
return super.log(point);
54+
}
55+
}

src/main/java/com/spt/development/logging/spring/JmsListenerLogger.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ public JmsListenerLogger(final boolean includeCorrelationIdInLogs) {
4949
* @throws Throwable thrown if the method logged throws a {@link Throwable}.
5050
*/
5151
@Override
52-
@Around("@annotation(org.springframework.jms.annotation.JmsListener) "
53-
+ "&& !@annotation(com.spt.development.logging.NoLogging) "
54-
+ "&& !@target(com.spt.development.logging.NoLogging)")
52+
@Around("@annotation(org.springframework.jms.annotation.JmsListener) && !loggingDisabled()")
5553
public Object log(final ProceedingJoinPoint point) throws Throwable {
5654
return super.log(point);
5755
}

src/main/java/com/spt/development/logging/spring/LoggerAspect.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.spt.development.cid.CorrelationId;
44
import org.aspectj.lang.ProceedingJoinPoint;
5+
import org.aspectj.lang.annotation.Pointcut;
56
import org.aspectj.lang.reflect.MethodSignature;
67
import org.slf4j.Logger;
78
import org.slf4j.LoggerFactory;
@@ -24,6 +25,9 @@ abstract class LoggerAspect {
2425
this.isStartAndCompleteMethodLoggedAtInfo = isStartAndCompleteMethodLoggedAtInfo;
2526
}
2627

28+
@Pointcut("@annotation(com.spt.development.logging.NoLogging) || @target(com.spt.development.logging.NoLogging)")
29+
void loggingDisabled() {}
30+
2731
Object log(final ProceedingJoinPoint point) throws Throwable {
2832
final MethodSignature signature = (MethodSignature) point.getSignature();
2933
final Logger log = LoggerFactory.getLogger(signature.getDeclaringType());

src/main/java/com/spt/development/logging/spring/RepositoryLogger.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ public RepositoryLogger(final boolean includeCorrelationIdInLogs) {
4848
* @throws Throwable thrown if the method logged throws a {@link Throwable}.
4949
*/
5050
@Override
51-
@Around("@within(org.springframework.stereotype.Repository) "
52-
+ "&& !@annotation(com.spt.development.logging.NoLogging) "
53-
+ "&& !@target(com.spt.development.logging.NoLogging)")
51+
@Around("@within(org.springframework.stereotype.Repository) && !loggingDisabled()")
5452
public Object log(final ProceedingJoinPoint point) throws Throwable {
5553
return super.log(point);
5654
}

src/main/java/com/spt/development/logging/spring/RestControllerLogger.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ public RestControllerLogger(final boolean includeCorrelationIdInLogs) {
6060
* @throws Throwable thrown if the method logged throws a {@link Throwable}.
6161
*/
6262
@Override
63-
@Around("@within(org.springframework.web.bind.annotation.RestController) "
64-
+ "&& !@annotation(com.spt.development.logging.NoLogging) "
65-
+ "&& !@target(com.spt.development.logging.NoLogging)")
63+
@Around("@within(org.springframework.web.bind.annotation.RestController) && !loggingDisabled()")
6664
public Object log(final ProceedingJoinPoint point) throws Throwable {
6765
return super.log(point);
6866
}

src/main/java/com/spt/development/logging/spring/ServiceLogger.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ public ServiceLogger(final boolean includeCorrelationIdInLogs) {
4848
* @throws Throwable thrown if the method logged throws a {@link Throwable}.
4949
*/
5050
@Override
51-
@Around("@within(org.springframework.stereotype.Service) "
52-
+ "&& !@annotation(com.spt.development.logging.NoLogging) "
53-
+ "&& !@target(com.spt.development.logging.NoLogging)")
51+
@Around("@within(org.springframework.stereotype.Service) && !loggingDisabled()")
5452
public Object log(final ProceedingJoinPoint point) throws Throwable {
5553
return super.log(point);
5654
}

0 commit comments

Comments
 (0)