Skip to content

Utility helpers and extensions for Log4j2 configuration and usage

License

Notifications You must be signed in to change notification settings

Neel1210/log4j-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

log4j-utils 🚀

Maven Central License

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.


✨ Features

  • 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.

🚀 Featured Plugin: MessageRewritePolicy

The MessageRewritePolicy allows you to scrub or transform log messages globally using configurable regex patterns directly in your log4j2.xml.

Key Benefits:

  • 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.

📦 Installation

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")

🛠 Usage

To use these utilities in your project, ensure you have the necessary Log4j dependencies configured.

Configuration example log4j2.xml

<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>

Configuration example log4j2.yml

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


🚀 Demo Project

To see log4j-utils in action within a Spring Boot/Java environment, check out the demo implementation:


🤝 Contributing

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.

📬 Contact & Support

Developed by Neelesh Mehar

Special thanks to the Apache Log4j Maintainers for their guidance on this project.

About

Utility helpers and extensions for Log4j2 configuration and usage

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages