forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#27917 from geoand/quarkusio#27864-followup
Enable the registration of @LoggingFilter classes via CDI
- Loading branch information
Showing
10 changed files
with
140 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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
57 changes: 57 additions & 0 deletions
57
core/runtime/src/main/java/io/quarkus/runtime/logging/LogFilterFactory.java
This file contains 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,57 @@ | ||
package io.quarkus.runtime.logging; | ||
|
||
import java.util.ServiceLoader; | ||
import java.util.logging.Filter; | ||
|
||
/** | ||
* Factory that allows for the creation of {@link Filter} classes annotated with {@link io.quarkus.logging.LoggingFilter}. | ||
* Implementations of this class are loaded via the {@link ServiceLoader} and the implementation selected is the one | ||
* with the lowest value returned from the {@code priority} method. | ||
*/ | ||
public interface LogFilterFactory { | ||
|
||
int MIN_PRIORITY = Integer.MAX_VALUE; | ||
int DEFAULT_PRIORITY = 0; | ||
|
||
Filter create(String className) throws Exception; | ||
|
||
default int priority() { | ||
return DEFAULT_PRIORITY; | ||
} | ||
|
||
static LogFilterFactory load() { | ||
LogFilterFactory result = null; | ||
ServiceLoader<LogFilterFactory> load = ServiceLoader.load(LogFilterFactory.class); | ||
for (LogFilterFactory next : load) { | ||
if (result == null) { | ||
result = next; | ||
} else { | ||
if (next.priority() < result.priority()) { | ||
result = next; | ||
} | ||
} | ||
} | ||
if (result == null) { | ||
result = new ReflectionLogFilterFactory(); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* The default implementation used when no other implementation is found. | ||
* This simply calls the class' no-arg constructor (and fails if one does not exist). | ||
*/ | ||
class ReflectionLogFilterFactory implements LogFilterFactory { | ||
|
||
@Override | ||
public Filter create(String className) throws Exception { | ||
return (Filter) Class.forName(className, true, Thread.currentThread().getContextClassLoader()) | ||
.getConstructor().newInstance(); | ||
} | ||
|
||
@Override | ||
public int priority() { | ||
return LogFilterFactory.MIN_PRIORITY; | ||
} | ||
} | ||
} |
This file contains 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 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
16 changes: 16 additions & 0 deletions
16
...s/arc/deployment/src/main/java/io/quarkus/arc/deployment/LoggingBeanSupportProcessor.java
This file contains 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,16 @@ | ||
package io.quarkus.arc.deployment; | ||
|
||
import io.quarkus.arc.processor.BuiltinScope; | ||
import io.quarkus.deployment.annotations.BuildProducer; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
import io.quarkus.deployment.logging.LoggingResourceProcessor; | ||
|
||
public class LoggingBeanSupportProcessor { | ||
|
||
@BuildStep | ||
public void discoveredComponents(BuildProducer<BeanDefiningAnnotationBuildItem> beanDefiningAnnotationProducer) { | ||
beanDefiningAnnotationProducer.produce(new BeanDefiningAnnotationBuildItem( | ||
LoggingResourceProcessor.LOGGING_FILTER, BuiltinScope.SINGLETON.getName(), false)); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/logging/ArcLogFilterFactory.java
This file contains 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,29 @@ | ||
package io.quarkus.arc.runtime.logging; | ||
|
||
import java.util.logging.Filter; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.InstanceHandle; | ||
import io.quarkus.runtime.logging.LogFilterFactory; | ||
|
||
/** | ||
* Creates the implementation of the class by getting a bean from Arc. | ||
* This class is loaded automatically by the {@link java.util.ServiceLoader}. | ||
*/ | ||
public class ArcLogFilterFactory implements LogFilterFactory { | ||
|
||
@Override | ||
public Filter create(String className) throws Exception { | ||
InstanceHandle<?> instance = Arc.container().instance(Class.forName(className, true, Thread.currentThread() | ||
.getContextClassLoader())); | ||
if (!instance.isAvailable()) { | ||
throw new IllegalStateException("Improper integration of '" + LogFilterFactory.class.getName() + "' detected"); | ||
} | ||
return (Filter) instance.get(); | ||
} | ||
|
||
@Override | ||
public int priority() { | ||
return LogFilterFactory.DEFAULT_PRIORITY - 100; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
.../runtime/src/main/resources/META-INF/services/io.quarkus.runtime.logging.LogFilterFactory
This file contains 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 @@ | ||
io.quarkus.arc.runtime.logging.ArcLogFilterFactory |
This file contains 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 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