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

Commit

Permalink
Polishing #170
Browse files Browse the repository at this point in the history
Add enabled property to JBoss7GelfLogHandler to mirror a consistent API. Add documentation. Reformat code. Drop WildFlyHandlerDisabledIntegrationTests.

Original pull request: #171.
  • Loading branch information
mp911de committed Dec 14, 2018
1 parent 316ace0 commit 1f1dcb1
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* <li>See docs for more details</li>
* </ul>
* </li>
* <li>enabled (Optional): Whether this handler is enabled, default true</li>
* <li>port (Optional): Port, default 12201</li>
* <li>originHost (Optional): Originating Hostname, default FQDN Hostname</li>
* <li>extractStackTrace (Optional): Post Stack-Trace to StackTrace field (true/false/throwable reference [0 = throwable, 1 =
Expand All @@ -38,17 +39,15 @@
* <li>additionalFields(number) (Optional): Post additional fields. Eg.
* .GelfLogHandler.additionalFields=fieldName=Value,field2=value2</li>
* <li>additionalFieldTypes (Optional): Type specification for additional and MDC fields. Supported types: String, long, Long,
* double, Double and discover (default if not specified, discover field type on parseability). Eg.
* field=String,field2=double</li>
* double, Double and discover (default if not specified, discover field type on parseability). Eg. field=String,field2=double</li>
* <li>mdcFields (Optional): Post additional fields, pull Values from MDC. Name of the Fields are comma-separated
* .JBoss7GelfLogHandler.mdcFields=Application,Version,SomeOtherFieldName</li>
* <li>dynamicMdcFields (Optional): Dynamic MDC Fields allows you to extract MDC values based on one or more regular
* expressions. Multiple regex are comma-separated. The name of the MDC entry is used as GELF field name.
* .JBoss7GelfLogHandler.dynamicMdcFields=mdc.*,[mdc|MDC]fields</li>
* <li>includeFullMdc (Optional): Include all fields from the MDC, default false</li>
* </ul>
* <a name="mdcProfiling"></a>
* <h2>MDC Profiling</h2>
* <a name="mdcProfiling"></a> <h2>MDC Profiling</h2>
* <p>
* MDC Profiling allows to calculate the runtime from request start up to the time until the log message was generated. You must
* set one value in the MDC:
Expand All @@ -70,16 +69,35 @@
public class JBoss7GelfLogHandler extends biz.paluch.logging.gelf.jul.GelfLogHandler {

private static final ErrorManager DEFAULT_ERROR_MANAGER = new OnlyOnceErrorManager();
private boolean enabled = true;

public JBoss7GelfLogHandler() {
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 @@ -25,6 +25,7 @@
* <li>See docs for more details</li>
* </ul>
* </li>
* <li>enabled (Optional): Whether this handler is enabled, default true</li>
* <li>port (Optional): Port, default 12201</li>
* <li>originHost (Optional): Originating Hostname, default FQDN Hostname</li>
* <li>extractStacktrace (Optional): Post Stack-Trace to StackTrace field, default false</li>
Expand Down Expand Up @@ -64,6 +65,7 @@
* The {@link #publish(LogRecord)} method is thread-safe and may be called by different threads at any time.
*
* @author Mark Paluch
* @author Christoph Linder
*/
public class WildFlyGelfLogHandler extends GelfLogHandler {
private static final ErrorManager DEFAULT_ERROR_MANAGER = new OnlyOnceErrorManager();
Expand Down
3 changes: 2 additions & 1 deletion src/site/markdown/examples/jbossas7.md.vm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Following settings can be used:

| Attribute Name | Description | Default |
| ----------------- |:------------------------------------:|:-------:|
| enabled | Whether this handler is enabled | `true` |
| host | Hostname/IP-Address of the Logstash host. The `host` field accepts following forms: <ul><li>`tcp:hostname` for TCP transport, e. g. `tcp:127.0.0.1` or `tcp:some.host.com` </li><li>`udp:hostname` for UDP transport, e. g. `udp:127.0.0.1`, `udp:some.host.com` or just `some.host.com` </li><li>`redis://[:password@]hostname:port/db-number#listname` for Redis transport. See [Redis transport for logstash-gelf](../redis.html) for details. </li><li>`redis-sentinel://[:password@]hostname:port/db-number?masterId=masterId#listname` for Redis transport with Sentinel lookup. See [Redis transport for logstash-gelf](../redis.html) for details. </li></ul> | none |
| port | Port of the Logstash host | `12201` |
| version | GELF Version `1.0` or `1.1` | `1.0` |
Expand Down Expand Up @@ -43,7 +44,7 @@ JBoss AS7 Logging Configuration

XML Configuration:

<custom-handler name="GelfLogger" class="biz.paluch.logging.gelf.jboss7.JBoss7GelfLogHandler" module="biz.paluch.logging">
<custom-handler name="GelfLogger" class="biz.paluch.logging.gelf.jboss7.JBoss7GelfLogHandler" module="biz.paluch.logging" enabled="true">
<level name="INFO" />
<properties>
<property name="host" value="udp:localhost" />
Expand Down
2 changes: 2 additions & 0 deletions src/site/markdown/examples/wildfly-swarm-thorntail.md.vm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Following settings can be used:

| Attribute Name | Description | Default |
| ----------------- |:------------------------------------:|:-------:|
| enabled | Whether this handler is enabled | `true` |
| host | Hostname/IP-Address of the Logstash host. The `host` field accepts following forms: <ul><li>`tcp:hostname` for TCP transport, e. g. `tcp:127.0.0.1` or `tcp:some.host.com` </li><li>`udp:hostname` for UDP transport, e. g. `udp:127.0.0.1`, `udp:some.host.com` or just `some.host.com` </li><li>`redis://[:password@]hostname:port/db-number#listname` for Redis transport. See [Redis transport for logstash-gelf](../redis.html) for details. </li><li>`redis-sentinel://[:password@]hostname:port/db-number?masterId=masterId#listname` for Redis transport with Sentinel lookup. See [Redis transport for logstash-gelf](../redis.html) for details. </li></ul> | none |
| port | Port of the Logstash host | `12201` |
| version | GELF Version `1.0` or `1.1` | `1.0` |
Expand Down Expand Up @@ -50,6 +51,7 @@ swarm:
attribute-class: biz.paluch.logging.gelf.wildfly.WildFlyGelfLogHandler
module: biz.paluch.logging
properties:
enabled=true
host=udp:localhost
port=12201
version=1.0
Expand Down
3 changes: 2 additions & 1 deletion src/site/markdown/examples/wildfly.md.vm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Following settings can be used:

| Attribute Name | Description | Default |
| ----------------- |:------------------------------------:|:-------:|
| enabled | Whether this handler is enabled | `true` |
| host | Hostname/IP-Address of the Logstash host. The `host` field accepts following forms: <ul><li>`tcp:hostname` for TCP transport, e. g. `tcp:127.0.0.1` or `tcp:some.host.com` </li><li>`udp:hostname` for UDP transport, e. g. `udp:127.0.0.1`, `udp:some.host.com` or just `some.host.com` </li><li>`redis://[:password@]hostname:port/db-number#listname` for Redis transport. See [Redis transport for logstash-gelf](../redis.html) for details. </li><li>`redis-sentinel://[:password@]hostname:port/db-number?masterId=masterId#listname` for Redis transport with Sentinel lookup. See [Redis transport for logstash-gelf](../redis.html) for details. </li></ul> | none |
| port | Port of the Logstash host | `12201` |
| version | GELF Version `1.0` or `1.1` | `1.0` |
Expand Down Expand Up @@ -44,7 +45,7 @@ WildFly Logging Configuration

XML Configuration:

<custom-handler name="GelfLogger" class="biz.paluch.logging.gelf.wildfly.WildFlyGelfLogHandler" module="biz.paluch.logging">
<custom-handler name="GelfLogger" class="biz.paluch.logging.gelf.wildfly.WildFlyGelfLogHandler" module="biz.paluch.logging" enabled="true">
<level name="INFO" />
<properties>
<property name="host" value="udp:localhost" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public void testSimple() throws Exception {
assertThat(gelfMessage.getMaximumMessageSize()).isEqualTo(8192);
assertThat(gelfMessage.getField(LogMessageField.NamedLogField.SourceMethodName.name())).isEqualTo("testSimple");
assertThat(gelfMessage.getField(LogMessageField.NamedLogField.SourceClassName.name())).isEqualTo(getClass().getName());

}

@Test
Expand Down Expand Up @@ -102,7 +101,6 @@ public void testFine() throws Exception {
logger.severe(LOG_MESSAGE);
assertThat(GelfTestSender.getMessages().get(0).getLevel()).isEqualTo("3");
GelfTestSender.getMessages().clear();

}

@Test
Expand All @@ -122,7 +120,6 @@ public void testSevere() throws Exception {
GelfMessage gelfMessage = GelfTestSender.getMessages().get(0);

assertThat(gelfMessage.getLevel()).isEqualTo("3");

}

@Test
Expand All @@ -135,7 +132,6 @@ public void testEmptyMessage() throws Exception {

logger.info("");
assertThat(GelfTestSender.getMessages()).isEmpty();

}

@Test
Expand All @@ -157,7 +153,6 @@ public void testSimpleWithMsgFormatSubstitution() throws Exception {
assertThat(gelfMessage.getShortMessage()).isEqualTo(expectedMessage);
assertThat(gelfMessage.getLevel()).isEqualTo("6");
assertThat(gelfMessage.getMaximumMessageSize()).isEqualTo(8192);

}

@Test
Expand All @@ -178,7 +173,6 @@ public void testSimpleWithStringFormatSubstitution() throws Exception {

assertThat(gelfMessage.getFullMessage()).isEqualTo(expectedMessage);
assertThat(gelfMessage.getShortMessage()).isEqualTo(expectedMessage);

}

@Test
Expand Down Expand Up @@ -214,6 +208,20 @@ public void execute() throws Throwable {
handler.setGraylogPort(0);
}
});
}

@Test
public void testDisabled() {

JBoss7GelfLogHandler handler = getJBoss7GelfLogHandler();
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
Expand Up @@ -21,6 +21,7 @@

/**
* @author Mark Paluch
* @author Christoph Linder
* @since 11.08.14 08:36
*/
public class WildFlyGelfLogHandlerTests {
Expand Down Expand Up @@ -60,7 +61,6 @@ public void testSimple() throws Exception {
assertThat(gelfMessage.getMaximumMessageSize()).isEqualTo(8192);
assertThat(gelfMessage.getField(LogMessageField.NamedLogField.SourceMethodName.name())).isEqualTo("testSimple");
assertThat(gelfMessage.getField(LogMessageField.NamedLogField.SourceClassName.name())).isEqualTo(getClass().getName());

}

@Test
Expand Down Expand Up @@ -102,7 +102,6 @@ public void testFine() throws Exception {
logger.severe(LOG_MESSAGE);
assertThat(GelfTestSender.getMessages().get(0).getLevel()).isEqualTo("3");
GelfTestSender.getMessages().clear();

}

@Test
Expand All @@ -121,7 +120,6 @@ public void testSevere() throws Exception {
GelfMessage gelfMessage = GelfTestSender.getMessages().get(0);

assertThat(gelfMessage.getLevel()).isEqualTo("3");

}

@Test
Expand All @@ -133,7 +131,6 @@ public void testEmptyMessage() throws Exception {

logger.info("");
assertThat(GelfTestSender.getMessages()).isEmpty();

}

@Test
Expand All @@ -154,7 +151,6 @@ public void testSimpleWithMsgFormatSubstitution() throws Exception {
assertThat(gelfMessage.getShortMessage()).isEqualTo(expectedMessage);
assertThat(gelfMessage.getLevel()).isEqualTo("6");
assertThat(gelfMessage.getMaximumMessageSize()).isEqualTo(8192);

}

@Test
Expand All @@ -174,7 +170,6 @@ public void testSimpleWithStringFormatSubstitution() throws Exception {

assertThat(gelfMessage.getFullMessage()).isEqualTo(expectedMessage);
assertThat(gelfMessage.getShortMessage()).isEqualTo(expectedMessage);

}

@Test
Expand All @@ -195,7 +190,6 @@ public void testFields() throws Exception {
assertThat(gelfMessage.getField("fieldName2")).isEqualTo("fieldValue2");
assertThat(gelfMessage.getField("mdcField1")).isEqualTo("a value");
assertThat(gelfMessage.getField("mdcField2")).isNull();

}

@Test
Expand Down Expand Up @@ -235,6 +229,7 @@ public void execute() throws Throwable {

@Test
public void testDisabled() {

WildFlyGelfLogHandler handler = getWildFlyGelfLogHandler();
handler.setEnabled(false);

Expand All @@ -245,7 +240,5 @@ public void testDisabled() {

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


}
}

This file was deleted.

0 comments on commit 1f1dcb1

Please sign in to comment.