Skip to content

Add the Log4j2 appender for log collection as an optional add-on.#6

Closed
Carlosho23 wants to merge 4 commits into
JohnPitter:mainfrom
Carlosho23:feature-add-log4j2
Closed

Add the Log4j2 appender for log collection as an optional add-on.#6
Carlosho23 wants to merge 4 commits into
JohnPitter:mainfrom
Carlosho23:feature-add-log4j2

Conversation

@Carlosho23
Copy link
Copy Markdown
Contributor

Adds support for capturing logs from Log4j2, mirroring the existing Logback integration. This allows applications using Log4j2 as their logging framework to have their logs collected and displayed within the J-Obs UI.

  • JObsLog4j2Appender: A new Log4j2 appender that captures log events, sanitizes them, and forwards them to the LogRepository.

  • It extracts traceId and spanId from the MDC or falls back to the current OpenTelemetry SpanContext.

  • It avoids circular logging by ignoring log events originating from the io.github.jobs packages.

  • Gracefully handles the absence of the OpenTelemetry API on the classpath.

  • JObsLogAutoConfiguration: Updated to include a new Log4j2Configuration inner class.

  • This configuration automatically registers the JObsLog4j2Appender bean and attaches it to the Log4j2 root logger.

  • It is conditionally enabled only when Log4j2 is on the classpath and the Logback appender is not present, ensuring no conflicts.

The J-Obs UI dashboard previously constructed URLs using only its own base path (`j-obs.path`, defaulting to `/j-obs`), which caused all links and asset requests to fail with a 404 when the application was deployed under a `server.servlet.context-path`.

This commit refactors `TemplateService` to be aware of the application's context path.

- `TemplateService` now injects `Environment` to read `server.servlet.context-path`.
- A new `fullPath()` method combines the application's context path with the J-Obs path (e.g., `/my-app/j-obs`).
- All controllers and template rendering logic now use `templateService.fullPath()` to generate correct, context-aware URLs for `{{BASE_PATH}}`.
- The API Docs page (`api-docs.html`) is updated to use a new `{{CONTEXT_PATH}}` variable for Swagger UI and OpenAPI spec links, ensuring they are also correctly prefixed.

This change ensures that the J-Obs UI is fully functional when hosted within a Spring Boot application that has a global context path configured.
Adds support for capturing logs from Log4j2, mirroring the existing Logback integration. This allows applications using Log4j2 as their logging framework to have their logs collected and displayed within the J-Obs UI.

- **`JObsLog4j2Appender`**: A new Log4j2 appender that captures log events, sanitizes them, and forwards them to the `LogRepository`.
 - It extracts `traceId` and `spanId` from the MDC or falls back to the current OpenTelemetry `SpanContext`.
 - It avoids circular logging by ignoring log events originating from the `io.github.jobs` packages.
 - Gracefully handles the absence of the OpenTelemetry API on the classpath.

- **`JObsLogAutoConfiguration`**: Updated to include a new `Log4j2Configuration` inner class.
 - This configuration automatically registers the `JObsLog4j2Appender` bean and attaches it to the Log4j2 root logger.
 - It is conditionally enabled only when Log4j2 is on the classpath and the Logback appender is not present, ensuring no conflicts.
…re-add-log4j2

# Conflicts:
#	j-obs-spring-boot-starter/src/main/java/io/github/jobs/spring/web/template/TemplateService.java
The Log4j2 appender auto-configuration has been refined to ensure correct conditional activation and robustness, particularly when Logback is also present or Log4j2 is bridged to SLF4J.

- **`JObsLogAutoConfiguration`**:
    - The `Log4j2Configuration` now uses `@ConditionalOnMissingClass("ch.qos.logback.classic.Logger")` instead of `@ConditionalOnMissingBean` to determine if Logback is absent. This ensures Logback takes priority by default (as is common in Spring Boot applications) and the Log4j2 appender only activates when Logback is explicitly not on the classpath.
    - The logic for attaching the `JObsLog4j2Appender` to the Log4j2 root logger has been made more resilient. It now explicitly checks if `LogManager.getContext()` returns a native `org.apache.logging.log4j.core.LoggerContext` before adding the appender, preventing issues when Log4j2 is bridged to SLF4J.
- **`JObsLogAutoConfigurationTest`**:
    - Added new tests to verify the conditional logic:
        - Confirms that the Logback appender is used when both Logback and Log4j2 are present.
        - Confirms that the Log4j2 appender is used when Logback is absent from the classpath.

This ensures reliable and correct logging appender activation based on the available logging frameworks.
JohnPitter added a commit that referenced this pull request May 6, 2026
* refactor: respect server.servlet.context-path for all UI URLs

The J-Obs UI dashboard previously constructed URLs using only its own base path (`j-obs.path`, defaulting to `/j-obs`), which caused all links and asset requests to fail with a 404 when the application was deployed under a `server.servlet.context-path`.

This commit refactors `TemplateService` to be aware of the application's context path.

- `TemplateService` now injects `Environment` to read `server.servlet.context-path`.
- A new `fullPath()` method combines the application's context path with the J-Obs path (e.g., `/my-app/j-obs`).
- All controllers and template rendering logic now use `templateService.fullPath()` to generate correct, context-aware URLs for `{{BASE_PATH}}`.
- The API Docs page (`api-docs.html`) is updated to use a new `{{CONTEXT_PATH}}` variable for Swagger UI and OpenAPI spec links, ensuring they are also correctly prefixed.

This change ensures that the J-Obs UI is fully functional when hosted within a Spring Boot application that has a global context path configured.

* feat: add Log4j2 appender for log collection

Adds support for capturing logs from Log4j2, mirroring the existing Logback integration. This allows applications using Log4j2 as their logging framework to have their logs collected and displayed within the J-Obs UI.

- **`JObsLog4j2Appender`**: A new Log4j2 appender that captures log events, sanitizes them, and forwards them to the `LogRepository`.
 - It extracts `traceId` and `spanId` from the MDC or falls back to the current OpenTelemetry `SpanContext`.
 - It avoids circular logging by ignoring log events originating from the `io.github.jobs` packages.
 - Gracefully handles the absence of the OpenTelemetry API on the classpath.

- **`JObsLogAutoConfiguration`**: Updated to include a new `Log4j2Configuration` inner class.
 - This configuration automatically registers the `JObsLog4j2Appender` bean and attaches it to the Log4j2 root logger.
 - It is conditionally enabled only when Log4j2 is on the classpath and the Logback appender is not present, ensuring no conflicts.

* fix: improve conditional activation and robustness of Log4j2 appender

The Log4j2 appender auto-configuration has been refined to ensure correct conditional activation and robustness, particularly when Logback is also present or Log4j2 is bridged to SLF4J.

- **`JObsLogAutoConfiguration`**:
    - The `Log4j2Configuration` now uses `@ConditionalOnMissingClass("ch.qos.logback.classic.Logger")` instead of `@ConditionalOnMissingBean` to determine if Logback is absent. This ensures Logback takes priority by default (as is common in Spring Boot applications) and the Log4j2 appender only activates when Logback is explicitly not on the classpath.
    - The logic for attaching the `JObsLog4j2Appender` to the Log4j2 root logger has been made more resilient. It now explicitly checks if `LogManager.getContext()` returns a native `org.apache.logging.log4j.core.LoggerContext` before adding the appender, preventing issues when Log4j2 is bridged to SLF4J.
- **`JObsLogAutoConfigurationTest`**:
    - Added new tests to verify the conditional logic:
        - Confirms that the Logback appender is used when both Logback and Log4j2 are present.
        - Confirms that the Log4j2 appender is used when Logback is absent from the classpath.

This ensures reliable and correct logging appender activation based on the available logging frameworks.

* chore(release): 1.3.0

Bump all module versions to 1.3.0, update README and BOM, and document
the new optional Log4j2 appender (PR #6) in CHANGELOG.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Carlosho <c.oliveira@nextage.com.br>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@JohnPitter
Copy link
Copy Markdown
Owner

Merged into main via release PR #7 (v1.3.0). Thank you for the contribution, @Carlosho23! 🎉

@JohnPitter JohnPitter closed this May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants