log4j-utils is a collection of high-performance, community-driven plugins for Apache Log4j. This project provides "plug-and-play" extensions designed to solve common logging challenges—such as sensitive data masking and message transformation—without requiring custom Java code in every microservice.
- Easy Configuration: Simplified programmatic setup for Log4j.
- Modern Java Support: Fully compatible with Java 8 through Java 21+.
- Checkstyle Verified: Follows Google Java Style for clean, maintainable code.
- Log4j Integration: Native support for Log4j Core version
2.24.3.
The MessageRewritePolicy allows you to scrub or transform log messages globally using configurable regex patterns
directly in your log4j2.xml.
- Loosely Coupled: Decouples your masking logic from your application code.
- Easy Configuration: Update security policies (like masking new token formats) via XML without redeploying code.
Add the following dependency to your pom.xml:
<dependency>
<groupId>in.org.nnm</groupId>
<artifactId>log4j-utils</artifactId>
<version>1.0.0</version>
</dependency>For Gradle users:
implementation("in.org.nnm:log4j-utils:1.0.0")
To use these utilities in your project, ensure you have the necessary Log4j dependencies configured.
<Rewrite name="MASKING_REWRITE">
<MessageRewritePolicy>
<replace regex="([A-Za-z0-9._%+-])([A-Za-z0-9._%+-]*)(@[A-Za-z0-9.-]+\.[A-Za-z]{2,})"
replacement="[EMAIL]"/>
<replace regex="\+?1?\s*\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}"
replacement="[PHONE]"/>
<replace> <!-- Uses a CDATA Section -->
<regex><![CDATA[[A-Za-z0-9_%+-]{20,}\.[A-Za-z0-9_%+-]{20,}\.[A-Za-z0-9_%+-]+\b]]></regex>
<replacement>[TOKEN]</replacement>
</replace>
</MessageRewritePolicy>
<Appender-Ref ref="Console"/>
</Rewrite>rewrite:
- name: MASKING_REWRITE
MessageRewritePolicy:
replace:
- regex: "([A-Za-z0-9._%+-])([A-Za-z0-9._%+-]*)(@[A-Za-z0-9.-]+\\.[A-Za-z]{2,})"
replacement: "[EMAIL MASKED]"
- regex: "\\+?1?\\s*\\(?\\d{3}\\)?[\\s.-]?\\d{3}[\\s.-]?\\d{4}"
replacement: "[PHONE]"
- regex: "[A-Za-z0-9_%+-]{20,}\\.[A-Za-z0-9_%+-]{20,}\\.[A-Za-z0-9_%+-]+\\b"
replacement: "[TOKEN]"
AppenderRef:
- ref: Console👉 log4j configuration files : Log4j Configuration Examples
To see log4j-utils in action within a Spring Boot/Java environment, check out the demo implementation:
- Repository: logging-activity
Contributions are welcome! If you have a Log4j plugin that solves a common problem, feel free to:
- open an issue
- Fork the repository.
- Create a feature branch.
- Submit a Pull Request.
Developed by Neelesh Mehar
- Website: nnm.org.in
- GitHub: @Neel1210
Special thanks to the Apache Log4j Maintainers for their guidance on this project.