Skip to content

Commit 018b2f8

Browse files
committed
update docs
1 parent 37cc1fa commit 018b2f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+4406
-153
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This README provides quickstart instructions on running [`fw-commons`]() on bare
1111
[![Maven Central](https://img.shields.io/badge/maven--central-1.1.0-orange.svg?style=plastic&logo=apachemaven)](https://central.sonatype.com/artifact/io.github.hoangtien2k3/fw-commons/1.1.0)
1212
[![Gradle](https://img.shields.io/badge/gradle-1.1.0-orange.svg?style=plastic&logo=apachemaven)](https://central.sonatype.com/artifact/io.github.hoangtien2k3/fw-commons/1.1.0)
1313

14-
### ⬇️ [Download From Gradle and Maven Central](https://central.sonatype.com/artifact/cn.ponfee/commons-core/1.4)
14+
#### ⬇️ [Download From Gradle and Maven Central](https://central.sonatype.com/artifact/io.github.hoangtien2k3/fw-commons/1.1.0)
1515

1616
#### Gradle
1717

pom.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<developer>
2828
<name>Hoang Anh Tien</name>
2929
<email>[email protected]</email>
30-
<organization>io.hoangtien2k3</organization>
30+
<organization>io.github.hoangtien2k3</organization>
3131
<organizationUrl>https://github.com/hoangtien2k3</organizationUrl>
3232
</developer>
3333
</developers>
@@ -433,6 +433,7 @@
433433
<version>${maven-javadoc-plugin.version}</version>
434434
<configuration>
435435
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
436+
<failOnError>false</failOnError>
436437
</configuration>
437438
<executions>
438439
<execution>
@@ -507,7 +508,8 @@
507508
<removeUnusedImports/>
508509
<palantirJavaFormat/>
509510
<licenseHeader>
510-
<content><![CDATA[
511+
<content>
512+
<![CDATA[
511513
/*
512514
* Copyright $YEAR author - Hoàng Anh Tiến
513515
*
@@ -522,7 +524,9 @@
522524
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
523525
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
524526
*/
525-
]]></content>
527+
528+
]]>
529+
</content>
526530
</licenseHeader>
527531
</java>
528532
</configuration>

src/main/java/io/hoangtien2k3/commons/annotations/LogPerformance.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,108 @@
1919
import java.lang.annotation.RetentionPolicy;
2020
import java.lang.annotation.Target;
2121

22+
/**
23+
* The {@code LogPerformance} annotation is used to mark methods or classes for
24+
* performance logging. It allows developers to specify what type of information
25+
* should be logged when the annotated method or class is executed, including
26+
* input arguments, output results, and other custom information.
27+
*
28+
* <p>
29+
* This annotation is typically applied to methods in a service layer where
30+
* performance monitoring is essential. The annotation can be configured to log
31+
* specific types of data, such as input arguments, output results, and the
32+
* duration of method execution. The logged information can then be used for
33+
* performance analysis, debugging, and auditing purposes.
34+
* </p>
35+
*
36+
* <h2>Example Usage:</h2>
37+
*
38+
* <pre>{@code
39+
* @LogPerformance(logType = "API_CALL", actionType = "FETCH_DATA", logInput = true, logOutput = true, title = "Fetching Data")
40+
* public Mono<DataResponse> fetchData(String id) {
41+
* // Method implementation
42+
* }
43+
* }</pre>
44+
*
45+
* <p>
46+
* In this example, the `fetchData` method is annotated with
47+
* {@code @LogPerformance}, which will log the input arguments, output results,
48+
* and execution duration when the method is called. The log will include the
49+
* specified `logType`, `actionType`, and `title`.
50+
* </p>
51+
*
52+
* <h2>Retention and Target:</h2>
53+
* <p>
54+
* The annotation is retained at runtime ({@link RetentionPolicy#RUNTIME}) and
55+
* can be applied to both methods and types (classes or interfaces)
56+
* ({@link ElementType#METHOD}, {@link ElementType#TYPE}).
57+
* </p>
58+
*/
2259
@Target({ElementType.METHOD, ElementType.TYPE})
2360
@Retention(RetentionPolicy.RUNTIME)
2461
public @interface LogPerformance {
62+
63+
/**
64+
* Specifies the type of log entry, such as "API_CALL", "DB_QUERY", etc.
65+
* <p>
66+
* This value helps categorize logs based on the type of operation being logged.
67+
* For instance, you might use different log types for API calls versus database
68+
* queries to differentiate them in your log analysis.
69+
* </p>
70+
*
71+
* @return the type of log entry
72+
*/
2573
String logType() default "";
2674

75+
/**
76+
* Specifies the type of action being performed, such as "CREATE", "UPDATE",
77+
* "DELETE", etc.
78+
* <p>
79+
* This value can be used to identify what kind of action is being logged,
80+
* providing more context for the log entry. For example, you might have actions
81+
* like "FETCH_DATA", "SAVE_ENTITY", etc.
82+
* </p>
83+
*
84+
* @return the type of action being logged
85+
*/
2786
String actionType() default "";
2887

88+
/**
89+
* Indicates whether the output (result) of the method should be logged.
90+
* <p>
91+
* When set to {@code true}, the output of the method will be logged, provided
92+
* it can be serialized. This is useful for tracing the results of method
93+
* executions, especially when debugging or monitoring system behavior.
94+
* </p>
95+
*
96+
* @return {@code true} if the output should be logged, {@code false} otherwise
97+
*/
2998
boolean logOutput() default true;
3099

100+
/**
101+
* Indicates whether the input arguments of the method should be logged.
102+
* <p>
103+
* When set to {@code true}, the input arguments to the method will be logged.
104+
* This is useful for capturing the state of the inputs when the method was
105+
* called, which can be important for understanding the context of the log
106+
* entry.
107+
* </p>
108+
*
109+
* @return {@code true} if the input arguments should be logged, {@code false}
110+
* otherwise
111+
*/
31112
boolean logInput() default true;
32113

114+
/**
115+
* Specifies a custom title for the log entry.
116+
* <p>
117+
* This title can be used to provide a brief description of the logged
118+
* operation, making it easier to identify and search for specific log entries
119+
* in your logging system. For example, the title might be something like "User
120+
* Login Attempt" or "Order Processing".
121+
* </p>
122+
*
123+
* @return the custom title for the log entry
124+
*/
33125
String title() default "";
34126
}

src/main/java/io/hoangtien2k3/commons/annotations/cache/CustomizeRemovalListener.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,62 @@
2222
import org.checkerframework.checker.nullness.qual.NonNull;
2323
import org.checkerframework.checker.nullness.qual.Nullable;
2424

25+
/**
26+
* This class implements the {@link RemovalListener} interface and provides
27+
* custom handling for cache entry removals. It logs information when a cache
28+
* entry is evicted and invokes a specified method.
29+
*
30+
* <p>
31+
* The class uses Lombok annotations {@code @Slf4j} for logging and
32+
* {@code @AllArgsConstructor} to generate a constructor with one parameter for
33+
* each field in the class.
34+
*
35+
* <p>
36+
* This listener is typically used in caching mechanisms where specific actions
37+
* need to be taken when an entry is removed due to eviction.
38+
*
39+
* <p>
40+
* The listener relies on a {@link Method} instance, which is passed through the
41+
* constructor. This method is invoked whenever a cache entry associated with it
42+
* is evicted.
43+
*
44+
* <p>
45+
* Example usage:
46+
*
47+
* <pre>
48+
* {@code
49+
* Cache<String, Object> cache = Caffeine.newBuilder().removalListener(new CustomizeRemovalListener(someMethod))
50+
* .build();
51+
* }
52+
* </pre>
53+
*
54+
* @author Your Name
55+
*/
2556
@Slf4j
2657
@AllArgsConstructor
2758
public class CustomizeRemovalListener implements RemovalListener {
28-
private Method method;
59+
private final Method method;
2960

61+
/**
62+
* Handles the removal of a cache entry. If the removal cause is eviction, this
63+
* method logs the event and invokes the method provided during the creation of
64+
* this listener.
65+
*
66+
* @param key
67+
* the key of the removed cache entry, can be null
68+
* @param value
69+
* the value of the removed cache entry, can be null
70+
* @param removalCause
71+
* the cause of the removal, never null
72+
*/
3073
@Override
3174
public void onRemoval(@Nullable Object key, @Nullable Object value, @NonNull RemovalCause removalCause) {
3275
if (removalCause.wasEvicted()) {
33-
log.info("Cache " + method.getDeclaringClass().getSimpleName() + "." + method.getName()
34-
+ " was evicted because " + removalCause);
76+
log.info(
77+
"Cache {}.{} was evicted because {}",
78+
method.getDeclaringClass().getSimpleName(),
79+
method.getName(),
80+
removalCause);
3581
CacheUtils.invokeMethod(method);
3682
}
3783
}

src/main/java/io/hoangtien2k3/commons/annotations/logging/LoggerAspect.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,84 @@
2121
import org.aspectj.lang.annotation.Pointcut;
2222
import org.springframework.context.annotation.Configuration;
2323

24+
/**
25+
* This aspect class is responsible for logging the performance of methods in
26+
* the specified packages. It uses AOP (Aspect-Oriented Programming) to
27+
* intercept method calls and log relevant information before and after the
28+
* method execution.
29+
*
30+
* <p>
31+
* The class uses Spring AOP and relies on two pointcuts to identify which
32+
* methods should be logged:
33+
* <ul>
34+
* <li>{@code performancePointCut}: Matches methods in the specified packages
35+
* (controller, service, repository, client) except for those in the Spring Boot
36+
* Actuator package.</li>
37+
* <li>{@code logPerfMethods}: Matches methods annotated with
38+
* {@link io.hoangtien2k3.commons.annotations.LogPerformance}.</li>
39+
* </ul>
40+
*
41+
* <p>
42+
* The {@code @Around} advice is applied to the methods matched by these
43+
* pointcuts, and it delegates the logging logic to {@link LoggerAspectUtils}.
44+
*
45+
* <p>
46+
* Example of usage:
47+
*
48+
* <pre>
49+
* {@code
50+
* @LogPerformance
51+
* public void someMethod() {
52+
* // method implementation
53+
* }
54+
* }
55+
* </pre>
56+
*
57+
* <p>
58+
* The class is annotated with {@code @Aspect} to define it as an aspect, and
59+
* {@code @Configuration} to ensure it is registered as a Spring bean. It also
60+
* uses {@code @RequiredArgsConstructor} from Lombok to generate a constructor
61+
* that injects the required dependencies.
62+
*
63+
* @author hoangtien2k3
64+
*/
2465
@Aspect
2566
@Configuration
2667
@RequiredArgsConstructor
2768
public class LoggerAspect {
69+
2870
private final LoggerAspectUtils loggerAspectUtils;
2971

72+
/**
73+
* Pointcut that matches the execution of any method within the specified
74+
* packages (controller, service, repository, client) except for those under the
75+
* Spring Boot Actuator package.
76+
*/
3077
@Pointcut("(execution(* io.hoangtien2k3.commons.*.controller..*(..)) || "
3178
+ "execution(* io.hoangtien2k3.commons.*.service..*(..)) || "
3279
+ "execution(* io.hoangtien2k3.commons.*.repository..*(..)) || "
3380
+ "execution(* io.hoangtien2k3.commons.*.client..*(..))) &&"
3481
+ "!execution(* org.springframework.boot.actuate..*(..))")
3582
public void performancePointCut() {}
3683

84+
/**
85+
* Pointcut that matches methods annotated with
86+
* {@link io.hoangtien2k3.commons.annotations.LogPerformance}.
87+
*/
3788
@Pointcut("@annotation(io.hoangtien2k3.commons.annotations.LogPerformance)")
3889
private void logPerfMethods() {}
3990

91+
/**
92+
* Around advice that wraps the matched method executions, providing logging
93+
* functionality around the method invocation. The actual logging logic is
94+
* delegated to {@link LoggerAspectUtils#logAround}.
95+
*
96+
* @param joinPoint
97+
* the join point representing the method execution
98+
* @return the result of the method execution
99+
* @throws Throwable
100+
* if the method execution throws an exception
101+
*/
40102
@Around("performancePointCut() || logPerfMethods()")
41103
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
42104
return loggerAspectUtils.logAround(joinPoint);

0 commit comments

Comments
 (0)