Skip to content

Commit

Permalink
Guiced Rabbit Configure Connections and Exchanges by Package
Browse files Browse the repository at this point in the history
  • Loading branch information
GedMarc committed Oct 28, 2024
1 parent 731025c commit 1903495
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 5 deletions.
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>${log4j2.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -132,6 +139,12 @@
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
27 changes: 22 additions & 5 deletions src/main/java/com/guicedee/guicedinjection/GuiceContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -53,10 +54,10 @@
* @version 1.0
* @since Nov 14, 2016
*/
@Log
@SuppressWarnings("MissingClassJavaDoc")
public class GuiceContext<J extends GuiceContext<J>> implements IGuiceContext
{
private static Logger log;
/**
* This particular instance of the class
*/
Expand Down Expand Up @@ -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<BuiltConfiguration> builder =
ConfigurationBuilderFactory.newConfigurationBuilder();
Expand All @@ -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<Log4JConfigurator> log4JConfigurators = ServiceLoader.load(Log4JConfigurator.class);
Expand All @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/guicedee/guicedinjection/InjectLogger.java
Original file line number Diff line number Diff line change
@@ -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 "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,5 +49,7 @@ public void configure() {
.toInstance(JobService.INSTANCE);
bind(JobService.class)
.toInstance(JobService.INSTANCE);

bindListener(Matchers.any(), new Log4JTypeListener());
}
}
Original file line number Diff line number Diff line change
@@ -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<T> implements MembersInjector<T> {
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);
}
}

}
Original file line number Diff line number Diff line change
@@ -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 <T> void hear(TypeLiteral<T> typeLiteral, TypeEncounter<T> 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<T>(field));
}
}
clazz = clazz.getSuperclass();
}
}
}

0 comments on commit 1903495

Please sign in to comment.