Skip to content
Permalink
Browse files

Merge pull request #3678 from jamezp/WFCORE-4332

[WFCORE-4332] Add the named-formatter attribute to the syslog-handler.
  • Loading branch information
jmesnil committed Apr 5, 2019
2 parents 3889c29 + abfab88 commit 40921d08ca4fe47c1a336ee9db29d01b11959955
Showing with 1,632 additions and 20 deletions.
  1. +2 −1 logging/src/main/java/org/jboss/as/logging/KnownModelVersion.java
  2. +6 −2 logging/src/main/java/org/jboss/as/logging/LoggingExtension.java
  3. +151 −0 logging/src/main/java/org/jboss/as/logging/LoggingSubsystemParser_7_0.java
  4. +10 −1 logging/src/main/java/org/jboss/as/logging/LoggingSubsystemWriter.java
  5. +3 −1 logging/src/main/java/org/jboss/as/logging/Namespace.java
  6. +11 −0 logging/src/main/java/org/jboss/as/logging/handlers/HandlerOperations.java
  7. +28 −4 logging/src/main/java/org/jboss/as/logging/handlers/SyslogHandlerResourceDefinition.java
  8. +1 −0 logging/src/main/resources/org/jboss/as/logging/LocalDescriptions.properties
  9. +825 −0 logging/src/main/resources/schema/jboss-as-logging_7_0.xsd
  10. +1 −1 logging/src/main/resources/subsystem-templates/logging.xml
  11. +12 −3 logging/src/test/java/org/jboss/as/logging/LoggingSubsystemTestCase.java
  12. +1 −1 logging/src/test/resources/default-subsystem.xml
  13. +1 −1 logging/src/test/resources/empty-subsystem.xml
  14. +3 −1 logging/src/test/resources/expressions.xml
  15. +249 −0 logging/src/test/resources/expressions_6_0.xml
  16. +3 −1 logging/src/test/resources/logging.xml
  17. +273 −0 logging/src/test/resources/logging_6_0.xml
  18. +1 −1 logging/src/test/resources/operations.xml
  19. +1 −1 logging/src/test/resources/rollback-logging.xml
  20. +1 −1 logging/src/test/resources/simple-subsystem.xml
  21. +49 −0 .../standalone/src/test/java/org/jboss/as/test/integration/logging/syslog/SyslogHandlerTestCase.java
@@ -14,7 +14,8 @@
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;
private final boolean hasTransformers;
@@ -98,7 +98,7 @@

private static final GenericSubsystemDescribeHandler DESCRIBE_HANDLER = GenericSubsystemDescribeHandler.create(LoggingChildResourceComparator.INSTANCE);

private static final int MANAGEMENT_API_MAJOR_VERSION = 7;
private static final int MANAGEMENT_API_MAJOR_VERSION = 8;
private static final int MANAGEMENT_API_MINOR_VERSION = 0;
private static final int MANAGEMENT_API_MICRO_VERSION = 0;

@@ -269,6 +269,7 @@ public void initializeParsers(final ExtensionParsingContext context) {
setParser(context, Namespace.LOGGING_4_0, new LoggingSubsystemParser_4_0());
setParser(context, Namespace.LOGGING_5_0, new LoggingSubsystemParser_5_0());
setParser(context, Namespace.LOGGING_6_0, new LoggingSubsystemParser_6_0());
setParser(context, Namespace.LOGGING_7_0, new LoggingSubsystemParser_7_0());

// Hack to ensure the Element and Attribute enums are loaded during this call which
// is part of concurrent boot. These enums trigger a lot of classloading and static
@@ -358,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.
@@ -368,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(),
});
}

@@ -0,0 +1,151 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2019 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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 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();
@@ -56,12 +56,14 @@
LOGGING_5_0("urn:jboss:domain:logging:5.0"),

LOGGING_6_0("urn:jboss:domain:logging:6.0"),

LOGGING_7_0("urn:jboss:domain:logging:7.0"),
;

/**
* The current namespace version.
*/
public static final Namespace CURRENT = LOGGING_6_0;
public static final Namespace CURRENT = LOGGING_7_0;

private final String name;

@@ -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.

0 comments on commit 40921d0

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