spring-log-utils
is a library designed to enhance logging capabilities in Spring-based applications. It provides features for logging HTTP methods, specific endpoints, and methods, as well as masking sensitive data in logs. The library leverages Spring Boot's autoconfiguration mechanism to simplify integration and usage.
This library simplifies logging in Spring applications, making it easier to monitor and debug while ensuring sensitive data is protected.
-
Global HTTP Methods Logging:
- Automatically logs HTTP method executions based on configuration properties.
-
@HttpMethodLogExecution
Annotation:- Enables logging for specific HTTP endpoints.
-
@LogExecution
Annotation:- Logs the execution of specific methods.
-
@MaskSensitiveData
Annotation:- Masks sensitive fields in logs to protect sensitive information.
To use this library in a Maven project, add the following dependency to your pom.xml
:
<dependency>
<groupId>io.github.open-source-lfernandes</groupId>
<artifactId>spring-log-utils</artifactId>
<version>1.0.0</version>
</dependency>
To use this library in a Gradle project, add the following dependency to your build.gradle
:
implementation 'io.github.open-source-lfernandes:spring-log-utils:1.0.0'
Configure logging for HTTP methods globally using Spring Boot configuration properties. Example:
spring:
log-utils:
http-methods:
enabled: true
Use this annotation to log specific HTTP endpoints:
@RestController
@RequestMapping("/greetings")
@RequiredArgsConstructor
public class GreetingController {
private final GreetingService service;
@HttpMethodLogExecution
@GetMapping
public ResponseEntity<List<User>> getUsers() {
return ResponseEntity.ok(service.getUser());
}
}
2025-05-13 15:59:15 app=example-secret-app [http-nio-8080-exec-3] [:] io.github.ice_lfernandes.spring.log.utils.commons.LoggingCommonsMethods INFO M=logInterceptJoinPoint, stage=init, method=getUsers, class=GreetingController, parameters=[]
2025-05-13 15:59:15 app=example-secret-app [http-nio-8080-exec-3] [:] io.github.ice_lfernandes.spring.log.utils.commons.LoggingCommonsMethods INFO M=logInterceptJoinPoint, stage=finish, method=getUsers, class=GreetingController, parameters=[], result=<200 OK OK,[User{email=******des***.com, name=**s****es, address=*a****o*45, age=**, birthDate=***5-13, phone=***9**9, document=********911}],[]>, time-execution=5ms
Use this annotation to log the execution of specific methods:
@Service
public class GreetingService {
@LogExecution
public User getUser(Long id) {
return User.builder()
.name("Lucas Fernandes")
.email("lucas.fernandes@gmail.com")
.document("12345678911")
.birthDate(LocalDate.now())
.age(31)
.phone("11 9999-9999")
.address("Rua Flamengo 745")
.build();
}
}
2025-05-13 16:22:08 app=example-secret-app [http-nio-8080-exec-1] [:] io.github.ice_lfernandes.spring.log.utils.commons.LoggingCommonsMethods INFO M=logInterceptJoinPoint, stage=init, method=getUser, class=GreetingService, parameters=[1]
2025-05-13 16:22:08 app=example-secret-app [http-nio-8080-exec-1] [:] io.github.ice_lfernandes.spring.log.utils.commons.LoggingCommonsMethods INFO M=logInterceptJoinPoint, stage=finish, method=getUser, class=GreetingService, parameters=[1], result=User{email=******des***.com, name=**s****es, address=*a****o*45, age=**, birthDate=***5-13, phone=***9**9, document=********911}, time-execution=2ms
Use this annotation to mask sensitive fields in logs:
public record User(
@MaskSensitiveData(maskedType = MaskedType.EMAIL) String email,
@MaskSensitiveData(maskedType = MaskedType.NAME) String name,
@MaskSensitiveData(maskedType = MaskedType.ADDRESS) String address,
@MaskSensitiveData(maskedType = MaskedType.NUMBER) Integer age,
@MaskSensitiveData(maskedType = MaskedType.DATE) LocalDate birthDate,
@MaskSensitiveData(maskedType = MaskedType.TELEPHONE) String phone,
@MaskSensitiveData(maskedType = MaskedType.DOCUMENT) String document) implements LogMask {
@Override
public String toString() {
return mask(this);
}
}
ALL
: Masks all characters.EMAIL
: Masks email addresses except the last 4 characters and domain.DOCUMENT
: Masks all but the last 3 characters.NAME
: Masks all characters except the first and last two.DATE
: Masks all characters except the last 3, excluding separators.ADDRESS
: Masks all characters except the last 3, excluding commas and spaces.ZIP_CODE
: Masks all characters except the last 3, excluding hyphens.NUMBER
: Masks all numeric characters.TELEPHONE
: Masks all characters except the last 2, excluding hyphens.
The customMaskRegex
attribute allows you to define a custom regular expression for masking sensitive data. This takes precedence over the predefined maskedType
.
public class User {
@MaskSensitiveData(customMaskRegex = "\\d{4}-\\d{2}-\\d{2}")
private String dateOfBirth;
@MaskSensitiveData(maskedType = MaskedType.EMAIL)
private String email;
// Getters and setters
}