|
|
249b5c |
Description: Provide built-in injection of SLF4J loggers
|
|
|
249b5c |
Author: Stuart McCulloch <mcculls@gmail.com>
|
|
|
249b5c |
Bug-Google: http://code.google.com/p/google-guice/issues/detail?id=492
|
|
|
249b5c |
Last-Update: 2014-11-07
|
|
|
249b5c |
|
|
|
249b5c |
diff --git a/common.xml b/common.xml
|
|
|
249b5c |
index a23eb90..2d3d9b3 100644
|
|
|
249b5c |
--- a/common.xml
|
|
|
249b5c |
+++ b/common.xml
|
|
|
249b5c |
@@ -44,6 +44,10 @@
|
|
|
249b5c |
|
|
|
249b5c |
<property name="Export-Package" value="!${module}.internal.*,${module}.*;version=${api.version}"/>
|
|
|
249b5c |
|
|
|
249b5c |
+ <condition property="DynamicImport-Package" value="org.slf4j">
|
|
|
249b5c |
+ <equals arg1="${module}" arg2="com.google.inject"/>
|
|
|
249b5c |
+ </condition>
|
|
|
249b5c |
+
|
|
|
249b5c |
<condition property="Eclipse-ExtensibleAPI" value="true">
|
|
|
249b5c |
<equals arg1="${module}" arg2="com.google.inject"/>
|
|
|
249b5c |
</condition>
|
|
|
249b5c |
diff --git a/core/pom.xml b/core/pom.xml
|
|
|
249b5c |
index 33e6dcc..edfa724 100644
|
|
|
249b5c |
--- a/core/pom.xml
|
|
|
249b5c |
+++ b/core/pom.xml
|
|
|
249b5c |
@@ -15,6 +15,12 @@
|
|
|
249b5c |
|
|
|
249b5c |
<dependencies>
|
|
|
249b5c |
<dependency>
|
|
|
249b5c |
+ <groupId>org.slf4j</groupId>
|
|
|
249b5c |
+ <artifactId>slf4j-api</artifactId>
|
|
|
249b5c |
+ <version>1.6.4</version>
|
|
|
249b5c |
+ <optional>true</optional>
|
|
|
249b5c |
+ </dependency>
|
|
|
249b5c |
+ <dependency>
|
|
|
249b5c |
<groupId>javax.inject</groupId>
|
|
|
249b5c |
<artifactId>javax.inject</artifactId>
|
|
|
249b5c |
</dependency>
|
|
|
249b5c |
@@ -91,6 +97,12 @@
|
|
|
249b5c |
<artifactId>maven-surefire-plugin</artifactId>
|
|
|
249b5c |
<configuration>
|
|
|
249b5c |
|
|
|
249b5c |
+ | Run core tests without SLF4J on the classpath
|
|
|
249b5c |
+ -->
|
|
|
249b5c |
+ <classpathDependencyExcludes>
|
|
|
249b5c |
+ <exclude>org.slf4j:slf4j-api</exclude>
|
|
|
249b5c |
+ </classpathDependencyExcludes>
|
|
|
249b5c |
+
|
|
|
249b5c |
| Temporarily excluded tests
|
|
|
249b5c |
-->
|
|
|
249b5c |
<excludes>
|
|
|
249b5c |
@@ -113,6 +125,7 @@
|
|
|
249b5c |
<Bundle-Name>${project.artifactId}$(if;$(classes;NAMED;*.MethodAspect);; (no_aop))</Bundle-Name>
|
|
|
249b5c |
<Import-Package>!net.sf.cglib.*,!org.objectweb.asm.*,!com.google.inject.*,*</Import-Package>
|
|
|
249b5c |
<Eclipse-ExtensibleAPI>true</Eclipse-ExtensibleAPI>
|
|
|
249b5c |
+ <DynamicImport-Package>org.slf4j</DynamicImport-Package>
|
|
|
249b5c |
</instructions>
|
|
|
249b5c |
</configuration>
|
|
|
249b5c |
</plugin>
|
|
|
249b5c |
diff --git a/core/src/com/google/inject/internal/InjectorShell.java b/core/src/com/google/inject/internal/InjectorShell.java
|
|
|
249b5c |
index 6100879..56b13b3 100644
|
|
|
249b5c |
--- a/core/src/com/google/inject/internal/InjectorShell.java
|
|
|
249b5c |
+++ b/core/src/com/google/inject/internal/InjectorShell.java
|
|
|
249b5c |
@@ -251,6 +251,15 @@ final class InjectorShell {
|
|
|
249b5c |
new ProviderInstanceBindingImpl<Logger>(injector, key,
|
|
|
249b5c |
SourceProvider.UNKNOWN_SOURCE, loggerFactory, Scoping.UNSCOPED,
|
|
|
249b5c |
loggerFactory, ImmutableSet.<InjectionPoint>of()));
|
|
|
249b5c |
+
|
|
|
249b5c |
+ try {
|
|
|
249b5c |
+ Key<org.slf4j.Logger> slf4jKey = Key.get(org.slf4j.Logger.class);
|
|
|
249b5c |
+ SLF4JLoggerFactory slf4jLoggerFactory = new SLF4JLoggerFactory(injector);
|
|
|
249b5c |
+ injector.state.putBinding(slf4jKey,
|
|
|
249b5c |
+ new ProviderInstanceBindingImpl<org.slf4j.Logger>(injector, slf4jKey,
|
|
|
249b5c |
+ SourceProvider.UNKNOWN_SOURCE, slf4jLoggerFactory, Scoping.UNSCOPED,
|
|
|
249b5c |
+ slf4jLoggerFactory, ImmutableSet.<InjectionPoint>of()));
|
|
|
249b5c |
+ } catch (Throwable e) {}
|
|
|
249b5c |
}
|
|
|
249b5c |
|
|
|
249b5c |
private static class LoggerFactory implements InternalFactory<Logger>, Provider<Logger> {
|
|
|
249b5c |
@@ -270,6 +279,44 @@ final class InjectorShell {
|
|
|
249b5c |
}
|
|
|
249b5c |
}
|
|
|
249b5c |
|
|
|
249b5c |
+ private static class SLF4JLoggerFactory implements InternalFactory<org.slf4j.Logger>, Provider<org.slf4j.Logger> {
|
|
|
249b5c |
+ private final Injector injector;
|
|
|
249b5c |
+
|
|
|
249b5c |
+ private org.slf4j.ILoggerFactory loggerFactory;
|
|
|
249b5c |
+
|
|
|
249b5c |
+ SLF4JLoggerFactory(Injector injector) {
|
|
|
249b5c |
+ this.injector = injector;
|
|
|
249b5c |
+ }
|
|
|
249b5c |
+
|
|
|
249b5c |
+ org.slf4j.ILoggerFactory loggerFactory() {
|
|
|
249b5c |
+ if (loggerFactory == null) {
|
|
|
249b5c |
+ try {
|
|
|
249b5c |
+ loggerFactory = injector.getInstance(org.slf4j.ILoggerFactory.class);
|
|
|
249b5c |
+ } catch (Throwable e) {}
|
|
|
249b5c |
+ if (loggerFactory == null) {
|
|
|
249b5c |
+ loggerFactory = org.slf4j.LoggerFactory.getILoggerFactory();
|
|
|
249b5c |
+ }
|
|
|
249b5c |
+ }
|
|
|
249b5c |
+ return loggerFactory;
|
|
|
249b5c |
+ }
|
|
|
249b5c |
+
|
|
|
249b5c |
+ public org.slf4j.Logger get(Errors errors, InternalContext context, Dependency dependency, boolean linked) {
|
|
|
249b5c |
+ InjectionPoint injectionPoint = dependency.getInjectionPoint();
|
|
|
249b5c |
+ if (injectionPoint != null) {
|
|
|
249b5c |
+ return loggerFactory().getLogger(injectionPoint.getMember().getDeclaringClass().getName());
|
|
|
249b5c |
+ }
|
|
|
249b5c |
+ return loggerFactory().getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
|
|
|
249b5c |
+ }
|
|
|
249b5c |
+
|
|
|
249b5c |
+ public org.slf4j.Logger get() {
|
|
|
249b5c |
+ return loggerFactory().getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
|
|
|
249b5c |
+ }
|
|
|
249b5c |
+
|
|
|
249b5c |
+ public String toString() {
|
|
|
249b5c |
+ return "Provider<org.slf4j.Logger>";
|
|
|
249b5c |
+ }
|
|
|
249b5c |
+ }
|
|
|
249b5c |
+
|
|
|
249b5c |
private static void bindStage(InjectorImpl injector, Stage stage) {
|
|
|
249b5c |
Key<Stage> key = Key.get(Stage.class);
|
|
|
249b5c |
InstanceBindingImpl<Stage> stageBinding = new InstanceBindingImpl<Stage>(
|
|
|
249b5c |
diff --git a/extensions/persist/pom.xml b/extensions/persist/pom.xml
|
|
|
249b5c |
index a560f38..e909927 100644
|
|
|
249b5c |
--- a/extensions/persist/pom.xml
|
|
|
249b5c |
+++ b/extensions/persist/pom.xml
|
|
|
249b5c |
@@ -29,7 +29,7 @@
|
|
|
249b5c |
<dependency>
|
|
|
249b5c |
<groupId>org.slf4j</groupId>
|
|
|
249b5c |
<artifactId>slf4j-simple</artifactId>
|
|
|
249b5c |
- <version>1.6.1</version>
|
|
|
249b5c |
+ <version>1.6.4</version>
|
|
|
249b5c |
<scope>test</scope>
|
|
|
249b5c |
</dependency>
|
|
|
249b5c |
<dependency>
|
|
|
249b5c |
diff --git a/lib/build/slf4j-api-1.6.4.jar b/lib/build/slf4j-api-1.6.4.jar
|
|
|
249b5c |
new file mode 100644
|
|
|
249b5c |
index 0000000..76ef305
|
|
|
249b5c |
Binary files /dev/null and b/lib/build/slf4j-api-1.6.4.jar differ
|
|
|
249b5c |
diff --git a/pom.xml b/pom.xml
|
|
|
249b5c |
index 37305d0..5834d49 100644
|
|
|
249b5c |
--- a/pom.xml
|
|
|
249b5c |
+++ b/pom.xml
|
|
|
249b5c |
@@ -283,7 +283,7 @@ See the Apache License Version 2.0 for the specific language governing permissio
|
|
|
249b5c |
</plugin>
|
|
|
249b5c |
<plugin>
|
|
|
249b5c |
<artifactId>maven-surefire-plugin</artifactId>
|
|
|
249b5c |
- <version>2.5</version>
|
|
|
249b5c |
+ <version>2.6</version>
|
|
|
249b5c |
<configuration>
|
|
|
249b5c |
<redirectTestOutputToFile>true</redirectTestOutputToFile>
|
|
|
249b5c |
|