use of org.jboss.logmanager.PropertyConfigurator in project wildfly-core by wildfly.
the class EmbeddedLogContext method configureLogContext.
/**
* Configures the log context for the server and returns the configured log context.
*
* @param logDir the logging directory, from jboss.server|domain.log.dir standalone default {@code $JBOSS_HOME/standalone/log}
* @param configDir the configuration directory from jboss.server|domain.config.dir, standalone default {@code $JBOSS_HOME/standalone/configuration}
* @param defaultLogFileName the name of the log file to pass to {@code org.jboss.boot.log.file}
* @param ctx the command context used to report errors to
*
* @return the configured log context
*/
static synchronized LogContext configureLogContext(final File logDir, final File configDir, final String defaultLogFileName, final CommandContext ctx) {
final LogContext embeddedLogContext = Holder.LOG_CONTEXT;
final Path bootLog = logDir.toPath().resolve(Paths.get(defaultLogFileName));
final Path loggingProperties = configDir.toPath().resolve(Paths.get("logging.properties"));
if (Files.exists(loggingProperties)) {
WildFlySecurityManager.setPropertyPrivileged("org.jboss.boot.log.file", bootLog.toAbsolutePath().toString());
try (final InputStream in = Files.newInputStream(loggingProperties)) {
// Attempt to get the configurator from the root logger
Configurator configurator = embeddedLogContext.getAttachment("", Configurator.ATTACHMENT_KEY);
if (configurator == null) {
configurator = new PropertyConfigurator(embeddedLogContext);
final Configurator existing = embeddedLogContext.getLogger("").attachIfAbsent(Configurator.ATTACHMENT_KEY, configurator);
if (existing != null) {
configurator = existing;
}
}
configurator.configure(in);
} catch (IOException e) {
ctx.printLine(String.format("Unable to configure logging from configuration file %s. Reason: %s", loggingProperties, e.getLocalizedMessage()));
}
}
return embeddedLogContext;
}
use of org.jboss.logmanager.PropertyConfigurator in project wildfly-core by wildfly.
the class LoggingConfigDeploymentProcessor method configure.
/**
* Configures the log context.
*
* @param configFile the configuration file
* @param classLoader the class loader to use for the configuration
* @param logContext the log context to configure
*
* @return {@code true} if the log context was successfully configured, otherwise {@code false}
*
* @throws DeploymentUnitProcessingException if the configuration fails
*/
private LoggingConfigurationService configure(final ResourceRoot root, final VirtualFile configFile, final ClassLoader classLoader, final LogContext logContext) throws DeploymentUnitProcessingException {
InputStream configStream = null;
try {
LoggingLogger.ROOT_LOGGER.debugf("Found logging configuration file: %s", configFile);
// Get the filname and open the stream
final String fileName = configFile.getName();
configStream = configFile.openStream();
// Check the type of the configuration file
if (isLog4jConfiguration(fileName)) {
LoggingLogger.ROOT_LOGGER.usageOfLog4j1Config(configFile.getPathName(), root.getRootName());
final ClassLoader current = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
final LogContext old = logContextSelector.setLocalContext(logContext);
try {
WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(classLoader);
if (LOG4J_XML.equals(fileName) || JBOSS_LOG4J_XML.equals(fileName)) {
new DOMConfigurator().doConfigure(configStream, org.apache.log4j.JBossLogManagerFacade.getLoggerRepository(logContext));
} else {
final Properties properties = new Properties();
properties.load(new InputStreamReader(configStream, ENCODING));
new org.apache.log4j.PropertyConfigurator().doConfigure(properties, org.apache.log4j.JBossLogManagerFacade.getLoggerRepository(logContext));
}
} finally {
logContextSelector.setLocalContext(old);
WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(current);
}
return new LoggingConfigurationService(null, resolveRelativePath(root, configFile));
} else {
// Create a properties file
final Properties properties = new Properties();
properties.load(new InputStreamReader(configStream, ENCODING));
// Attempt to see if this is a J.U.L. configuration file
if (isJulConfiguration(properties)) {
LoggingLogger.ROOT_LOGGER.julConfigurationFileFound(configFile.getName());
} else {
// Load non-log4j types
final PropertyConfigurator propertyConfigurator = new PropertyConfigurator(logContext);
propertyConfigurator.configure(properties);
return new LoggingConfigurationService(propertyConfigurator.getLogContextConfiguration(), resolveRelativePath(root, configFile));
}
}
} catch (Exception e) {
throw LoggingLogger.ROOT_LOGGER.failedToConfigureLogging(e, configFile.getName());
} finally {
safeClose(configStream);
}
return null;
}
use of org.jboss.logmanager.PropertyConfigurator in project wildfly-core by wildfly.
the class LoggingDeploymentResourceProcessor method deploy.
@Override
public final void deploy(final DeploymentPhaseContext phaseContext) {
final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
if (deploymentUnit.hasAttachment(Attachments.MODULE)) {
LoggingConfigurationService loggingConfigurationService;
if (deploymentUnit.hasAttachment(LOGGING_CONFIGURATION_SERVICE_KEY)) {
loggingConfigurationService = deploymentUnit.getAttachment(LOGGING_CONFIGURATION_SERVICE_KEY);
// Remove the attachment as it should no longer be needed
deploymentUnit.removeAttachment(LOGGING_CONFIGURATION_SERVICE_KEY);
} else {
// Get the module
final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
// Set the deployments class loader to ensure we get the correct log context
final ClassLoader current = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
try {
WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(module.getClassLoader());
LogContextConfiguration logContextConfiguration = null;
final LogContext logContext = LogContext.getLogContext();
final Configurator configurator = logContext.getAttachment(CommonAttributes.ROOT_LOGGER_NAME, Configurator.ATTACHMENT_KEY);
if (configurator instanceof LogContextConfiguration) {
logContextConfiguration = (LogContextConfiguration) configurator;
} else if (configurator instanceof PropertyConfigurator) {
logContextConfiguration = ((PropertyConfigurator) configurator).getLogContextConfiguration();
}
loggingConfigurationService = new LoggingConfigurationService(logContextConfiguration, "default");
} finally {
WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(current);
}
}
final DeploymentResourceSupport deploymentResourceSupport = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_RESOURCE_SUPPORT);
// Register the resources
LoggingDeploymentResources.registerDeploymentResource(deploymentResourceSupport, loggingConfigurationService);
phaseContext.getServiceTarget().addService(deploymentUnit.getServiceName().append("logging", "configuration"), loggingConfigurationService).install();
}
}
use of org.jboss.logmanager.PropertyConfigurator in project wildfly-core by wildfly.
the class ConfigurationPersistence method getOrCreateConfigurationPersistence.
/**
* Gets the property configurator. If the {@link ConfigurationPersistence} does not exist a new one is created.
*
* @param logContext the log context used to find the property configurator or to attach it to.
*
* @return the property configurator
*/
public static ConfigurationPersistence getOrCreateConfigurationPersistence(final LogContext logContext) {
final Logger root = logContext.getLogger(CommonAttributes.ROOT_LOGGER_NAME);
final ConfigurationPersistence result;
synchronized (LOCK) {
Configurator configurator = root.getAttachment(Configurator.ATTACHMENT_KEY);
if (configurator == null) {
configurator = new ConfigurationPersistence(logContext);
Configurator existing = root.attachIfAbsent(Configurator.ATTACHMENT_KEY, configurator);
if (existing != null) {
configurator = existing;
}
}
if (configurator instanceof ConfigurationPersistence) {
// We have the correct configurator
result = (ConfigurationPersistence) configurator;
} else if (configurator instanceof PropertyConfigurator) {
// Create a new configurator delegating to the configurator found
result = new ConfigurationPersistence((PropertyConfigurator) configurator);
root.attach(Configurator.ATTACHMENT_KEY, result);
} else {
// An unknown configurator, log a warning and replace
LoggingLogger.ROOT_LOGGER.replacingConfigurator(configurator);
result = new ConfigurationPersistence(logContext);
root.attach(Configurator.ATTACHMENT_KEY, result);
}
}
return result;
}
use of org.jboss.logmanager.PropertyConfigurator in project activemq-artemis by rh-messaging.
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;
}
Aggregations