Skip to content

Commit

Permalink
#156 - Multi-release jar with logging via System.Logger and JUL
Browse files Browse the repository at this point in the history
This isn't fully satisfactory yet.
  • Loading branch information
rbygrave committed Dec 14, 2021
1 parent 625c75f commit 313bc83
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 26 deletions.
7 changes: 7 additions & 0 deletions blackbox-test-inject/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk-platform-logging</artifactId>
<version>2.0.0-alpha5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.avaje.composite</groupId>
<artifactId>logback</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,38 @@

import io.avaje.inject.BeanScope;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;

import static org.junit.jupiter.api.Assertions.*;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import static org.junit.jupiter.api.Assertions.assertEquals;

class HelloServiceTest {

static {
LogManager.getLogManager().reset();
SLF4JBridgeHandler.install();
}

void asd() {
org.slf4j.Logger slf4j = LoggerFactory.getLogger("io.avaje.doo2");
slf4j.trace("trace doo2");
}
/**
* No mocking, no use of <code>@TestScope</code> so just like main.
*/
@Test
void basic() {

asd();
Logger utilLogger = Logger.getLogger("io.avaje.doo1");
utilLogger.log(Level.FINE, "fine doo");
utilLogger.log(Level.FINER, "finer doo");
utilLogger.log(Level.INFO, "info doo");

// just wire everything with no test scope, mocks etc
BeanScope beanScope = BeanScope.newBuilder().build();

Expand Down
14 changes: 14 additions & 0 deletions blackbox-test-inject/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<configuration scan="true" scanPeriod="10 seconds">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="DEBUG">
<appender-ref ref="STDOUT"/>
</root>

<logger name="io.avaje" level="trace"/>

</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Iterator;
Expand All @@ -23,7 +21,6 @@
*/
public class InjectExtension implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback, ExtensionContext.Store.CloseableResource {

private static final Logger log = LoggerFactory.getLogger(InjectExtension.class);
private static final Namespace INJECT_NS = Namespace.create("io.avaje.inject.InjectTest");
private static final String BEAN_SCOPE = "BEAN_SCOPE";
private static final ReentrantLock lock = new ReentrantLock();
Expand All @@ -48,7 +45,7 @@ public void close() throws Throwable {
lock.lock();
try {
if (globalTestScope != null) {
log.debug("Closing global test BeanScope");
Log.debug("Closing global test BeanScope");
globalTestScope.close();
}
} finally {
Expand All @@ -59,12 +56,11 @@ public void close() throws Throwable {
private void initialiseGlobalTestScope(ExtensionContext context) {
Iterator<TestModule> iterator = ServiceLoader.load(TestModule.class).iterator();
if (iterator.hasNext()) {
log.debug("Building global test BeanScope (as parent scope for tests)");
Log.debug("Building global test BeanScope (as parent scope for tests)");
globalTestScope = BeanScope.newBuilder()
.withModules(iterator.next())
.build();

log.trace("register global test BeanScope with beans {}", globalTestScope);
context.getRoot().getStore(Namespace.GLOBAL).put(InjectExtension.class.getCanonicalName(), this);
}
}
Expand All @@ -90,7 +86,6 @@ public void beforeEach(final ExtensionContext context) {
for (MetaReader reader : readers) {
reader.setFromScope(beanScope);
}
log.trace("test setup with {}", readers);
context.getStore(INJECT_NS).put(BEAN_SCOPE, beanScope);
}

Expand Down
13 changes: 13 additions & 0 deletions inject-test/src/main/java/io/avaje/inject/test/Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.avaje.inject.test;

import java.util.logging.Logger;

class Log {

private static final Logger log = Logger.getLogger("io.avaje.inject");

static void debug(String message) {
log.fine(message);
}

}
51 changes: 45 additions & 6 deletions inject/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
<version>2.0.1</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-lang</artifactId>
Expand Down Expand Up @@ -77,6 +71,51 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile-java-8</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</execution>
<execution>
<id>compile-java-11</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>11</release>
<compileSourceRoots>
<compileSourceRoot>${project.basedir}/src/main/java11</compileSourceRoot>
</compileSourceRoots>
<multiReleaseOutput>true</multiReleaseOutput>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</archive>
</configuration>
</plugin>

</plugins>
</build>

Expand Down
5 changes: 0 additions & 5 deletions inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import io.avaje.inject.spi.Module;
import io.avaje.inject.spi.SuppliedBean;
import io.avaje.lang.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Type;
import java.util.*;
Expand All @@ -17,8 +15,6 @@
*/
class DBeanScopeBuilder implements BeanScopeBuilder.ForTesting {

private static final Logger log = LoggerFactory.getLogger("io.avaje.inject");

@SuppressWarnings("rawtypes")
private final List<SuppliedBean> suppliedBeans = new ArrayList<>();

Expand Down Expand Up @@ -150,7 +146,6 @@ public BeanScope build() {
" Review IntelliJ Settings / Build / Build tools / Gradle - 'Build and run using' value and set that to 'Gradle'. " +
" Refer to https://avaje.io/inject#gradle");
}
log.debug("building with modules {}", moduleNames);
Builder builder = Builder.newBuilder(suppliedBeans, enrichBeans, parent, parentOverride);
for (Module factory : factoryOrder.factories()) {
factory.build(builder);
Expand Down
10 changes: 3 additions & 7 deletions inject/src/main/java/io/avaje/inject/spi/DBeanScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import io.avaje.inject.Priority;
import io.avaje.lang.NonNullApi;
import io.avaje.lang.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
Expand All @@ -16,8 +14,6 @@
@NonNullApi
class DBeanScope implements BeanScope {

private static final Logger log = LoggerFactory.getLogger("io.avaje.inject");

private final ReentrantLock lock = new ReentrantLock();
private final List<Runnable> postConstruct;
private final List<AutoCloseable> preDestroy;
Expand Down Expand Up @@ -152,7 +148,7 @@ public List<Object> listByAnnotation(Class<?> annotation) {
DBeanScope start() {
lock.lock();
try {
log.trace("firing postConstruct");
Log.trace("firing postConstruct");
for (Runnable invoke : postConstruct) {
invoke.run();
}
Expand All @@ -172,12 +168,12 @@ public void close() {
if (!closed) {
// we only allow one call to preDestroy
closed = true;
log.trace("firing preDestroy");
Log.trace("firing preDestroy");
for (AutoCloseable closeable : preDestroy) {
try {
closeable.close();
} catch (Exception e) {
log.error("Error during PreDestroy lifecycle method", e);
Log.error("Error during PreDestroy lifecycle method", e);
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions inject/src/main/java/io/avaje/inject/spi/Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.avaje.inject.spi;

import java.util.logging.Level;
import java.util.logging.Logger;

class Log {
private static final Logger log = Logger.getLogger("io.avaje.inject");

static void trace(String message) {
log.log(Level.FINER, message);
}

static void error(String message, Exception e) {
log.log(Level.SEVERE, message, e);
}
}
14 changes: 14 additions & 0 deletions inject/src/main/java11/io/avaje/inject/spi/Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.avaje.inject.spi;

class Log {

private static final System.Logger log = System.getLogger("io.avaje.inject");

static void trace(String message) {
log.log(System.Logger.Level.TRACE, message);
}

static void error(String message, Exception e) {
log.log(System.Logger.Level.ERROR, message, e);
}
}

0 comments on commit 313bc83

Please sign in to comment.