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); } }
/** * Returns the Configuration obtained using a given ClassLoader. * @param loggerContext The logger context * @param name The configuration name. * @param configLocation A URI representing the location of the configuration. * @param loader The default ClassLoader to use. If this is {@code null}, then the * {@linkplain LoaderUtil#getThreadContextClassLoader() default ClassLoader} will be used. * * @return The Configuration. */ public Configuration getConfiguration(final LoggerContext loggerContext, final String name, final URI configLocation, final ClassLoader loader) { if (!isActive()) { return null; } if (loader == null) { return getConfiguration(loggerContext, name, configLocation); } if (isClassLoaderUri(configLocation)) { final String path = extractClassLoaderUriPath(configLocation); final ConfigurationSource source = ConfigurationSource.fromResource(path, loader); if (source != null) { final Configuration configuration = getConfiguration(loggerContext, source); if (configuration != null) { return configuration; } } } return getConfiguration(loggerContext, name, configLocation); }
@Override public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) { if (source != null) { final String config = source.getLocation(); for (final ConfigurationFactory factory : getFactories()) { final String[] types = factory.getSupportedTypes(); if (types != null) { for (final String type : types) { if (type.equals(ALL_TYPES) || config != null && config.endsWith(type)) { final Configuration c = factory.getConfiguration(loggerContext, source); if (c != null) { LOGGER.debug("Loaded configuration from {}", source); return c; } LOGGER.error("Cannot determine the ConfigurationFactory to use for {}", config); return null; } } } } } LOGGER.error("Cannot process configuration, input source is null"); return null; } }
/** * Returns the Configuration. * @param loggerContext The logger context * @param name The configuration name. * @param configLocation The configuration location. * @return The Configuration. */ public Configuration getConfiguration(final LoggerContext loggerContext, final String name, final URI configLocation) { if (!isActive()) { return null; } if (configLocation != null) { final ConfigurationSource source = ConfigurationSource.fromUri(configLocation); if (source != null) { return getConfiguration(loggerContext, source); } } return null; }
@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); } }
private Configuration getConfiguration(final LoggerContext loggerContext, final boolean isTest, final String name) { final boolean named = Strings.isNotEmpty(name); final ClassLoader loader = LoaderUtil.getThreadContextClassLoader(); for (final ConfigurationFactory factory : getFactories()) { String configName; final String prefix = isTest ? TEST_PREFIX : DEFAULT_PREFIX; final String [] types = factory.getSupportedTypes(); if (types == null) { continue; } for (final String suffix : types) { if (suffix.equals(ALL_TYPES)) { continue; } configName = named ? prefix + name + suffix : prefix + suffix; final ConfigurationSource source = ConfigurationSource.fromResource(configName, loader); if (source != null) { if (!factory.isActive()) { LOGGER.warn("Found configuration file {} for inactive ConfigurationFactory {}", configName, factory.getClass().getName()); } return factory.getConfiguration(loggerContext, source); } } } return null; }
@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 {
ConfigurationSource cfgSrc = new ConfigurationSource(fis); this.context = new LoggerContext(this.ctxName); cfg = factory.getConfiguration(this.context, cfgSrc);
/** * 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()); } } }
private Configuration getConfiguration(final LoggerContext loggerContext, final String configLocationStr) { ConfigurationSource source = null; try { source = ConfigurationSource.fromUri(NetUtils.toURI(configLocationStr)); } catch (final Exception ex) { // Ignore the error and try as a String. LOGGER.catching(Level.DEBUG, ex); } if (source == null) { final ClassLoader loader = LoaderUtil.getThreadContextClassLoader(); source = getInputFromString(configLocationStr, loader); } if (source != null) { for (final ConfigurationFactory factory : getFactories()) { final String[] types = factory.getSupportedTypes(); if (types != null) { for (final String type : types) { if (type.equals(ALL_TYPES) || configLocationStr.endsWith(type)) { final Configuration config = factory.getConfiguration(loggerContext, source); if (config != null) { return config; } } } } } } return null; }
for (final URI configLocation : configLocations) { final Configuration currentReadConfiguration = ConfigurationFactory.getInstance() .getConfiguration(ctx, name, configLocation); if (currentReadConfiguration instanceof AbstractConfiguration) { configurations.add((AbstractConfiguration) currentReadConfiguration);
@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);