Search in sources :

Example 6 with Configurator

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

use of org.jboss.logmanager.Configurator 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

Configurator (org.jboss.logmanager.Configurator)7 PropertyConfigurator (org.jboss.logmanager.PropertyConfigurator)7 LogContextConfiguration (org.jboss.logmanager.config.LogContextConfiguration)4 LogContext (org.jboss.logmanager.LogContext)3 IOException (java.io.IOException)2 Logger (org.jboss.logmanager.Logger)2 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 List (java.util.List)1 Handler (java.util.logging.Handler)1 LogManager (java.util.logging.LogManager)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 Module (org.jboss.modules.Module)1