Skip to content
Permalink
Browse files

[WFCORE-4332] Add the named-formatter attribute to the syslog-handler.

  • Loading branch information
jamezp authored and jmesnil committed Feb 19, 2019
1 parent d4b5cf3 commit 6bd86929bbaba5fa35b5ebbb0c2ed76082e04eba
@@ -14,7 +14,7 @@
VERSION_4_0_0(ModelVersion.create(4, 0, 0), false),
VERSION_5_0_0(ModelVersion.create(5, 0, 0), true),
VERSION_6_0_0(ModelVersion.create(6, 0, 0), true),
VERSION_7_0_0(ModelVersion.create(7, 0, 0), false),
VERSION_7_0_0(ModelVersion.create(7, 0, 0), true),
VERSION_8_0_0(ModelVersion.create(8, 0, 0), false),
;
private final ModelVersion modelVersion;
@@ -359,7 +359,8 @@ private void registerSubModels(final ManagementResourceRegistration registration
private void registerTransformers(final SubsystemRegistration registration, final TransformerResourceDefinition... defs) {
ChainedTransformationDescriptionBuilder chainedBuilder = TransformationDescriptionBuilder.Factory.createChainedSubystemInstance(registration.getSubsystemVersion());

registerTransformers(chainedBuilder, registration.getSubsystemVersion(), KnownModelVersion.VERSION_6_0_0, defs);
registerTransformers(chainedBuilder, registration.getSubsystemVersion(), KnownModelVersion.VERSION_7_0_0, defs);
registerTransformers(chainedBuilder, KnownModelVersion.VERSION_7_0_0, KnownModelVersion.VERSION_6_0_0, defs);
registerTransformers(chainedBuilder, KnownModelVersion.VERSION_6_0_0, KnownModelVersion.VERSION_5_0_0, defs);
registerTransformers(chainedBuilder, KnownModelVersion.VERSION_5_0_0, KnownModelVersion.VERSION_2_0_0, defs);
// Version 1.5.0 has the periodic-size-rotating-file-handler and the suffix attribute on the size-rotating-file-handler.
@@ -369,12 +370,14 @@ private void registerTransformers(final SubsystemRegistration registration, fina
chainedBuilder.buildAndRegister(registration, new ModelVersion[] {
KnownModelVersion.VERSION_2_0_0.getModelVersion(),
KnownModelVersion.VERSION_6_0_0.getModelVersion(),
KnownModelVersion.VERSION_7_0_0.getModelVersion(),
}, new ModelVersion[] {
KnownModelVersion.VERSION_1_5_0.getModelVersion(),
KnownModelVersion.VERSION_3_0_0.getModelVersion(),
KnownModelVersion.VERSION_4_0_0.getModelVersion(),
KnownModelVersion.VERSION_5_0_0.getModelVersion(),
KnownModelVersion.VERSION_6_0_0.getModelVersion(),
KnownModelVersion.VERSION_7_0_0.getModelVersion(),
});
}

@@ -19,10 +19,133 @@

package org.jboss.as.logging;

import static org.jboss.as.controller.parsing.ParseUtils.duplicateNamedElement;
import static org.jboss.as.controller.parsing.ParseUtils.missingRequired;
import static org.jboss.as.controller.parsing.ParseUtils.readStringAttributeElement;
import static org.jboss.as.controller.parsing.ParseUtils.requireNoNamespaceAttribute;
import static org.jboss.as.controller.parsing.ParseUtils.unexpectedAttribute;
import static org.jboss.as.controller.parsing.ParseUtils.unexpectedElement;
import static org.jboss.as.logging.CommonAttributes.ENABLED;
import static org.jboss.as.logging.CommonAttributes.LEVEL;
import static org.jboss.as.logging.handlers.SyslogHandlerResourceDefinition.APP_NAME;
import static org.jboss.as.logging.handlers.SyslogHandlerResourceDefinition.FACILITY;
import static org.jboss.as.logging.handlers.SyslogHandlerResourceDefinition.HOSTNAME;
import static org.jboss.as.logging.handlers.SyslogHandlerResourceDefinition.PORT;
import static org.jboss.as.logging.handlers.SyslogHandlerResourceDefinition.SERVER_ADDRESS;
import static org.jboss.as.logging.handlers.SyslogHandlerResourceDefinition.SYSLOG_FORMATTER;

import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import javax.xml.stream.XMLStreamException;

import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.operations.common.Util;
import org.jboss.as.controller.parsing.ParseUtils;
import org.jboss.as.logging.handlers.SyslogHandlerResourceDefinition;
import org.jboss.dmr.ModelNode;
import org.jboss.staxmapper.XMLExtendedStreamReader;

/**
* Subsystem parser for 6.0 of the logging subsystem.
* Subsystem parser for 7.0 of the logging subsystem.
*
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
class LoggingSubsystemParser_7_0 extends LoggingSubsystemParser_6_0 {

@Override
void parseSyslogHandler(final XMLExtendedStreamReader reader, final PathAddress address, final List<ModelNode> operations, final Set<String> names) throws XMLStreamException {
final ModelNode operation = Util.createAddOperation();
// Attributes
String name = null;
final EnumSet<Attribute> required = EnumSet.of(Attribute.NAME);
final int count = reader.getAttributeCount();
for (int i = 0; i < count; i++) {
requireNoNamespaceAttribute(reader, i);
final String value = reader.getAttributeValue(i);
final Attribute attribute = Attribute.forName(reader.getAttributeLocalName(i));
required.remove(attribute);
switch (attribute) {
case NAME: {
name = value;
break;
}
case ENABLED:
ENABLED.parseAndSetParameter(value, operation, reader);
break;
default:
throw unexpectedAttribute(reader, i);
}
}
if (!required.isEmpty()) {
throw missingRequired(reader, required);
}
if (!names.add(name)) {
throw duplicateNamedElement(reader, name);
}

// Setup the operation address
addOperationAddress(operation, address, SyslogHandlerResourceDefinition.NAME, name);

final EnumSet<Element> encountered = EnumSet.noneOf(Element.class);
while (reader.nextTag() != END_ELEMENT) {
final Element element = Element.forName(reader.getLocalName());
if (!encountered.add(element)) {
throw unexpectedElement(reader);
}
switch (element) {
case APP_NAME: {
APP_NAME.parseAndSetParameter(readValueAttribute(reader), operation, reader);
break;
}
case FACILITY: {
FACILITY.parseAndSetParameter(readValueAttribute(reader), operation, reader);
break;
}
case HOSTNAME: {
HOSTNAME.parseAndSetParameter(readValueAttribute(reader), operation, reader);
break;
}
case LEVEL: {
LEVEL.parseAndSetParameter(readNameAttribute(reader), operation, reader);
break;
}
case FORMATTER: {
final EnumSet<Element> requiredFormatter = EnumSet.of(Element.SYSLOG_FORMATTER);
while (reader.nextTag() != END_ELEMENT) {
switch (Element.forName(reader.getLocalName())) {
case SYSLOG_FORMATTER: {
requiredFormatter.remove(Element.SYSLOG_FORMATTER);
operation.get(SYSLOG_FORMATTER.getName()).set(readStringAttributeElement(reader, Attribute.SYSLOG_TYPE.getLocalName()));
break;
}
case NAMED_FORMATTER: {
SyslogHandlerResourceDefinition.NAMED_FORMATTER.parseAndSetParameter(readNameAttribute(reader), operation, reader);
break;
}
default: {
throw unexpectedElement(reader);
}
}
}
if (!requiredFormatter.isEmpty()) {
throw ParseUtils.missingRequiredElement(reader, requiredFormatter);
}
break;
}
case PORT: {
PORT.parseAndSetParameter(readValueAttribute(reader), operation, reader);
break;
}
case SERVER_ADDRESS: {
SERVER_ADDRESS.parseAndSetParameter(readValueAttribute(reader), operation, reader);
break;
}
default: {
throw unexpectedElement(reader);
}
}
}
operations.add(operation);
}
}
@@ -69,6 +69,7 @@
import org.jboss.as.logging.formatters.PatternFormatterResourceDefinition;
import org.jboss.as.logging.formatters.StructuredFormatterResourceDefinition;
import org.jboss.as.logging.formatters.XmlFormatterResourceDefinition;
import org.jboss.as.logging.handlers.AbstractHandlerDefinition;
import org.jboss.as.logging.handlers.AsyncHandlerResourceDefinition;
import org.jboss.as.logging.handlers.ConsoleHandlerResourceDefinition;
import org.jboss.as.logging.handlers.CustomHandlerResourceDefinition;
@@ -358,7 +359,15 @@ private void writeSyslogHandler(final XMLExtendedStreamWriter writer, final Mode
HOSTNAME.marshallAsElement(model, writer);
PORT.marshallAsElement(model, writer);
APP_NAME.marshallAsElement(model, writer);
SYSLOG_FORMATTER.marshallAsElement(model, writer);

// Write the formatter elements
if (model.hasDefined(SYSLOG_FORMATTER.getName()) || model.hasDefined(SyslogHandlerResourceDefinition.NAMED_FORMATTER.getName())) {
writer.writeStartElement(AbstractHandlerDefinition.FORMATTER.getXmlName());
SYSLOG_FORMATTER.marshallAsElement(model, writer);
SyslogHandlerResourceDefinition.NAMED_FORMATTER.marshallAsElement(model, writer);
writer.writeEndElement();
}

FACILITY.marshallAsElement(model, writer);

writer.writeEndElement();
@@ -76,6 +76,7 @@
import org.jboss.logmanager.config.PropertyConfigurable;
import org.jboss.logmanager.formatters.PatternFormatter;
import org.jboss.logmanager.handlers.AsyncHandler;
import org.jboss.logmanager.handlers.SyslogHandler;
import org.jboss.modules.ModuleLoadException;
import org.jboss.modules.ModuleLoader;

@@ -654,6 +655,16 @@ private static void handleProperty(final AttributeDefinition attribute, final Op
if (logContextConfiguration.getFormatterNames().contains(handlerName)) {
logContextConfiguration.removeFormatterConfiguration(handlerName);
}
} else if (configuration.getClassName().equals(SyslogHandler.class.getName())) {
// The value shouldn't be defined so we want to remove the current formatter, however a null formatter
// is not allowed in a java.util.logging.Handler. Therefore we must configure a formatter of some kind.
// We'll skip the config step on this and set the handler manually. This allows the formatter
// configuration not to be persisted. By default the SyslogHandler will use
// ExtLogRecord.getFormattedMessage() to get the message which the %s pattern should duplicate.
final Handler instance = configuration.getInstance();
if (instance != null) {
instance.setFormatter(new PatternFormatter("%s"));
}
} else {
// If the named-formatter was undefined we need to create a formatter based on the formatter attribute
final FormatterConfiguration fmtConfig;
@@ -33,9 +33,13 @@
import org.jboss.as.controller.DefaultAttributeMarshaller;
import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.SimpleAttributeDefinition;
import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
import org.jboss.as.controller.operations.validation.EnumValidator;
import org.jboss.as.controller.operations.validation.IntRangeValidator;
import org.jboss.as.controller.registry.ManagementResourceRegistration;
import org.jboss.as.controller.transform.description.DiscardAttributeChecker;
import org.jboss.as.controller.transform.description.RejectAttributeChecker;
import org.jboss.as.controller.transform.description.ResourceTransformationDescriptionBuilder;
import org.jboss.as.logging.Attribute;
import org.jboss.as.logging.ElementAttributeMarshaller;
@@ -104,12 +108,10 @@
@Override
public void marshallAsElement(final AttributeDefinition attribute, final ModelNode resourceModel, final boolean marshallDefault, final XMLStreamWriter writer) throws XMLStreamException {
if (isMarshallable(attribute, resourceModel, marshallDefault)) {
writer.writeStartElement(AbstractHandlerDefinition.FORMATTER.getXmlName());
writer.writeStartElement(attribute.getXmlName());
final String content = resourceModel.get(attribute.getName()).asString();
writer.writeAttribute(Attribute.SYSLOG_TYPE.getLocalName(), content);
writer.writeEndElement();
writer.writeEndElement();
}
}
})
@@ -118,6 +120,13 @@ public void marshallAsElement(final AttributeDefinition attribute, final ModelNo
.setValidator(EnumValidator.create(SyslogType.class, EnumSet.allOf(SyslogType.class)))
.build();

// This is being redefined here to clear the "formatter" from the alternatives and redefine the how the attribute is
// persisted to the configuration file.
public static final SimpleAttributeDefinition NAMED_FORMATTER = SimpleAttributeDefinitionBuilder.create(AbstractHandlerDefinition.NAMED_FORMATTER)
.setAlternatives()
.setAttributeMarshaller(ElementAttributeMarshaller.NAME_ATTRIBUTE_MARSHALLER)
.build();

/*
* Attributes
*/
@@ -129,7 +138,8 @@ public void marshallAsElement(final AttributeDefinition attribute, final ModelNo
LEVEL,
PORT,
SERVER_ADDRESS,
SYSLOG_FORMATTER
SYSLOG_FORMATTER,
NAMED_FORMATTER
};

private static final HandlerAddOperationStepHandler ADD_HANDLER = new HandlerAddOperationStepHandler(SyslogHandler.class, ATTRIBUTES);
@@ -153,7 +163,21 @@ public void registerAttributes(final ManagementResourceRegistration resourceRegi

@Override
public void registerTransformers(final KnownModelVersion modelVersion, final ResourceTransformationDescriptionBuilder rootResourceBuilder, final ResourceTransformationDescriptionBuilder loggingProfileBuilder) {
//
switch (modelVersion) {
case VERSION_7_0_0: {
final ResourceTransformationDescriptionBuilder resourceBuilder = rootResourceBuilder.addChildResource(SYSLOG_HANDLER_PATH);
final ResourceTransformationDescriptionBuilder loggingProfileResourceBuilder = loggingProfileBuilder.addChildResource(SYSLOG_HANDLER_PATH);
resourceBuilder.getAttributeBuilder()
.setDiscard(DiscardAttributeChecker.UNDEFINED, NAMED_FORMATTER)
.addRejectCheck(RejectAttributeChecker.DEFINED, NAMED_FORMATTER)
.end();
loggingProfileResourceBuilder.getAttributeBuilder()
.setDiscard(DiscardAttributeChecker.UNDEFINED, NAMED_FORMATTER)
.addRejectCheck(RejectAttributeChecker.DEFINED, NAMED_FORMATTER)
.end();
break;
}
}
}

public enum FacilityAttribute {
@@ -601,6 +601,7 @@ logging.syslog-handler.level=The log level specifying which message levels will
logging.syslog-handler.app-name=The app name used when formatting the message in RFC5424 format. By default the app name is "java".
logging.syslog-handler.facility= Facility as defined by RFC-5424 (http://tools.ietf.org/html/rfc5424)and RFC-3164 (http://tools.ietf.org/html/rfc3164).
logging.syslog-handler.hostname=The name of the host the messages are being sent from. For example the name of the host the application server is running on.
logging.syslog-handler.named-formatter=The name of the defined formatter to be used on the handler.
logging.syslog-handler.port=The port the syslog server is listening on.
logging.syslog-handler.server-address=The address of the syslog server.
logging.syslog-handler.syslog-format=Formats the log message according to the RFC specification.
@@ -786,9 +786,10 @@
Defines a formatter.
</xs:documentation>
</xs:annotation>
<xs:choice minOccurs="1" maxOccurs="1">
<xs:all minOccurs="1" maxOccurs="1">
<xs:element name="syslog-format" type="syslogFormatType" maxOccurs="1"/>
</xs:choice>
<xs:element name="named-formatter" type="namedFormatterType" minOccurs="0"/>
</xs:all>
</xs:complexType>

<xs:complexType name="syslogFormatType">
@@ -40,6 +40,7 @@
import org.jboss.as.logging.handlers.AbstractHandlerDefinition;
import org.jboss.as.logging.handlers.AsyncHandlerResourceDefinition;
import org.jboss.as.logging.handlers.ConsoleHandlerResourceDefinition;
import org.jboss.as.logging.handlers.SyslogHandlerResourceDefinition;
import org.jboss.as.logging.logmanager.ConfigurationPersistence;
import org.jboss.as.model.test.FailedOperationTransformationConfig;
import org.jboss.as.model.test.FailedOperationTransformationConfig.NewAttributesConfig;
@@ -163,8 +164,12 @@ public void testFailedTransformersEAP640() throws Exception {
FailedOperationTransformationConfig.REJECTED_RESOURCE)
.addFailedAttribute(SUBSYSTEM_ADDRESS.append("socket-handler"),
FailedOperationTransformationConfig.REJECTED_RESOURCE)
.addFailedAttribute(SUBSYSTEM_ADDRESS.append("syslog-handler"),
new NewAttributesConfig(SyslogHandlerResourceDefinition.NAMED_FORMATTER))
.addFailedAttribute(loggingProfileAddress.append("socket-handler"),
FailedOperationTransformationConfig.REJECTED_RESOURCE));
FailedOperationTransformationConfig.REJECTED_RESOURCE)
.addFailedAttribute(SUBSYSTEM_ADDRESS.append(CommonAttributes.LOGGING_PROFILE).append("syslog-handler"),
new NewAttributesConfig(SyslogHandlerResourceDefinition.NAMED_FORMATTER)));
}

@Test
@@ -186,8 +191,12 @@ public void testFailedTransformersEAP700() throws Exception {
FailedOperationTransformationConfig.REJECTED_RESOURCE)
.addFailedAttribute(SUBSYSTEM_ADDRESS.append("socket-handler"),
FailedOperationTransformationConfig.REJECTED_RESOURCE)
.addFailedAttribute(SUBSYSTEM_ADDRESS.append("syslog-handler"),
new NewAttributesConfig(SyslogHandlerResourceDefinition.NAMED_FORMATTER))
.addFailedAttribute(SUBSYSTEM_ADDRESS.append(CommonAttributes.LOGGING_PROFILE).append("socket-handler"),
FailedOperationTransformationConfig.REJECTED_RESOURCE));
FailedOperationTransformationConfig.REJECTED_RESOURCE)
.addFailedAttribute(SUBSYSTEM_ADDRESS.append(CommonAttributes.LOGGING_PROFILE).append("syslog-handler"),
new NewAttributesConfig(SyslogHandlerResourceDefinition.NAMED_FORMATTER)));
}

private void testEap7Transformer(final ModelTestControllerVersion controllerVersion, final ModelVersion legacyModelVersion, final String subsystemXml, final ModelFixer... modelFixers) throws Exception {

0 comments on commit 6bd8692

Please sign in to comment.
You can’t perform that action at this time.