This repository demonstrates key features of the Simple Logging Facade for Java (SLF4J), including integration with popular logging frameworks and advanced logging techniques. Whether you're a beginner or an experienced Java developer, this project will help you understand and use SLF4J effectively.
SLF4J (Simple Logging Facade for Java) provides a unified logging interface for Java applications. It allows developers to write logging code independent of the underlying logging framework (e.g., Logback, Log4j, Java Util Logging).
Key benefits:
- Simplifies switching between logging frameworks.
- Supports advanced logging features like Markers and Mapped Diagnostic Contexts (MDC).
- Clean, simple API.
- Basic logging levels:
TRACE
,DEBUG
,INFO
,WARN
,ERROR
- Parameterized logging
- Integration with popular frameworks:
- Logback
- Log4j2
- Java Util Logging (JUL)
- Advanced features:
- Markers for contextual logging
- MDC for diagnostic context management
- Exception handling and logging best practices
slf4j-examples/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ ├── com/example/
│ │ │ │ ├── basics/ # Basic SLF4J examples
│ │ │ │ ├── bindings/ # Integrating with logging frameworks
│ │ │ │ ├── advanced/ # Advanced SLF4J features
│ │ │ │ ├── configuration/ # Configuration files
│ ├── test/ # Unit tests
├── lib/ # Optional JAR files
├── docs/ # Documentation
├── build.gradle # Gradle build file
├── pom.xml # Maven build file
├── README.md # Project documentation
├── .gitignore # Ignored files
- Java 8 or higher
- Gradle or Maven installed
git clone https://github.com/rvguradiya/slf4j-examples.git
cd slf4j-examples
gradle build
mvn clean install
Navigate to the src/main/java/com/example
directory and run the desired examples using your IDE or a command-line tool.
- HelloWorldLogger: Simple example demonstrating SLF4J logging.
- ParameterizedLogging: Demonstrates how to use placeholders for efficient logging.
- LoggingLevelsExample: Explains the different logging levels.
- LogbackExample: Integration with Logback.
- Log4j2Example: Integration with Log4j2.
- JULExample: Integration with Java Util Logging.
- MDCExample: Demonstrates how to use Mapped Diagnostic Contexts.
- MarkerExample: Shows how to use Markers for filtering and categorization.
- ExceptionHandlingExample: Best practices for logging exceptions.
Contributions are welcome! If you have additional examples or enhancements, feel free to submit a pull request.
- Fork the repository
- Create a feature branch (
git checkout -b feature-name
) - Commit your changes (
git commit -m 'Add feature'
) - Push to your branch (
git push origin feature-name
) - Open a pull request
This project is licensed under the MIT License. See the LICENSE file for details.