Search in sources :

Example 11 with LogContextConfiguration

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

the class LoggingSubsystemAdd method performRuntime.

@Override
protected void performRuntime(final OperationContext context, final ModelNode operation, final ModelNode model) throws OperationFailedException {
    final boolean addDependencies = LoggingResourceDefinition.ADD_LOGGING_API_DEPENDENCIES.resolveModelAttribute(context, model).asBoolean();
    final boolean useLoggingConfig = LoggingResourceDefinition.USE_DEPLOYMENT_LOGGING_CONFIG.resolveModelAttribute(context, model).asBoolean();
    context.addStep(new AbstractDeploymentChainStep() {

        @Override
        protected void execute(final DeploymentProcessorTarget processorTarget) {
            if (addDependencies) {
                processorTarget.addDeploymentProcessor(LoggingExtension.SUBSYSTEM_NAME, Phase.DEPENDENCIES, Phase.DEPENDENCIES_LOGGING, new LoggingDependencyDeploymentProcessor());
            }
            processorTarget.addDeploymentProcessor(LoggingExtension.SUBSYSTEM_NAME, Phase.POST_MODULE, Phase.POST_MODULE_LOGGING_CONFIG, new LoggingConfigDeploymentProcessor(contextSelector, LoggingResourceDefinition.USE_DEPLOYMENT_LOGGING_CONFIG.getName(), useLoggingConfig));
            processorTarget.addDeploymentProcessor(LoggingExtension.SUBSYSTEM_NAME, Phase.POST_MODULE, Phase.POST_MODULE_LOGGING_PROFILE, new LoggingProfileDeploymentProcessor(contextSelector));
            processorTarget.addDeploymentProcessor(LoggingExtension.SUBSYSTEM_NAME, Phase.INSTALL, Phase.INSTALL_LOGGING_DEPLOYMENT_RESOURCES, new LoggingDeploymentResourceProcessor());
        }
    }, Stage.RUNTIME);
    final Resource resource = context.readResource(PathAddress.EMPTY_ADDRESS);
    final ConfigurationPersistence configurationPersistence = ConfigurationPersistence.getOrCreateConfigurationPersistence();
    final LogContextConfiguration logContextConfiguration = configurationPersistence.getLogContextConfiguration();
    // root logger
    if (!resource.hasChild(RootLoggerResourceDefinition.ROOT_LOGGER_PATH)) {
        LoggingLogger.ROOT_LOGGER.tracef("Removing the root logger configuration.");
        logContextConfiguration.removeLoggerConfiguration(CommonAttributes.ROOT_LOGGER_NAME);
    }
    // remove all configured loggers which aren't in the model
    if (resource.hasChild(PathElement.pathElement(LoggerResourceDefinition.NAME))) {
        final Set<String> loggerNames = resource.getChildrenNames(LoggerResourceDefinition.NAME);
        final List<String> configuredLoggerNames = logContextConfiguration.getLoggerNames();
        // Always remove the root
        configuredLoggerNames.remove(CommonAttributes.ROOT_LOGGER_NAME);
        configuredLoggerNames.removeAll(loggerNames);
        for (String name : configuredLoggerNames) {
            LoggingLogger.ROOT_LOGGER.tracef("Removing logger configuration for '%s'", name);
            logContextConfiguration.removeLoggerConfiguration(name);
        }
    }
    // Create a collection of all subsystem handlers
    final Collection<String> subsystemHandlers = new ArrayList<>();
    subsystemHandlers.addAll(resource.getChildrenNames(AsyncHandlerResourceDefinition.NAME));
    subsystemHandlers.addAll(resource.getChildrenNames(ConsoleHandlerResourceDefinition.NAME));
    subsystemHandlers.addAll(resource.getChildrenNames(CustomHandlerResourceDefinition.NAME));
    subsystemHandlers.addAll(resource.getChildrenNames(FileHandlerResourceDefinition.NAME));
    subsystemHandlers.addAll(resource.getChildrenNames(PeriodicHandlerResourceDefinition.NAME));
    subsystemHandlers.addAll(resource.getChildrenNames(PeriodicSizeRotatingHandlerResourceDefinition.NAME));
    subsystemHandlers.addAll(resource.getChildrenNames(SizeRotatingHandlerResourceDefinition.NAME));
    subsystemHandlers.addAll(resource.getChildrenNames(SocketHandlerResourceDefinition.NAME));
    subsystemHandlers.addAll(resource.getChildrenNames(SyslogHandlerResourceDefinition.NAME));
    // handlers
    final List<String> configuredHandlerNames = logContextConfiguration.getHandlerNames();
    configuredHandlerNames.removeAll(subsystemHandlers);
    for (String name : configuredHandlerNames) {
        LoggingLogger.ROOT_LOGGER.tracef("Removing handler configuration for '%s'", name);
        // Clean up any possible POJO references
        logContextConfiguration.removePojoConfiguration(name);
        // Remove the handler configuration
        logContextConfiguration.removeHandlerConfiguration(name);
    }
    // Remove formatters
    final List<String> configuredFormatters = logContextConfiguration.getFormatterNames();
    configuredFormatters.removeAll(resource.getChildrenNames(PatternFormatterResourceDefinition.NAME));
    configuredFormatters.removeAll(resource.getChildrenNames(CustomFormatterResourceDefinition.NAME));
    configuredFormatters.removeAll(resource.getChildrenNames(JsonFormatterResourceDefinition.NAME));
    // Formatter names could also be the name of a handler if the formatter attribute is used rather than a named-formatter
    configuredFormatters.removeAll(subsystemHandlers);
    for (String name : configuredFormatters) {
        LoggingLogger.ROOT_LOGGER.tracef("Removing formatter configuration for '%s'", name);
        // Remove the formatter configuration
        logContextConfiguration.removeFormatterConfiguration(name);
    }
    LoggingOperations.addCommitStep(context, configurationPersistence);
    LoggingLogger.ROOT_LOGGER.trace("Logging subsystem has been added.");
}
Also used : LoggingProfileDeploymentProcessor(org.jboss.as.logging.deployments.LoggingProfileDeploymentProcessor) ConfigurationPersistence(org.jboss.as.logging.logmanager.ConfigurationPersistence) LoggingDeploymentResourceProcessor(org.jboss.as.logging.deployments.LoggingDeploymentResourceProcessor) LoggingConfigDeploymentProcessor(org.jboss.as.logging.deployments.LoggingConfigDeploymentProcessor) Resource(org.jboss.as.controller.registry.Resource) ArrayList(java.util.ArrayList) LoggingDependencyDeploymentProcessor(org.jboss.as.logging.deployments.LoggingDependencyDeploymentProcessor) LogContextConfiguration(org.jboss.logmanager.config.LogContextConfiguration) DeploymentProcessorTarget(org.jboss.as.server.DeploymentProcessorTarget) AbstractDeploymentChainStep(org.jboss.as.server.AbstractDeploymentChainStep)

Example 12 with LogContextConfiguration

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

the class LoggingConfigurationReadStepHandler method execute.

@Override
public void execute(final OperationContext context, final ModelNode operation) {
    LogContextConfiguration configuration = null;
    // Lookup the service
    final ServiceController<?> controller = context.getServiceRegistry(false).getService(getServiceName(context));
    if (controller != null) {
        configuration = (LogContextConfiguration) controller.getValue();
    }
    // Some deployments may not have a logging configuration associated, e.g. log4j configured deployments
    if (configuration != null) {
        // Attempt to resolve the resource to ensure it exists before continuing, this can be removed when
        // WFCORE-1800 is resolved
        context.readResource(PathAddress.EMPTY_ADDRESS, false);
        final String name = context.getCurrentAddressValue();
        final ModelNode result = context.getResult();
        updateModel(configuration, name, result);
    }
}
Also used : LogContextConfiguration(org.jboss.logmanager.config.LogContextConfiguration) ModelNode(org.jboss.dmr.ModelNode)

Example 13 with LogContextConfiguration

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

the class EmbeddedLogContext method clearLogContext.

/**
 * Attempts to clear the global log context used for embedded servers.
 */
static synchronized void clearLogContext() {
    final LogContext embeddedLogContext = Holder.LOG_CONTEXT;
    // Remove the configurator and clear the log context
    final Configurator configurator = embeddedLogContext.getLogger("").detach(Configurator.ATTACHMENT_KEY);
    // If this was a PropertyConfigurator we can use the LogContextConfiguration API to tear down the LogContext
    if (configurator instanceof PropertyConfigurator) {
        final LogContextConfiguration logContextConfiguration = ((PropertyConfigurator) configurator).getLogContextConfiguration();
        clearLogContext(logContextConfiguration);
    } else if (configurator instanceof LogContextConfiguration) {
        clearLogContext((LogContextConfiguration) configurator);
    } else {
        // Remove all the handlers and close them as well as reset the loggers
        final List<String> loggerNames = Collections.list(embeddedLogContext.getLoggerNames());
        for (String name : loggerNames) {
            final Logger logger = embeddedLogContext.getLoggerIfExists(name);
            if (logger != null) {
                final Handler[] handlers = logger.clearHandlers();
                if (handlers != null) {
                    for (Handler handler : handlers) {
                        handler.close();
                    }
                }
                logger.setFilter(null);
                logger.setUseParentFilters(false);
                logger.setUseParentHandlers(true);
                logger.setLevel(Level.INFO);
            }
        }
    }
}
Also used : LogContextConfiguration(org.jboss.logmanager.config.LogContextConfiguration) PropertyConfigurator(org.jboss.logmanager.PropertyConfigurator) Configurator(org.jboss.logmanager.Configurator) LogContext(org.jboss.logmanager.LogContext) Handler(java.util.logging.Handler) List(java.util.List) Logger(org.jboss.logmanager.Logger) PropertyConfigurator(org.jboss.logmanager.PropertyConfigurator)

Example 14 with LogContextConfiguration

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

the class LoggingConfigurationFileReloader method getOrCreateUpdater.

private LoggingConfigurationUpdater getOrCreateUpdater() {
    final LogContext logContext = LogContext.getLogContext();
    final org.jboss.logmanager.Logger rootLogger = logContext.getLogger("");
    LoggingConfigurationUpdater updater = rootLogger.getAttachment(KEY);
    if (updater == null) {
        final LogContextConfiguration logContextConfiguration = getOrCreateConfiguration(rootLogger);
        if (logContextConfiguration == null) {
            return null;
        }
        updater = new LoggingConfigurationUpdater(logContextConfiguration);
        final LoggingConfigurationUpdater appearing = rootLogger.attachIfAbsent(KEY, updater);
        if (appearing != null) {
            updater = appearing;
        }
    }
    return updater;
}
Also used : LogContextConfiguration(org.jboss.logmanager.config.LogContextConfiguration) LogContext(org.jboss.logmanager.LogContext)

Aggregations

LogContextConfiguration (org.jboss.logmanager.config.LogContextConfiguration)14 Configurator (org.jboss.logmanager.Configurator)4 LogContext (org.jboss.logmanager.LogContext)4 PropertyConfigurator (org.jboss.logmanager.PropertyConfigurator)4 ModelNode (org.jboss.dmr.ModelNode)3 ArrayList (java.util.ArrayList)2 HandlerConfiguration (org.jboss.logmanager.config.HandlerConfiguration)2 Test (org.junit.Test)2 List (java.util.List)1 Handler (java.util.logging.Handler)1 PathElement (org.jboss.as.controller.PathElement)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 Resource (org.jboss.as.controller.registry.Resource)1 LoggingConfigDeploymentProcessor (org.jboss.as.logging.deployments.LoggingConfigDeploymentProcessor)1 LoggingDependencyDeploymentProcessor (org.jboss.as.logging.deployments.LoggingDependencyDeploymentProcessor)1 LoggingDeploymentResourceProcessor (org.jboss.as.logging.deployments.LoggingDeploymentResourceProcessor)1 LoggingProfileDeploymentProcessor (org.jboss.as.logging.deployments.LoggingProfileDeploymentProcessor)1