Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
Add enabled property to WildFlyGelfLogHandler #170
Browse files Browse the repository at this point in the history
Honor now "enabled" attribute of <custom-handler>.

Original pull request: #171.
  • Loading branch information
Christoph Linder authored and mp911de committed Dec 14, 2018
1 parent c6ebdd3 commit 92251de
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,35 @@
*/
public class WildFlyGelfLogHandler extends GelfLogHandler {
private static final ErrorManager DEFAULT_ERROR_MANAGER = new OnlyOnceErrorManager();
private boolean enabled = true;

public WildFlyGelfLogHandler() {
super();
super.setErrorManager(DEFAULT_ERROR_MANAGER);
}

public boolean isEnabled() {
return enabled;
}

/**
* Manually enable/disable the handler. This is also called by wildfly logger setup routines on server-startup with the
* value of the "enabled" attribute of <custom-handler>
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

protected void initializeDefaultFields() {
gelfMessageAssembler.addFields(LogMessageField.getDefaultMapping(Time, Severity, ThreadName, SourceClassName,
SourceMethodName, SourceSimpleClassName, LoggerName, NDC));
}

@Override
public boolean isLoggable(LogRecord record) {
return enabled && super.isLoggable(record);
}

@Override
public void publish(LogRecord record) {
super.publish(ExtLogRecord.wrap(record));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,20 @@ public void execute() throws Throwable {
}
});
}

@Test
public void testDisabled() {
WildFlyGelfLogHandler handler = getWildFlyGelfLogHandler();
handler.setEnabled(false);

Logger logger = Logger.getLogger(getClass().getName());
logger.addHandler(handler);

logger.info(LOG_MESSAGE);

assertThat(handler.isEnabled()).isFalse();
assertThat(GelfTestSender.getMessages()).isEmpty();


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package biz.paluch.logging.gelf.wildfly;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;

import org.apache.log4j.LogManager;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.as.arquillian.api.ServerSetup;
import org.jboss.as.arquillian.api.ServerSetupTask;
import org.jboss.as.arquillian.container.ManagementClient;
import org.jboss.as.cli.CommandContext;
import org.jboss.as.cli.CommandContextFactory;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.Test;
import org.junit.runner.RunWith;

import biz.paluch.logging.gelf.netty.GelfInboundHandler;
import biz.paluch.logging.gelf.netty.NettyLocalServer;
import io.netty.channel.socket.nio.NioDatagramChannel;

/**
* Test for the GelfLogHandler of the logstash-gelf module in WildFly.
*
* @author Mark Paluch
* @since 11.08.14 08:36
*/
@RunWith(Arquillian.class)
@ServerSetup({ WildFlyHandlerDisabledIntegrationTests.LoggerSetup.class })
public class WildFlyHandlerDisabledIntegrationTests {

static class LoggerSetup implements ServerSetupTask {

@Override
public void setup(ManagementClient managementClient, String containerId) throws Exception {

CommandContext commandContext = CommandContextFactory.getInstance().newCommandContext();
commandContext.bindClient(managementClient.getControllerClient());
commandContext
.handle("/subsystem=logging/custom-handler=GelfLogger/:add(module=biz.paluch.logging,class=biz.paluch.logging.gelf.wildfly.WildFlyGelfLogHandler,enabled=false,properties={ \\\n"
+ " host=\"udp:localhost\", \\\n"
+ " port=\"19392\", \\\n"
+ " version=\"1.0\", \\\n"
+ " facility=\"java-test\", \\\n"
+ " extractStackTrace=true, \\\n"
+ " filterStackTrace=true, \\\n"
+ " mdcProfiling=true, \\\n"
+ " timestampPattern=\"yyyy-MM-dd HH:mm:ss,SSS\", \\\n"
+ " maximumMessageSize=8192, \\\n"
+ " additionalFields=\"fieldName1=fieldValue1,fieldName2=fieldValue2\", \\\n"
+ " mdcFields=\"mdcField1,mdcField2\" \\\n"
+ " dynamicMdcFields=\"mdc.*,(mdc|MDC)fields\" \\\n"
+ " includeFullMdc=true \\\n"
+ " })");

commandContext.handle(
"/subsystem=logging/root-logger=ROOT/:write-attribute(name=handlers,value=[\"FILE\",\"CONSOLE\",\"GelfLogger\"])");
}

@Override
public void tearDown(ManagementClient managementClient, String containerId) throws Exception {
CommandContext commandContext = CommandContextFactory.getInstance().newCommandContext();
commandContext.bindClient(managementClient.getControllerClient());
commandContext
.handle("/subsystem=logging/root-logger=ROOT/:write-attribute(name=handlers,value=[\"FILE\",\"CONSOLE\"])");
commandContext.handle("/subsystem=logging/custom-handler=GelfLogger/:remove()");
}
}

@Deployment
public static Archive<?> createTestArchive() {

File[] files = Maven.resolver().loadPomFromFile("pom.xml").resolve("io.netty:netty-all", "org.assertj:assertj-core")
.withoutTransitivity().asFile();
return ShrinkWrap.create(WebArchive.class, "logstash-gelf.war").addAsLibraries(files).addClasses(NettyLocalServer.class,
GelfInboundHandler.class);
}

@Test
public void testGelfSubmissionToEmbeddedNettyGelfServer() throws Exception {
NettyLocalServer nettyLocalServer = new NettyLocalServer(NioDatagramChannel.class);
nettyLocalServer.run();
String logMessage = "some log event, console/file only, gelf disabled";

int iterations = 0;
while (nettyLocalServer.getJsonValues().isEmpty() && iterations < 10) {
LogManager.getLogger(getClass()).info(logMessage);
Thread.sleep(100);
iterations++;
}

assertThat(nettyLocalServer.getJsonValues()).isEmpty();

boolean foundSomeLogEvent = false;

for (Object o : nettyLocalServer.getJsonValues()) {
if (o.toString().contains(logMessage)) {
foundSomeLogEvent = true;
}
}
assertThat(foundSomeLogEvent).isFalse();
nettyLocalServer.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,19 @@ public void setup(ManagementClient managementClient, String containerId) throws
commandContext.bindClient(managementClient.getControllerClient());
commandContext
.handle("/subsystem=logging/custom-handler=GelfLogger/:add(module=biz.paluch.logging,class=biz.paluch.logging.gelf.wildfly.WildFlyGelfLogHandler,properties={ \\\n"
+ " host=\"udp:localhost\", \\\n" + " port=\"19392\", \\\n"
+ " version=\"1.0\", \\\n" + "\t\t facility=\"java-test\", \\\n"
+ "\t\t extractStackTrace=true, \\\n" + "\t\t filterStackTrace=true, \\\n"
+ "\t\t mdcProfiling=true, \\\n" + "\t\t timestampPattern=\"yyyy-MM-dd HH:mm:ss,SSS\", \\\n"
+ "\t\t maximumMessageSize=8192, \\\n"
+ "\t\t additionalFields=\"fieldName1=fieldValue1,fieldName2=fieldValue2\", \\\n"
+ "\t\t mdcFields=\"mdcField1,mdcField2\" \\\n"
+ "\t\t dynamicMdcFields=\"mdc.*,(mdc|MDC)fields\" \\\n" + "\t\t includeFullMdc=true \\\n"
+ " host=\"udp:localhost\", \\\n"
+ " port=\"19392\", \\\n"
+ " version=\"1.0\", \\\n"
+ " facility=\"java-test\", \\\n"
+ " extractStackTrace=true, \\\n"
+ " filterStackTrace=true, \\\n"
+ " mdcProfiling=true, \\\n"
+ " timestampPattern=\"yyyy-MM-dd HH:mm:ss,SSS\", \\\n"
+ " maximumMessageSize=8192, \\\n"
+ " additionalFields=\"fieldName1=fieldValue1,fieldName2=fieldValue2\", \\\n"
+ " mdcFields=\"mdcField1,mdcField2\" \\\n"
+ " dynamicMdcFields=\"mdc.*,(mdc|MDC)fields\" \\\n"
+ " includeFullMdc=true \\\n"
+ " })");

commandContext.handle(
Expand Down

0 comments on commit 92251de

Please sign in to comment.