Search in sources :

Example 1 with LoggerConfiguration

use of org.jboss.logmanager.config.LoggerConfiguration 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 2 with LoggerConfiguration

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

the class LoggingConfigurationUpdater method configureLogger.

private List<String> configureLogger(final Properties properties, final String loggerName) {
    final LoggerConfiguration loggerConfiguration;
    if (config.getLoggerNames().contains(loggerName)) {
        loggerConfiguration = config.getLoggerConfiguration(loggerName);
    } else {
        loggerConfiguration = config.addLoggerConfiguration(loggerName);
    }
    // Get logger level
    final String levelName = getStringProperty(properties, getKey(LOGGER, loggerName, LEVEL));
    if (notEqual(levelName, loggerConfiguration.getLevelValueExpression())) {
        loggerConfiguration.setLevel(levelName == null ? "ALL" : levelName);
    }
    // Get logger filter
    final String filterName = getStringProperty(properties, getKey(LOGGER, loggerName, FILTER));
    final ValueExpression<String> newValue = ValueExpression.STRING_RESOLVER.resolve(filterName);
    if (notEqual(newValue, loggerConfiguration.getFilterValueExpression())) {
        loggerConfiguration.setFilter(filterName);
        final String resolvedFilter = loggerConfiguration.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);
            }
        }
    }
    // Get logger handlers
    configureHandlerNames(properties, loggerConfiguration, LOGGER, loggerName);
    // Get logger properties
    final String useParentHandlersString = getStringProperty(properties, getKey(LOGGER, loggerName, USE_PARENT_HANDLERS));
    if (booleanNotEqual(useParentHandlersString, loggerConfiguration.getUseParentHandlersValueExpression())) {
        // Check for expression
        if (EXPRESSION_PATTERN.matcher(useParentHandlersString).matches()) {
            loggerConfiguration.setUseParentHandlers(useParentHandlersString);
        } else {
            loggerConfiguration.setUseParentHandlers(Boolean.parseBoolean(useParentHandlersString));
        }
    }
    return loggerConfiguration.getHandlerNames();
}
Also used : LoggerConfiguration(org.jboss.logmanager.config.LoggerConfiguration)

Example 3 with LoggerConfiguration

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

the class LoggingConfigurationUpdater method configureLogger.

private List<String> configureLogger(final Properties properties, final String loggerName) {
    final LoggerConfiguration loggerConfiguration;
    if (config.getLoggerNames().contains(loggerName)) {
        loggerConfiguration = config.getLoggerConfiguration(loggerName);
    } else {
        loggerConfiguration = config.addLoggerConfiguration(loggerName);
    }
    // Get logger level
    final String levelName = getStringProperty(properties, getKey(LOGGER, loggerName, LEVEL));
    if (notEqual(levelName, loggerConfiguration.getLevelValueExpression())) {
        loggerConfiguration.setLevel(levelName == null ? "ALL" : levelName);
    }
    // Get logger filter
    final String filterName = getStringProperty(properties, getKey(LOGGER, loggerName, FILTER));
    final ValueExpression<String> newValue = ValueExpression.STRING_RESOLVER.resolve(filterName);
    if (notEqual(newValue, loggerConfiguration.getFilterValueExpression())) {
        loggerConfiguration.setFilter(filterName);
        final String resolvedFilter = loggerConfiguration.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);
            }
        }
    }
    // Get logger handlers
    configureHandlerNames(properties, loggerConfiguration, LOGGER, loggerName);
    // Get logger properties
    final String useParentHandlersString = getStringProperty(properties, getKey(LOGGER, loggerName, USE_PARENT_HANDLERS));
    if (booleanNotEqual(useParentHandlersString, loggerConfiguration.getUseParentHandlersValueExpression())) {
        // Check for expression
        if (EXPRESSION_PATTERN.matcher(useParentHandlersString).matches()) {
            loggerConfiguration.setUseParentHandlers(useParentHandlersString);
        } else {
            loggerConfiguration.setUseParentHandlers(Boolean.parseBoolean(useParentHandlersString));
        }
    }
    return loggerConfiguration.getHandlerNames();
}
Also used : LoggerConfiguration(org.jboss.logmanager.config.LoggerConfiguration)

Example 4 with LoggerConfiguration

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

the class AbstractLoggingSubsystemTest method compareLoggers.

private void compareLoggers(final LogContextConfiguration logContextConfiguration, final ModelNode model) {
    final List<String> loggerNames = logContextConfiguration.getLoggerNames();
    for (String name : loggerNames) {
        final LoggerConfiguration loggerConfig = logContextConfiguration.getLoggerConfiguration(name);
        final ModelNode loggerModel = (name.isEmpty() ? model.get(RootLoggerResourceDefinition.NAME, RootLoggerResourceDefinition.RESOURCE_NAME) : model.get(LoggerResourceDefinition.NAME, name));
        // Logger could be empty
        if (loggerModel.isDefined()) {
            final Set<String> attributes = loggerModel.keys();
            attributes.remove(LoggerResourceDefinition.CATEGORY.getName());
            attributes.remove(CommonAttributes.FILTER.getName());
            attributes.remove(CommonAttributes.NAME.getName());
            for (String attribute : attributes) {
                if (attribute.equals(CommonAttributes.LEVEL.getName())) {
                    final String configValue = loggerConfig.getLevel();
                    final String modelValue = loggerModel.get(attribute).asString();
                    Assert.assertEquals(String.format("Levels do not match. Config Value: %s  Model Value: %s", configValue, modelValue), configValue, modelValue);
                } else if (attribute.equals(LoggerAttributes.FILTER_SPEC.getName())) {
                    final String configValue = loggerConfig.getFilter();
                    final String modelValue = loggerModel.hasDefined(attribute) ? loggerModel.get(attribute).asString() : null;
                    Assert.assertEquals(String.format("Filter expressions do not match. Config Value: %s  Model Value: %s", configValue, modelValue), configValue, modelValue);
                } else if (attribute.equals(LoggerAttributes.HANDLERS.getName())) {
                    final List<String> handlerNames = loggerConfig.getHandlerNames();
                    final ModelNode handlers = loggerModel.get(attribute);
                    if (handlers.isDefined()) {
                        final List<String> modelHandlerNames = new ArrayList<>();
                        for (ModelNode handler : handlers.asList()) {
                            modelHandlerNames.add(handler.asString());
                        }
                        final List<String> missingConfigHandlers = new ArrayList<>(handlerNames);
                        missingConfigHandlers.removeAll(modelHandlerNames);
                        final List<String> missingModelHandlers = new ArrayList<>(modelHandlerNames);
                        missingModelHandlers.removeAll(handlerNames);
                        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, handlerNames.isEmpty());
                    }
                } else if (attribute.equals(LoggerResourceDefinition.USE_PARENT_HANDLERS.getName())) {
                    final Boolean configValue = loggerConfig.getUseParentHandlers();
                    final Boolean modelValue = loggerModel.get(attribute).asBoolean();
                    Assert.assertEquals(String.format("Use parent handler attributes do not match. Config Value: %s  Model Value: %s", configValue, modelValue), configValue, modelValue);
                } else {
                    // Invalid
                    Assert.fail("Invalid attribute: " + attribute);
                }
            }
        }
    }
}
Also used : LoggerConfiguration(org.jboss.logmanager.config.LoggerConfiguration) ArrayList(java.util.ArrayList) ModelNode(org.jboss.dmr.ModelNode)

Aggregations

LoggerConfiguration (org.jboss.logmanager.config.LoggerConfiguration)4 ModelNode (org.jboss.dmr.ModelNode)2 ArrayList (java.util.ArrayList)1 CompositeOperationBuilder (org.jboss.as.controller.client.helpers.Operations.CompositeOperationBuilder)1 HandlerConfiguration (org.jboss.logmanager.config.HandlerConfiguration)1 LogContextConfiguration (org.jboss.logmanager.config.LogContextConfiguration)1 Test (org.junit.Test)1