Skip to content

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

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

Add the Log4j2 appender for log collection as an optional add-on.#5
Carlosho23 wants to merge 3 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
@Carlosho23 Carlosho23 closed this Apr 28, 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.

2 participants