Search in sources :

Example 1 with HandlerConfiguration

use of org.jboss.logmanager.config.HandlerConfiguration in project wildfly-core by wildfly.

the class AbstractLoggingSubsystemTest method compareHandlers.

// TODO (jrp) looking up property names is hard-coded, use a more dynamic approach
private void compareHandlers(final LogContextConfiguration logContextConfig, final Collection<String> handlerNames, final ModelNode model) throws OperationFailedException {
    final ModelNode clonedModel = model.clone();
    // Remove a socket-handler since it's wrapped in a DelayedHandler and the values will not match
    if (clonedModel.hasDefined(SocketHandlerResourceDefinition.NAME)) {
        final ModelNode socketHandlers = clonedModel.remove(SocketHandlerResourceDefinition.NAME);
        for (Property socketHandler : socketHandlers.asPropertyList()) {
            // Get the name of the socket-handler and remove it from the handler names
            handlerNames.remove(socketHandler.getName());
        }
    }
    // Compare property values for the handlers
    for (String name : handlerNames) {
        final HandlerConfiguration handlerConfig = logContextConfig.getHandlerConfiguration(name);
        final ModelNode handlerModel = findHandlerModel(clonedModel, name);
        final Set<String> modelPropertyNames = new HashSet<>(handlerModel.keys());
        final List<String> configPropertyNames = new ArrayList<>(handlerConfig.getPropertyNames());
        // Remove unneeded properties
        modelPropertyNames.remove(CommonAttributes.FILTER.getName());
        modelPropertyNames.remove(CommonAttributes.NAME.getName());
        // Process the properties
        for (String modelPropertyName : modelPropertyNames) {
            ModelNode modelValue = handlerModel.get(modelPropertyName);
            String modelStringValue = modelValue.asString();
            final String configValue;
            // Special properties
            if (modelPropertyName.equals(CommonAttributes.ENABLED.getName())) {
                final String propertyName = CommonAttributes.ENABLED.getPropertyName();
                if (configPropertyNames.contains(propertyName)) {
                    configValue = handlerConfig.getPropertyValueString(propertyName);
                } else {
                    continue;
                }
            } else if (modelPropertyName.equals(CommonAttributes.ENCODING.getName())) {
                configValue = handlerConfig.getEncoding();
            } else if (modelPropertyName.equals(AbstractHandlerDefinition.FORMATTER.getName()) || modelPropertyName.equals(AbstractHandlerDefinition.NAMED_FORMATTER.getName())) {
                // If there is a named formatter in the model, just match the names
                if (handlerModel.hasDefined(AbstractHandlerDefinition.NAMED_FORMATTER.getName())) {
                    configValue = handlerConfig.getFormatterName();
                    modelValue = handlerModel.get(AbstractHandlerDefinition.NAMED_FORMATTER.getName());
                } else {
                    // Not a named-formatter, so attempt to match the pattern
                    final String formatterName = handlerConfig.getFormatterName();
                    if (formatterName == null) {
                        configValue = null;
                    } else {
                        final FormatterConfiguration formatterConfig = logContextConfig.getFormatterConfiguration(formatterName);
                        configValue = formatterConfig.getPropertyValueString(PatternFormatterResourceDefinition.PATTERN.getName());
                        modelValue = handlerModel.get(AbstractHandlerDefinition.FORMATTER.getName());
                    }
                }
                modelStringValue = modelValue.asString();
            } else if (modelPropertyName.equals(AbstractHandlerDefinition.FILTER_SPEC.getName())) {
                configValue = handlerConfig.getFilter();
            } else if (modelPropertyName.equals(CommonAttributes.LEVEL.getName())) {
                configValue = handlerConfig.getLevel();
            } else {
                // Process custom properties
                final String configPropertyName;
                if (modelPropertyName.equals(CommonAttributes.AUTOFLUSH.getName())) {
                    configPropertyName = CommonAttributes.AUTOFLUSH.getPropertyName();
                } else if (modelPropertyName.equals(SizeRotatingHandlerResourceDefinition.ROTATE_SIZE.getName())) {
                    configPropertyName = SizeRotatingHandlerResourceDefinition.ROTATE_SIZE.getPropertyName();
                    modelStringValue = String.valueOf(SizeResolver.INSTANCE.parseSize(modelValue));
                } else if (modelPropertyName.equals(CommonAttributes.FILE.getName())) {
                    configPropertyName = CommonAttributes.FILE.getPropertyName();
                    // Resolve the file
                    modelStringValue = modelValue.get(PathResourceDefinition.PATH.getName()).asString();
                    if (modelValue.hasDefined(PathResourceDefinition.RELATIVE_TO.getName())) {
                        final String relativeTo = System.getProperty(modelValue.get(PathResourceDefinition.RELATIVE_TO.getName()).asString());
                        modelStringValue = relativeTo + File.separator + modelStringValue;
                    }
                } else if (modelPropertyName.equals(ConsoleHandlerResourceDefinition.TARGET.getName())) {
                    configPropertyName = ConsoleHandlerResourceDefinition.TARGET.getPropertyName();
                    modelStringValue = Target.fromString(modelValue.asString()).name();
                } else if (modelPropertyName.equals(AsyncHandlerResourceDefinition.SUBHANDLERS.getName())) {
                    final List<String> handlerHandlerNames = handlerConfig.getHandlerNames();
                    final ModelNode handlers = handlerModel.get(modelPropertyName);
                    if (handlers.isDefined()) {
                        final List<String> modelHandlerNames = new ArrayList<>();
                        for (ModelNode handler : handlers.asList()) {
                            modelHandlerNames.add(handler.asString());
                        }
                        final List<String> missingConfigHandlers = new ArrayList<>(handlerHandlerNames);
                        missingConfigHandlers.removeAll(modelHandlerNames);
                        final List<String> missingModelHandlers = new ArrayList<>(modelHandlerNames);
                        missingModelHandlers.removeAll(handlerHandlerNames);
                        Assert.assertTrue("Logger in model contains handlers not in the configuration: " + missingConfigHandlers, missingConfigHandlers.isEmpty());
                        Assert.assertTrue("Logger in configuration contains handlers not in the model: " + missingModelHandlers, missingModelHandlers.isEmpty());
                    } else {
                        Assert.assertTrue("Handlers attached to loggers in the configuration that are not attached to loggers in the model. Logger: " + name, handlerHandlerNames.isEmpty());
                    }
                    continue;
                } else if (modelPropertyName.equals(SyslogHandlerResourceDefinition.FACILITY.getName())) {
                    configPropertyName = modelPropertyName;
                    modelStringValue = FacilityAttribute.fromString(modelValue.asString()).getFacility().name();
                } else if (modelPropertyName.equals(SyslogHandlerResourceDefinition.SERVER_ADDRESS.getName())) {
                    configPropertyName = SyslogHandlerResourceDefinition.SERVER_ADDRESS.getPropertyName();
                } else if (modelPropertyName.equals(SyslogHandlerResourceDefinition.SYSLOG_FORMATTER.getName())) {
                    configPropertyName = SyslogHandlerResourceDefinition.SYSLOG_FORMATTER.getPropertyName();
                } else {
                    configPropertyName = convertModelPropertyName(modelPropertyName);
                }
                Assert.assertTrue("Configuration is missing property name: " + modelPropertyName, configPropertyNames.contains(configPropertyName));
                configValue = handlerConfig.getPropertyValueString(configPropertyName);
            }
            if (configValue == null) {
                Assert.assertFalse(String.format("Handler property values do not match.%nConfig Value: %s%nModel Value:  %s", configValue, modelValue), modelValue.isDefined());
            } else {
                Assert.assertEquals(String.format("Handler property values do not match.%nConfig Value: %s%nModel Value:  %s", configValue, modelStringValue), configValue, modelStringValue);
            }
        }
    }
}
Also used : HandlerConfiguration(org.jboss.logmanager.config.HandlerConfiguration) FormatterConfiguration(org.jboss.logmanager.config.FormatterConfiguration) ArrayList(java.util.ArrayList) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property) HashSet(java.util.HashSet)

Example 2 with HandlerConfiguration

use of org.jboss.logmanager.config.HandlerConfiguration in project wildfly-core by wildfly.

the class HandlerOperationsTestCase method testAddHandlerComposite.

@Test
public void testAddHandlerComposite() {
    final ModelNode handlerAddress = createFileHandlerAddress("FILE").toModelNode();
    final String filename = "test-file-2.log";
    final CompositeOperationBuilder builder = CompositeOperationBuilder.create();
    // Add the handler
    builder.addStep(OperationBuilder.createAddOperation(handlerAddress).addAttribute(CommonAttributes.FILE, createFileValue("jboss.server.log.dir", filename)).build());
    // Create a formatter and add it
    final ModelNode patternFormatterAddress = createPatternFormatterAddress("PATTERN").toModelNode();
    builder.addStep(OperationBuilder.createAddOperation(patternFormatterAddress).addAttribute(PatternFormatterResourceDefinition.PATTERN, "%d{HH:mm:ss,SSS} %-5p [%c] %s%e%n").build());
    // Write the named-formatter
    builder.addStep(SubsystemOperations.createWriteAttributeOperation(handlerAddress, "named-formatter", "PATTERN"));
    // Create an async-handler
    final ModelNode asyncHandlerAddress = createAsyncHandlerAddress(null, "ASYNC").toModelNode();
    builder.addStep(OperationBuilder.createAddOperation(asyncHandlerAddress).addAttribute(AsyncHandlerResourceDefinition.QUEUE_LENGTH, 100).build());
    // Add the file-handler to the async-handler
    ModelNode addHandlerOp = SubsystemOperations.createOperation("add-handler", asyncHandlerAddress);
    addHandlerOp.get("name").set("FILE");
    builder.addStep(addHandlerOp);
    // Create a logger
    final ModelNode loggerAddress = createLoggerAddress("org.jboss.as.logging").toModelNode();
    builder.addStep(SubsystemOperations.createAddOperation(loggerAddress));
    // Use the add-handler operation to add the handler to the logger
    addHandlerOp = SubsystemOperations.createOperation("add-handler", loggerAddress);
    addHandlerOp.get("name").set("ASYNC");
    builder.addStep(addHandlerOp);
    executeOperation(kernelServices, builder.build().getOperation());
    // Get the log context configuration to validate what has been configured
    final LogContextConfiguration configuration = ConfigurationPersistence.getConfigurationPersistence(LogContext.getLogContext());
    assertNotNull("Expected to find the configuration", configuration);
    final HandlerConfiguration handlerConfiguration = configuration.getHandlerConfiguration("FILE");
    assertNotNull("Expected to find the configuration for the FILE handler", configuration);
    assertEquals("Expected the handler named FILE to use the PATTERN formatter", "PATTERN", handlerConfiguration.getFormatterName());
    final LoggerConfiguration loggerConfiguration = configuration.getLoggerConfiguration("org.jboss.as.logging");
    assertNotNull("Expected the logger configuration for org.jboss.as.logging to exist", loggerConfiguration);
    assertTrue("Expected the FILE handler to be assigned", loggerConfiguration.getHandlerNames().contains("ASYNC"));
}
Also used : LogContextConfiguration(org.jboss.logmanager.config.LogContextConfiguration) LoggerConfiguration(org.jboss.logmanager.config.LoggerConfiguration) HandlerConfiguration(org.jboss.logmanager.config.HandlerConfiguration) CompositeOperationBuilder(org.jboss.as.controller.client.helpers.Operations.CompositeOperationBuilder) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Example 3 with HandlerConfiguration

use of org.jboss.logmanager.config.HandlerConfiguration in project activemq-artemis by rh-messaging.

the class LoggingConfigurationUpdater method configureHandler.

private boolean configureHandler(final Properties properties, final String handlerName) {
    final String className = getStringProperty(properties, getKey(HANDLER, handlerName));
    if (className == null) {
        printError("Handler %s is not defined%n", handlerName);
        return false;
    }
    final HandlerConfiguration configuration;
    if (config.getHandlerNames().contains(handlerName)) {
        configuration = config.getHandlerConfiguration(handlerName);
    } else {
        configuration = config.addHandlerConfiguration(getStringProperty(properties, getKey(HANDLER, handlerName, MODULE)), className, handlerName, getStringCsvArray(properties, getKey(HANDLER, handlerName, CONSTRUCTOR_PROPERTIES)));
    }
    final String filter = getStringProperty(properties, getKey(HANDLER, handlerName, FILTER));
    if (notEqual(filter, configuration.getFilterValueExpression())) {
        configuration.setFilter(filter);
        final String resolvedFilter = configuration.getFilterValueExpression().getResolvedValue();
        if (resolvedFilter != null) {
            // Check for a filter class
            final String filterClassName = getStringProperty(properties, getKey(FILTER, resolvedFilter));
            // If the filter class is null, assume it's a filter expression
            if (filterClassName != null) {
                configureFilter(properties, resolvedFilter);
            }
        }
    }
    final String levelName = getStringProperty(properties, getKey(HANDLER, handlerName, LEVEL));
    if (notEqual(levelName, configuration.getLevelValueExpression())) {
        configuration.setLevel(levelName == null ? "ALL" : levelName);
    }
    final String formatterName = getStringProperty(properties, getKey(HANDLER, handlerName, FORMATTER));
    if (formatterName != null) {
        if (getStringProperty(properties, getKey(FORMATTER, ValueExpression.STRING_RESOLVER.resolve(formatterName).getResolvedValue())) == null) {
            printError("Formatter %s is not defined%n", formatterName);
        } else {
            final ValueExpression<String> newValue = ValueExpression.STRING_RESOLVER.resolve(formatterName);
            if (notEqual(newValue, configuration.getFormatterNameValueExpression())) {
                if (configureFormatter(properties, newValue.getResolvedValue())) {
                    configuration.setFormatterName(formatterName);
                }
            }
        }
    }
    final String encoding = getStringProperty(properties, getKey(HANDLER, handlerName, ENCODING));
    if (notEqual(encoding, configuration.getEncodingValueExpression())) {
        configuration.setEncoding(encoding);
    }
    final String errorManagerName = getStringProperty(properties, getKey(HANDLER, handlerName, ERROR_MANAGER));
    if (errorManagerName != null) {
        if (getStringProperty(properties, getKey(ERROR_MANAGER, ValueExpression.STRING_RESOLVER.resolve(errorManagerName).getResolvedValue())) == null) {
            printError("Error manager %s is not defined%n", errorManagerName);
        } else {
            final ValueExpression<String> newValue = ValueExpression.STRING_RESOLVER.resolve(errorManagerName);
            if (notEqual(newValue, configuration.getErrorManagerNameValueExpression())) {
                if (configureErrorManager(properties, newValue.getResolvedValue())) {
                    configuration.setErrorManagerName(errorManagerName);
                }
            }
        }
    }
    configureHandlerNames(properties, configuration, HANDLER, handlerName);
    final String[] postConfigurationMethods = getStringCsvArray(properties, getKey(HANDLER, handlerName, POST_CONFIGURATION));
    configuration.setPostConfigurationMethods(postConfigurationMethods);
    configureProperties(properties, configuration, getKey(HANDLER, handlerName));
    return true;
}
Also used : HandlerConfiguration(org.jboss.logmanager.config.HandlerConfiguration)

Example 4 with HandlerConfiguration

use of org.jboss.logmanager.config.HandlerConfiguration in project activemq-artemis by apache.

the class LoggingConfigurationUpdater method configureHandler.

private boolean configureHandler(final Properties properties, final String handlerName) {
    final String className = getStringProperty(properties, getKey(HANDLER, handlerName));
    if (className == null) {
        printError("Handler %s is not defined%n", handlerName);
        return false;
    }
    final HandlerConfiguration configuration;
    if (config.getHandlerNames().contains(handlerName)) {
        configuration = config.getHandlerConfiguration(handlerName);
    } else {
        configuration = config.addHandlerConfiguration(getStringProperty(properties, getKey(HANDLER, handlerName, MODULE)), className, handlerName, getStringCsvArray(properties, getKey(HANDLER, handlerName, CONSTRUCTOR_PROPERTIES)));
    }
    final String filter = getStringProperty(properties, getKey(HANDLER, handlerName, FILTER));
    if (notEqual(filter, configuration.getFilterValueExpression())) {
        configuration.setFilter(filter);
        final String resolvedFilter = configuration.getFilterValueExpression().getResolvedValue();
        if (resolvedFilter != null) {
            // Check for a filter class
            final String filterClassName = getStringProperty(properties, getKey(FILTER, resolvedFilter));
            // If the filter class is null, assume it's a filter expression
            if (filterClassName != null) {
                configureFilter(properties, resolvedFilter);
            }
        }
    }
    final String levelName = getStringProperty(properties, getKey(HANDLER, handlerName, LEVEL));
    if (notEqual(levelName, configuration.getLevelValueExpression())) {
        configuration.setLevel(levelName == null ? "ALL" : levelName);
    }
    final String formatterName = getStringProperty(properties, getKey(HANDLER, handlerName, FORMATTER));
    if (formatterName != null) {
        if (getStringProperty(properties, getKey(FORMATTER, ValueExpression.STRING_RESOLVER.resolve(formatterName).getResolvedValue())) == null) {
            printError("Formatter %s is not defined%n", formatterName);
        } else {
            final ValueExpression<String> newValue = ValueExpression.STRING_RESOLVER.resolve(formatterName);
            if (notEqual(newValue, configuration.getFormatterNameValueExpression())) {
                if (configureFormatter(properties, newValue.getResolvedValue())) {
                    configuration.setFormatterName(formatterName);
                }
            }
        }
    }
    final String encoding = getStringProperty(properties, getKey(HANDLER, handlerName, ENCODING));
    if (notEqual(encoding, configuration.getEncodingValueExpression())) {
        configuration.setEncoding(encoding);
    }
    final String errorManagerName = getStringProperty(properties, getKey(HANDLER, handlerName, ERROR_MANAGER));
    if (errorManagerName != null) {
        if (getStringProperty(properties, getKey(ERROR_MANAGER, ValueExpression.STRING_RESOLVER.resolve(errorManagerName).getResolvedValue())) == null) {
            printError("Error manager %s is not defined%n", errorManagerName);
        } else {
            final ValueExpression<String> newValue = ValueExpression.STRING_RESOLVER.resolve(errorManagerName);
            if (notEqual(newValue, configuration.getErrorManagerNameValueExpression())) {
                if (configureErrorManager(properties, newValue.getResolvedValue())) {
                    configuration.setErrorManagerName(errorManagerName);
                }
            }
        }
    }
    configureHandlerNames(properties, configuration, HANDLER, handlerName);
    final String[] postConfigurationMethods = getStringCsvArray(properties, getKey(HANDLER, handlerName, POST_CONFIGURATION));
    configuration.setPostConfigurationMethods(postConfigurationMethods);
    configureProperties(properties, configuration, getKey(HANDLER, handlerName));
    return true;
}
Also used : HandlerConfiguration(org.jboss.logmanager.config.HandlerConfiguration)

Example 5 with HandlerConfiguration

use of org.jboss.logmanager.config.HandlerConfiguration in project wildfly-core by wildfly.

the class HandlerOperationsTestCase method testCompositeOperations.

/**
 * Tests a composite operation of undefining a {@code formatter} attribute and defining a {@code named-formatter}
 * attribute in a composite operation. These two specific attributes have strange behavior. If the
 * {@code named-formatter} is defined it removes the formatter, named the same as the handler, which was created
 * as part of the {@code undefine-attribute} operation of the {@code formatter} attribute.
 */
@Test
public void testCompositeOperations() {
    final ModelNode address = createFileHandlerAddress("FILE").toModelNode();
    final String filename = "test-file.log";
    final String defaultFormatterName = PatternFormatterResourceDefinition.getDefaultFomatterName("FILE");
    // Add the handler
    ModelNode addOp = OperationBuilder.createAddOperation(address).addAttribute(CommonAttributes.FILE, createFileValue("jboss.server.log.dir", filename)).build();
    executeOperation(kernelServices, addOp);
    final ModelNode patternFormatterAddress = createPatternFormatterAddress("PATTERN").toModelNode();
    addOp = SubsystemOperations.createAddOperation(patternFormatterAddress);
    addOp.get(PatternFormatterResourceDefinition.PATTERN.getName()).set("%d{HH:mm:ss,SSS} %-5p [%c] %s%e%n");
    executeOperation(kernelServices, addOp);
    // Create a composite operation to undefine
    final Operation op = CompositeOperationBuilder.create().addStep(SubsystemOperations.createUndefineAttributeOperation(address, "formatter")).addStep(SubsystemOperations.createWriteAttributeOperation(address, "named-formatter", "PATTERN")).build();
    executeOperation(kernelServices, op.getOperation());
    // Get the log context configuration to validate what has been configured
    final LogContextConfiguration configuration = ConfigurationPersistence.getConfigurationPersistence(LogContext.getLogContext());
    assertNotNull("Expected to find the configuration", configuration);
    assertFalse("Expected the default formatter named " + defaultFormatterName + " to be removed for the handler FILE", configuration.getFormatterNames().contains(defaultFormatterName));
    final HandlerConfiguration handlerConfiguration = configuration.getHandlerConfiguration("FILE");
    assertNotNull("Expected to find the configuration for the FILE handler", configuration);
    assertEquals("Expected the handler named FILE to use the PATTERN formatter", "PATTERN", handlerConfiguration.getFormatterName());
    // Undefine the named-formatter to ensure a formatter is created
    executeOperation(kernelServices, SubsystemOperations.createUndefineAttributeOperation(address, "named-formatter"));
    assertTrue("Expected the default formatter named " + defaultFormatterName + " to be added", configuration.getFormatterNames().contains(defaultFormatterName));
    assertEquals("Expected the handler named FILE to use the FILE formatter", defaultFormatterName, handlerConfiguration.getFormatterName());
}
Also used : LogContextConfiguration(org.jboss.logmanager.config.LogContextConfiguration) HandlerConfiguration(org.jboss.logmanager.config.HandlerConfiguration) Operation(org.jboss.as.controller.client.Operation) Operations.createAddOperation(org.jboss.as.controller.client.helpers.Operations.createAddOperation) Operations.createRemoveOperation(org.jboss.as.controller.client.helpers.Operations.createRemoveOperation) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Aggregations

HandlerConfiguration (org.jboss.logmanager.config.HandlerConfiguration)7 ModelNode (org.jboss.dmr.ModelNode)3 LogContextConfiguration (org.jboss.logmanager.config.LogContextConfiguration)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Operation (org.jboss.as.controller.client.Operation)1 CompositeOperationBuilder (org.jboss.as.controller.client.helpers.Operations.CompositeOperationBuilder)1 Operations.createAddOperation (org.jboss.as.controller.client.helpers.Operations.createAddOperation)1 Operations.createRemoveOperation (org.jboss.as.controller.client.helpers.Operations.createRemoveOperation)1 LoggingLogger (org.jboss.as.logging.logging.LoggingLogger)1 Property (org.jboss.dmr.Property)1 Logger (org.jboss.logmanager.Logger)1 FormatterConfiguration (org.jboss.logmanager.config.FormatterConfiguration)1 LoggerConfiguration (org.jboss.logmanager.config.LoggerConfiguration)1