-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- resolves #13510
- Loading branch information
Showing
5 changed files
with
211 additions
and
0 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
82 changes: 82 additions & 0 deletions
82
extensions/arc/deployment/src/test/java/io/quarkus/arc/test/log/InjectedLoggerTest.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,82 @@ | ||
package io.quarkus.arc.test.log; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.context.Dependent; | ||
import javax.inject.Inject; | ||
|
||
import org.jboss.logging.Logger; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.log.LoggerName; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class InjectedLoggerTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(SimpleBean.class)); | ||
|
||
@Inject | ||
SimpleBean simpleBean; | ||
|
||
@Inject | ||
AnotherSimpleBean anotherSimpleBean; | ||
|
||
@Test | ||
public void testInjectedLogger() { | ||
assertEquals(SimpleBean.class.getName(), simpleBean.getLog().getName()); | ||
assertEquals("shared", simpleBean.getSharedLog().getName()); | ||
assertEquals(AnotherSimpleBean.class.getName(), anotherSimpleBean.getLog().getName()); | ||
assertEquals(simpleBean.getSharedLog(), anotherSimpleBean.getSharedLog()); | ||
} | ||
|
||
@ApplicationScoped | ||
static class SimpleBean { | ||
|
||
@Inject | ||
Logger log; | ||
|
||
@LoggerName("shared") | ||
Logger sharedLog; | ||
|
||
public Logger getLog() { | ||
log.info("Someone is here!"); | ||
return log; | ||
} | ||
|
||
public Logger getSharedLog() { | ||
return sharedLog; | ||
} | ||
|
||
} | ||
|
||
@Dependent | ||
static class AnotherSimpleBean { | ||
|
||
private final Logger log; | ||
|
||
@LoggerName("shared") | ||
Logger sharedLog; | ||
|
||
public AnotherSimpleBean(Logger log) { | ||
this.log = log; | ||
} | ||
|
||
public Logger getLog() { | ||
return log; | ||
} | ||
|
||
public Logger getSharedLog() { | ||
sharedLog.info("Yet another someone is here!"); | ||
return sharedLog; | ||
} | ||
|
||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
extensions/arc/runtime/src/main/java/io/quarkus/arc/log/LoggerName.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,48 @@ | ||
package io.quarkus.arc.log; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import javax.enterprise.util.AnnotationLiteral; | ||
import javax.enterprise.util.Nonbinding; | ||
import javax.inject.Qualifier; | ||
|
||
@Qualifier | ||
@Retention(RUNTIME) | ||
@Target({ FIELD, PARAMETER, METHOD }) | ||
public @interface LoggerName { | ||
|
||
/** | ||
* The logger name must not be empty. | ||
* | ||
* @return the logger name | ||
*/ | ||
@Nonbinding | ||
String value(); | ||
|
||
/** | ||
* Supports inline instantiation of this qualifier. | ||
*/ | ||
public static final class Literal extends AnnotationLiteral<LoggerName> implements LoggerName { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private final String value; | ||
|
||
public Literal(String value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public String value() { | ||
return value; | ||
} | ||
|
||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/LoggerProducer.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,49 @@ | ||
package io.quarkus.arc.runtime; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
import javax.annotation.PreDestroy; | ||
import javax.enterprise.context.Dependent; | ||
import javax.enterprise.inject.Produces; | ||
import javax.enterprise.inject.spi.InjectionPoint; | ||
import javax.inject.Singleton; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
import io.quarkus.arc.log.LoggerName; | ||
|
||
@Singleton | ||
public class LoggerProducer { | ||
|
||
private final ConcurrentMap<String, Logger> loggers = new ConcurrentHashMap<>(); | ||
|
||
@Dependent | ||
@Produces | ||
Logger getSimpleLogger(InjectionPoint injectionPoint) { | ||
return loggers.computeIfAbsent(injectionPoint.getMember().getDeclaringClass().getName(), Logger::getLogger); | ||
} | ||
|
||
@LoggerName("") | ||
@Dependent | ||
@Produces | ||
Logger getLoggerWithCustomName(InjectionPoint injectionPoint) { | ||
String name = null; | ||
for (Annotation qualifier : injectionPoint.getQualifiers()) { | ||
if (qualifier.annotationType().equals(LoggerName.class)) { | ||
name = ((LoggerName) qualifier).value(); | ||
} | ||
} | ||
if (name == null || name.isEmpty()) { | ||
throw new IllegalStateException("Unable to derive the logger name at " + injectionPoint); | ||
} | ||
return loggers.computeIfAbsent(name, Logger::getLogger); | ||
} | ||
|
||
@PreDestroy | ||
void destroy() { | ||
loggers.clear(); | ||
} | ||
|
||
} |