Search in sources :

Example 6 with PropertyConfigurator

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

the class LoggingConfigurationFileReloader method getOrCreateConfiguration.

private LogContextConfiguration getOrCreateConfiguration(final org.jboss.logmanager.Logger rootLogger) {
    Configurator configurator = rootLogger.getAttachment(Configurator.ATTACHMENT_KEY);
    if (configurator == null) {
        configurator = new PropertyConfigurator(rootLogger.getLogContext());
        final Configurator appearing = rootLogger.attachIfAbsent(Configurator.ATTACHMENT_KEY, configurator);
        if (appearing != null) {
            configurator = appearing;
        }
    }
    if (configurator instanceof PropertyConfigurator) {
        return ((PropertyConfigurator) configurator).getLogContextConfiguration();
    }
    if (configurator instanceof LogContextConfiguration) {
        return (LogContextConfiguration) configurator;
    }
    return null;
}
Also used : LogContextConfiguration(org.jboss.logmanager.config.LogContextConfiguration) Configurator(org.jboss.logmanager.Configurator) PropertyConfigurator(org.jboss.logmanager.PropertyConfigurator) PropertyConfigurator(org.jboss.logmanager.PropertyConfigurator)

Example 7 with PropertyConfigurator

use of org.jboss.logmanager.PropertyConfigurator 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 8 with PropertyConfigurator

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

the class BootableJar method configureLogContext.

private LogContext configureLogContext() throws IOException {
    // Create our own log context instead of using the default system log context. This is useful for cases when the
    // LogManager.readConfiguration() may be invoked it will not override the current configuration.
    final LogContext logContext = LogContext.create();
    final Path bootLog = environment.resolveLogDir(SERVER_LOG);
    final Path loggingProperties = environment.resolveConfigurationDir(LOGGING_PROPERTIES);
    if (Files.exists(loggingProperties)) {
        try (final InputStream in = Files.newInputStream(loggingProperties)) {
            environment.setSystemProperty(LOG_BOOT_FILE_PROP, bootLog.toAbsolutePath().toString());
            // The LogManager.readConfiguration() uses the LogContext.getSystemLogContext(). Since we create our
            // own LogContext we need to configure the context and attach the configurator to the root logger. The
            // logging subsystem will use this configurator to determine what resources may need to be reconfigured.
            PropertyConfigurator configurator = new PropertyConfigurator(logContext);
            configurator.configure(in);
            logContext.getLogger("").attach(Configurator.ATTACHMENT_KEY, configurator);
        }
    }
    return logContext;
}
Also used : Path(java.nio.file.Path) NullInputStream(org.jboss.stdio.NullInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) LogContext(org.jboss.logmanager.LogContext) PropertyConfigurator(org.jboss.logmanager.PropertyConfigurator)

Example 9 with PropertyConfigurator

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

the class CommandLineMain method configureLogManager.

private static void configureLogManager(final String[] args) {
    // If the property is already set, we don't want to replace it
    if (getSystemProperty("java.util.logging.manager") == null) {
        try {
            setSystemProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager");
            String logLevel = parseValue(getSystemProperty("jboss.cli.log.level"));
            for (String arg : args) {
                if (arg.startsWith("-Djboss.cli.log.level")) {
                    logLevel = parseValue(arg);
                } else if (arg.startsWith("-Dlogging.configuration")) {
                    setSystemProperty("logging.configuration", parseValue(arg));
                }
            }
            // The log level has not been set, no need to continue
            if (logLevel == null)
                return;
            // Configure the log manager
            final LogManager logManager = LogManager.getLogManager();
            if (logManager instanceof org.jboss.logmanager.LogManager) {
                // Attempt to configure based on defaults
                logManager.readConfiguration();
                // If configured a Configurator will be on the root logger
                if (LogContext.getSystemLogContext().getAttachment("", Configurator.ATTACHMENT_KEY) == null) {
                    if (!"OFF".equalsIgnoreCase(logLevel)) {
                        try {
                            final PropertyConfigurator configurator = new PropertyConfigurator();
                            // Get the root logger and attach the configurator, note we don't need to be concerned with security exceptions
                            // as the logManager.readConfiguration() will have already failed the check
                            final Configurator appearing = LogContext.getSystemLogContext().getLogger("").attachIfAbsent(Configurator.ATTACHMENT_KEY, configurator);
                            if (appearing == null) {
                                configurator.configure(createLogManagerConfig(logLevel));
                            }
                        } catch (IOException e) {
                            System.err.println("ERROR: Could not configure LogManager");
                            e.printStackTrace();
                        }
                    }
                }
            }
        } catch (SecurityException e) {
            System.err.println("ERROR: Could not configure LogManager");
            e.printStackTrace();
        } catch (Throwable ignored) {
        }
    }
}
Also used : Configurator(org.jboss.logmanager.Configurator) PropertyConfigurator(org.jboss.logmanager.PropertyConfigurator) IOException(java.io.IOException) LogManager(java.util.logging.LogManager) PropertyConfigurator(org.jboss.logmanager.PropertyConfigurator)

Aggregations

PropertyConfigurator (org.jboss.logmanager.PropertyConfigurator)9 Configurator (org.jboss.logmanager.Configurator)7 LogContext (org.jboss.logmanager.LogContext)5 LogContextConfiguration (org.jboss.logmanager.config.LogContextConfiguration)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Path (java.nio.file.Path)2 Logger (org.jboss.logmanager.Logger)2 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 List (java.util.List)1 Properties (java.util.Properties)1 Handler (java.util.logging.Handler)1 LogManager (java.util.logging.LogManager)1 DOMConfigurator (org.apache.log4j.xml.DOMConfigurator)1 LoggingLogger (org.jboss.as.logging.logging.LoggingLogger)1 DeploymentResourceSupport (org.jboss.as.server.deployment.DeploymentResourceSupport)1 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)1 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)1 Module (org.jboss.modules.Module)1