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