-
Notifications
You must be signed in to change notification settings - Fork 4
Sling log interceptor #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2774614
Native OSGi log interceptor
krystian-panek-vmltech 5938728
Hardening
krystian-panek-vmltech 45fd427
Native and fallback log interceptors
krystian-panek-vmltech d8cac6f
Polished
krystian-panek-vmltech d8f1ece
Sling commons log appender approach
krystian-panek-vmltech 0d69312
Sling log interceptor
krystian-panek-vmltech cee4722
Clean up
krystian-panek-vmltech 1c7c37e
Simplified
krystian-panek-vmltech 094b6b5
Clean up
krystian-panek-vmltech 0f78dc3
Hardening
krystian-panek-vmltech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
core/src/main/java/dev/vml/es/acm/core/code/log/LogInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package dev.vml.es.acm.core.code.log; | ||
|
|
||
| import java.util.function.Consumer; | ||
|
|
||
| /** | ||
| * Intercepts log messages from specified loggers for automatic capture in script output. | ||
| * | ||
| * <p>Opt-in feature controlled by {@link dev.vml.es.acm.core.code.Executor.Config#logPrintingEnabled()}. | ||
| * Scripts can always log manually via SLF4J regardless of this setting.</p> | ||
| */ | ||
| public interface LogInterceptor { | ||
|
|
||
| boolean isAvailable(); | ||
|
|
||
| Handle attach(Consumer<LogMessage> listener, String... loggerNames); | ||
|
|
||
| @FunctionalInterface | ||
| interface Handle { | ||
| void detach(); | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
core/src/main/java/dev/vml/es/acm/core/code/log/LogInterceptorManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package dev.vml.es.acm.core.code.log; | ||
|
|
||
| import java.util.List; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
| import java.util.function.Consumer; | ||
| import org.osgi.service.component.annotations.*; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Selects the best available {@link LogInterceptor} implementation. | ||
| * | ||
| * <p><strong>Why not OSGi Log Service 1.4?</strong> Its {@code LogReaderService} only captures | ||
| * logs sent directly to OSGi Log Service. SLF4J/Logback logs are bridged one-way (SLF4J → OSGi), | ||
| * so {@code LogReaderService} doesn't receive them. We use Sling AppenderTracker instead.</p> | ||
| * | ||
| * @see SlingLogInterceptor | ||
| */ | ||
| @Component(service = LogInterceptorManager.class) | ||
| public class LogInterceptorManager { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(LogInterceptorManager.class); | ||
|
|
||
| private final List<LogInterceptor> interceptors = new CopyOnWriteArrayList<>(); | ||
|
|
||
| @Reference( | ||
| service = LogInterceptor.class, | ||
| cardinality = ReferenceCardinality.MULTIPLE, | ||
| policy = ReferencePolicy.DYNAMIC) | ||
| protected void bindInterceptor(LogInterceptor interceptor) { | ||
| interceptors.add(interceptor); | ||
| } | ||
|
|
||
| protected void unbindInterceptor(LogInterceptor interceptor) { | ||
| interceptors.remove(interceptor); | ||
| } | ||
|
|
||
| public LogInterceptor.Handle attach(Consumer<LogMessage> listener, String... loggerNames) { | ||
| return interceptors.stream() | ||
| .filter(LogInterceptor::isAvailable) | ||
| .findFirst() | ||
| .map(i -> { | ||
| LOG.debug("Using log interceptor: {}", i.getClass().getSimpleName()); | ||
| return i.attach(listener, loggerNames); | ||
| }) | ||
| .orElseGet(() -> { | ||
| LOG.warn("No log interceptor available"); | ||
| return () -> {}; | ||
| }); | ||
| } | ||
|
|
||
| public boolean isAvailable() { | ||
| return interceptors.stream().anyMatch(LogInterceptor::isAvailable); | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
core/src/main/java/dev/vml/es/acm/core/code/log/LogMessage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package dev.vml.es.acm.core.code.log; | ||
|
|
||
| public class LogMessage { | ||
|
|
||
| private final String loggerName; | ||
|
|
||
| private final String level; | ||
|
|
||
| private final String message; | ||
|
|
||
| private final long timestamp; | ||
|
|
||
| public LogMessage(String loggerName, String level, String message, long timestamp) { | ||
| this.loggerName = loggerName; | ||
| this.level = level; | ||
| this.message = message; | ||
| this.timestamp = timestamp; | ||
| } | ||
|
|
||
| public String getLoggerName() { | ||
| return loggerName; | ||
| } | ||
|
|
||
| public String getLevel() { | ||
| return level; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return message; | ||
| } | ||
|
|
||
| public long getTimestamp() { | ||
| return timestamp; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.