protected void loadConfiguration(String location, LogFile logFile) { Assert.notNull(location, "Location must not be null"); try { LoggerContext ctx = getLoggerContext(); URL url = ResourceUtils.getURL(location); ConfigurationSource source = getConfigurationSource(url); ctx.start(ConfigurationFactory.getInstance().getConfiguration(ctx, source)); } catch (Exception ex) { throw new IllegalStateException( "Could not initialize Log4J2 logging from " + location, ex); } }
@Nonnull protected Configuration createConfigurationFor(@Nonnull Log4J2BasedLoggingEnvironmentConfiguration requirement) { return ConfigurationFactory.getInstance().getConfiguration(createConfigurationSourceFor(requirement)); }
/** * Reconfigures the context. */ private void reconfigure(final URI configURI) { final ClassLoader cl = ClassLoader.class.isInstance(externalContext) ? (ClassLoader) externalContext : null; LOGGER.debug("Reconfiguration started for context[name={}] at URI {} ({}) with optional ClassLoader: {}", contextName, configURI, this, cl); final Configuration instance = ConfigurationFactory.getInstance().getConfiguration(this, contextName, configURI, cl); if (instance == null) { LOGGER.error("Reconfiguration failed: No configuration found for '{}' at '{}' in '{}'", contextName, configURI, cl); } else { setConfiguration(instance); /* * instance.start(); Configuration old = setConfiguration(instance); updateLoggers(); if (old != null) { * old.stop(); } */ final String location = configuration == null ? "?" : String.valueOf(configuration.getConfigurationSource()); LOGGER.debug("Reconfiguration complete for context[name={}] at URI {} ({}) with optional ClassLoader: {}", contextName, location, this, cl); } }
ConfigurationFactory configurationFactory = ConfigurationFactory.getInstance(); try {
@Override public void setConfigLocationUri(final String configLocation) throws URISyntaxException, IOException { if (configLocation == null || configLocation.isEmpty()) { throw new IllegalArgumentException("Missing configuration location"); } LOGGER.debug("---------"); LOGGER.debug("Remote request to reconfigure using location " + configLocation); final File configFile = new File(configLocation); ConfigurationSource configSource = null; if (configFile.exists()) { LOGGER.debug("Opening config file {}", configFile.getAbsolutePath()); configSource = new ConfigurationSource(new FileInputStream(configFile), configFile); } else { final URL configURL = new URL(configLocation); LOGGER.debug("Opening config URL {}", configURL); configSource = new ConfigurationSource(configURL.openStream(), configURL); } final Configuration config = ConfigurationFactory.getInstance().getConfiguration(loggerContext, configSource); loggerContext.start(config); LOGGER.debug("Completed remote request to reconfigure."); }
@Override public void setConfigText(final String configText, final String charsetName) { LOGGER.debug("---------"); LOGGER.debug("Remote request to reconfigure from config text."); try { final InputStream in = new ByteArrayInputStream(configText.getBytes(charsetName)); final ConfigurationSource source = new ConfigurationSource(in); final Configuration updated = ConfigurationFactory.getInstance().getConfiguration(loggerContext, source); loggerContext.start(updated); LOGGER.debug("Completed remote request to reconfigure from config text."); } catch (final Exception ex) { final String msg = "Could not reconfigure from config text"; LOGGER.error(msg, ex); throw new IllegalArgumentException(msg, ex); } }
@Override public Configuration reconfigure() { LOGGER.debug("Reconfiguring composite configuration"); final List<AbstractConfiguration> configs = new ArrayList<>(); final ConfigurationFactory factory = ConfigurationFactory.getInstance(); for (final AbstractConfiguration config : configurations) { final ConfigurationSource source = config.getConfigurationSource(); final URI sourceURI = source.getURI(); Configuration currentConfig; if (sourceURI == null) { LOGGER.warn("Unable to determine URI for configuration {}, changes to it will be ignored", config.getName()); currentConfig = factory.getConfiguration(getLoggerContext(), config.getName(), sourceURI); if (currentConfig == null) { LOGGER.warn("Unable to reload configuration {}, changes to it will be ignored", config.getName()); currentConfig = config; } } else { currentConfig = config; } configs.add((AbstractConfiguration) currentConfig); } return new CompositeConfiguration(configs); }
Object configfile = configuration.get(LOG4J2_CONFIG_FILE_KEY); if (configfile != null) { config = ConfigurationFactory.getInstance().getConfiguration(m_log4jContext, LOGGER_CONTEXT_NAME, new File(configfile.toString()).toURI()); } else {
/** * Initialize Log4J, including setting the web app root system property. */ public static void initLogging(MagnoliaConfigurationProperties configurationProperties, FileSystemHelper fileSystemHelper) { // can't use log4j yet log("Initializing Log4J"); //TODO: should provide a new default log4j2.xml? In case of migration and legacy system doesn't have new log4j.xml with new syntax, default fallback would help log4j2 start successfully. String log4jFileName = configurationProperties.getProperty(LOG4J_CONFIG); if (StringUtils.isNotEmpty(log4jFileName)) { final String config; try { config = fileSystemHelper.getTokenizedConfigFile(log4jFileName); } catch (IOException e) { log("Cannot load config file [" + log4jFileName + "], got a IOException :" + e.getMessage()); return; } try (InputStream log4jConfigStream = IOUtils.toInputStream(config)) { ConfigurationSource source = new ConfigurationSource(log4jConfigStream); Configuration configuration = ConfigurationFactory.getInstance().getConfiguration(null, source); ((LoggerContext) LogManager.getContext(false)).start(configuration); } catch (IOException ex) { log("Cannot load log configuration with config file [" + config + "], get IOException: " + ex.getMessage()); } } }
final List<AbstractConfiguration> configurations = new ArrayList<>(configLocations.size()); for (final URI configLocation : configLocations) { final Configuration currentReadConfiguration = ConfigurationFactory.getInstance() .getConfiguration(ctx, name, configLocation); if (currentReadConfiguration instanceof AbstractConfiguration) {
@Override public synchronized ILogger init(ILog owner, String loggerName) throws Exception { if (!__inited) { __owner = owner; __loggerName = loggerName; // if (!__logInited) { ConfigurationSource _source = new ConfigurationSource(new FileInputStream(__owner.getModuleCfg().getConfigFile())); Configurator.initialize(null, _source); final Configuration _config = new DefaultConfiguration(); ConfigurationFactory.setConfigurationFactory(new XmlConfigurationFactory() { @Override public Configuration getConfiguration(ConfigurationSource source) { return _config; } }); ConfigurationFactory.getInstance().getConfiguration(_source); __logInited = true; } __logger = LogManager.getLogger(StringUtils.defaultIfBlank(loggerName, __owner.getModuleCfg().getLoggerName())); __inited = true; } return this; }
@Override public LoggerContext getContext(String fqcn, ClassLoader loader, Object externalContext, boolean currentContext, URI configLocation, String name) { final LoggerContext ctx = customLoggerContext; if (externalContext != null && ctx.getExternalContext() == null) { ctx.setExternalContext(externalContext); } if (name != null) { ctx.setName(name); } if (ctx.getState() == LifeCycle.State.INITIALIZED) { if (configLocation != null || name != null) { THREAD_CONTEXT.set(ctx); final Configuration config = ConfigurationFactory.getInstance().getConfiguration(name, configLocation); StatusLogger.getLogger().debug("Starting LoggerContext[name={}] from configuration at {}", ctx.getName(), configLocation); ctx.start(config); THREAD_CONTEXT.remove(); } else { ctx.start(); } } return ctx; } }
/** * Loads the LoggerContext using the ContextSelector. * @param fqcn The fully qualified class name of the caller. * @param loader The ClassLoader to use or null. * @param externalContext An external context (such as a ServletContext) to be associated with the LoggerContext. * @param currentContext If true returns the current Context, if false returns the Context appropriate * for the caller if a more appropriate Context can be determined. * @param source The configuration source. * @return The LoggerContext. */ public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext, final boolean currentContext, final ConfigurationSource source) { final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext, null); if (externalContext != null && ctx.getExternalContext() == null) { ctx.setExternalContext(externalContext); } if (ctx.getState() == LifeCycle.State.INITIALIZED) { if (source != null) { ContextAnchor.THREAD_CONTEXT.set(ctx); final Configuration config = ConfigurationFactory.getInstance().getConfiguration(ctx, source); LOGGER.debug("Starting LoggerContext[name={}] from configuration {}", ctx.getName(), source); ctx.start(config); ContextAnchor.THREAD_CONTEXT.remove(); } else { ctx.start(); } } return ctx; }
Configuration config = ConfigurationFactory.getInstance().getConfiguration( loggerContext, source); for (Map.Entry entry : properties.entrySet()) {
if (configLocation != null || name != null) { ContextAnchor.THREAD_CONTEXT.set(ctx); final Configuration config = ConfigurationFactory.getInstance().getConfiguration(ctx, name, configLocation); LOGGER.debug("Starting LoggerContext[name={}] from configuration at {}", ctx.getName(), configLocation); ctx.start(config);