diff --git a/pom.xml b/pom.xml index e0a7f6b..175d4be 100644 --- a/pom.xml +++ b/pom.xml @@ -71,6 +71,13 @@ import pom + + org.apache.logging.log4j + log4j-bom + ${log4j2.version} + import + pom + @@ -132,6 +139,12 @@ guava + + org.apache.logging.log4j + log4j-slf4j2-impl + compile + + org.junit.jupiter junit-jupiter-api diff --git a/src/main/java/com/guicedee/guicedinjection/GuiceContext.java b/src/main/java/com/guicedee/guicedinjection/GuiceContext.java index 431e061..c6a639b 100644 --- a/src/main/java/com/guicedee/guicedinjection/GuiceContext.java +++ b/src/main/java/com/guicedee/guicedinjection/GuiceContext.java @@ -40,6 +40,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import java.util.logging.Level; +import java.util.logging.Logger; import java.util.regex.Pattern; import static com.guicedee.guicedinjection.properties.GlobalProperties.getSystemPropertyOrEnvironment; @@ -53,10 +54,10 @@ * @version 1.0 * @since Nov 14, 2016 */ -@Log @SuppressWarnings("MissingClassJavaDoc") public class GuiceContext> implements IGuiceContext { + private static Logger log; /** * This particular instance of the class */ @@ -102,8 +103,10 @@ private GuiceContext() { try { - // String cn = "org.apache.logging.log4j.jul.LogManager"; - // System.setProperty("java.util.logging.manager", cn); + System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager"); + System.setProperty("vertx.logger-delegate-factory-class-name", "io.vertx.core.logging.Log4j2LogDelegateFactory"); + + log = Logger.getLogger("GuiceContext"); ConfigurationBuilder builder = ConfigurationBuilderFactory.newConfigurationBuilder(); @@ -114,13 +117,27 @@ private GuiceContext() // create the console appender AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE") .addAttribute("target", - ConsoleAppender.Target.SYSTEM_ERR) + ConsoleAppender.Target.SYSTEM_OUT) //.addAttribute("additivity", "true") ; appenderBuilder.add(builder.newLayout("PatternLayout"). addAttribute("pattern", "%d{ABSOLUTE} %-5level: %msg%n")); builder.add(appenderBuilder); + System.setProperty("hazelcast.logging.type", "log4j2"); + System.setProperty("log4j.level", "DEBUG"); + + Configurator.setLevel("org.hibernate", org.apache.logging.log4j.Level.ERROR); + Configurator.setLevel("com.hazelcast", org.apache.logging.log4j.Level.INFO); + Configurator.setLevel("com.hazelcast.cache.impl ", org.apache.logging.log4j.Level.DEBUG); + Configurator.setLevel("com.hazelcast.system.logo", org.apache.logging.log4j.Level.ERROR); + Configurator.setLevel("com.hazelcast.internal.server.tcp.TcpServerConnection", org.apache.logging.log4j.Level.ERROR); + Configurator.setLevel("io.netty", org.apache.logging.log4j.Level.ERROR); + Configurator.setLevel("com.mchange", org.apache.logging.log4j.Level.ERROR); + Configurator.setLevel("com.zandero", org.apache.logging.log4j.Level.ERROR); + Configurator.setLevel("com.google", org.apache.logging.log4j.Level.ERROR); + Configurator.setLevel("jdk.event.security", org.apache.logging.log4j.Level.ERROR); + Configurator.setLevel("org.apache.commons.beanutils", org.apache.logging.log4j.Level.ERROR); RootLoggerComponentBuilder rootLogger = builder.newRootLogger(org.apache.logging.log4j.Level.DEBUG); ServiceLoader log4JConfigurators = ServiceLoader.load(Log4JConfigurator.class); @@ -132,7 +149,7 @@ private GuiceContext() rootLogger.add(builder.newAppenderRef("Stdout")); builder.add(rootLogger); - builder.writeXmlConfiguration(System.out); + //builder.writeXmlConfiguration(System.out); Configurator.initialize(builder.build()); } catch (Throwable T) diff --git a/src/main/java/com/guicedee/guicedinjection/InjectLogger.java b/src/main/java/com/guicedee/guicedinjection/InjectLogger.java new file mode 100644 index 0000000..cd274ed --- /dev/null +++ b/src/main/java/com/guicedee/guicedinjection/InjectLogger.java @@ -0,0 +1,22 @@ +package com.guicedee.guicedinjection; + + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Target({METHOD, CONSTRUCTOR, FIELD}) +@Retention(RUNTIME) +@Documented +public @interface InjectLogger +{ + String value(); + String level() default ""; + + String rollingFileName() default ""; + String fileName() default ""; + +} diff --git a/src/main/java/com/guicedee/guicedinjection/injections/ContextBinderGuice.java b/src/main/java/com/guicedee/guicedinjection/injections/ContextBinderGuice.java index c628429..c5fb40d 100644 --- a/src/main/java/com/guicedee/guicedinjection/injections/ContextBinderGuice.java +++ b/src/main/java/com/guicedee/guicedinjection/injections/ContextBinderGuice.java @@ -2,11 +2,13 @@ import com.google.inject.AbstractModule; import com.google.inject.Singleton; +import com.google.inject.matcher.Matchers; import com.guicedee.guicedinjection.GuiceConfig; import com.guicedee.guicedinjection.GuiceContext; import com.guicedee.guicedinjection.JobService; import com.guicedee.guicedinjection.interfaces.*; +import com.guicedee.guicedinjection.logging.Log4JTypeListener; import com.guicedee.guicedinjection.properties.GlobalProperties; import io.github.classgraph.ScanResult; import lombok.extern.java.Log; @@ -47,5 +49,7 @@ public void configure() { .toInstance(JobService.INSTANCE); bind(JobService.class) .toInstance(JobService.INSTANCE); + + bindListener(Matchers.any(), new Log4JTypeListener()); } } diff --git a/src/main/java/com/guicedee/guicedinjection/logging/Log4JMembersInjector.java b/src/main/java/com/guicedee/guicedinjection/logging/Log4JMembersInjector.java new file mode 100644 index 0000000..28b99a4 --- /dev/null +++ b/src/main/java/com/guicedee/guicedinjection/logging/Log4JMembersInjector.java @@ -0,0 +1,32 @@ +package com.guicedee.guicedinjection.logging; + +import com.google.common.base.Strings; +import com.google.inject.MembersInjector; +import com.guicedee.guicedinjection.InjectLogger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.lang.reflect.Field; + +public class Log4JMembersInjector implements MembersInjector { + private final Field field; + private final InjectLogger injectLogger; + private final Logger logger; + + Log4JMembersInjector(Field field) { + this.field = field; + this.injectLogger = field.getAnnotation(InjectLogger.class); + String logName = Strings.isNullOrEmpty(injectLogger.value()) ? field.getDeclaringClass().getCanonicalName() : injectLogger.value(); + this.logger = LogManager.getLogger(logName); + field.setAccessible(true); + } + + public void injectMembers(T t) { + try { + field.set(t, logger); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/src/main/java/com/guicedee/guicedinjection/logging/Log4JTypeListener.java b/src/main/java/com/guicedee/guicedinjection/logging/Log4JTypeListener.java new file mode 100644 index 0000000..c60f649 --- /dev/null +++ b/src/main/java/com/guicedee/guicedinjection/logging/Log4JTypeListener.java @@ -0,0 +1,24 @@ +package com.guicedee.guicedinjection.logging; + +import com.google.inject.TypeLiteral; +import com.google.inject.spi.TypeEncounter; +import com.google.inject.spi.TypeListener; +import com.guicedee.guicedinjection.InjectLogger; +import org.apache.logging.log4j.Logger; + +import java.lang.reflect.Field; + +public class Log4JTypeListener implements TypeListener { + public void hear(TypeLiteral typeLiteral, TypeEncounter typeEncounter) { + Class clazz = typeLiteral.getRawType(); + while (clazz != null) { + for (Field field : clazz.getDeclaredFields()) { + if (field.getType() == Logger.class && + field.isAnnotationPresent(InjectLogger.class)) { + typeEncounter.register(new Log4JMembersInjector(field)); + } + } + clazz = clazz.getSuperclass(); + } + } +}