@Test public void testRollingFileAppenderWithReconfigure() throws Exception { logger.debug("Before reconfigure"); @SuppressWarnings("resource") // managed by the rule. final LoggerContext context = loggerContextRule.getLoggerContext(); Configuration config = context.getConfiguration(); context.setConfigLocation(new URI(CONFIG)); context.reconfigure(); logger.debug("Force a rollover"); final File dir = new File(DIR); for (int i = 0; i < MAX_TRIES; ++i) { Thread.sleep(200); if (config != context.getConfiguration()) { break; } } assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0); final File[] files = dir.listFiles(); assertNotNull(files); assertThat(dir.listFiles().length, is(equalTo(2))); } }
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false); // this will force a reconfiguration context.setConfigLocation(logConfFile.toURI());
File file = new File("/config/new/log4j2.xml"); final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); ctx.setConfigLocation(file.toURI());
LoggerContext context = (LoggerContext)LogManager.getContext(false); context.setConfigLocation(URI.create("path to file")); context.reconfigure();
File file = new File(log4jConfigFilePath); final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); ctx.setConfigLocation(file.toURI()); Collection<org.apache.logging.log4j.core.Logger> collection = ctx.getLoggers(); System.out.println(collection.size()); // returns 0 (No loggers instantiated) Logger logger = LoggerFactory.getLogger("myLogger"); collection = ctx.getLoggers(); System.out.println(collection.size()); // returns 1 (myLogger instantiated)
private static void updateLoggerContext(final JsonObject config) { try { loggerCtx.setConfigLocation(new URI(getLog4jConfigUri(config))); } catch (final Exception e) { logger.error(e); } }
private static LoggerContext locateContext(final Bundle bundle, final URI configLocation) { final String name = Objects.requireNonNull(bundle, "No Bundle provided").getSymbolicName(); final AtomicReference<WeakReference<LoggerContext>> ref = CONTEXT_MAP.get(name); if (ref == null) { final LoggerContext context = new LoggerContext(name, bundle, configLocation); CONTEXT_MAP.putIfAbsent(name, new AtomicReference<>(new WeakReference<>(context))); return CONTEXT_MAP.get(name).get().get(); } final WeakReference<LoggerContext> r = ref.get(); final LoggerContext ctx = r.get(); if (ctx == null) { final LoggerContext context = new LoggerContext(name, bundle, configLocation); ref.compareAndSet(r, new WeakReference<>(context)); return ref.get().get(); } final URI oldConfigLocation = ctx.getConfigLocation(); if (oldConfigLocation == null && configLocation != null) { LOGGER.debug("Setting bundle ({}) configuration to {}", name, configLocation); ctx.setConfigLocation(configLocation); } else if (oldConfigLocation != null && configLocation != null && !configLocation.equals(oldConfigLocation)) { LOGGER.warn("locateContext called with URI [{}], but existing LoggerContext has URI [{}]", configLocation, oldConfigLocation); } return ctx; } }
if (ctx.getConfigLocation() == null && configLocation != null) { LOGGER.debug("Setting configuration to {}", configLocation); ctx.setConfigLocation(configLocation); } else if (ctx.getConfigLocation() != null && configLocation != null && !ctx.getConfigLocation().equals(configLocation)) {
ctx.setConfigLocation(log4JUri); logger.debug("log4j reconfigured");
public static void configure(File dataDir) { File file = getConfigurationFile(dataDir); if (file.exists()) { if (System.getProperty("log.file") == null) { System.setProperty("log.file", new File(dataDir, DEBUG_LOG).getAbsolutePath()); } // register configuration error listener StatusListener errorStatusListener = new ConfigurationErrorStatusListener(); StatusLogger.getLogger().setLevel(Level.OFF); StatusLogger.getLogger().reset(); StatusLogger.getLogger().registerListener(errorStatusListener); // load configuration final LoggerContext context = (LoggerContext) LogManager.getContext(false); context.setConfigLocation(file.toURI()); context.updateLoggers(); // remove configuration error listener StatusLogger.getLogger().removeListener(errorStatusListener); } }
@BeforeClass public static void setup() throws IOException { LOGGER_CONTEXT.setConfigLocation(new ClassPathResource("log4j2-amqp-appender.xml").getURI()); LOGGER_CONTEXT.reconfigure(); }
context.setConfigLocation(uri); log.info("Logging configuration loaded from source: {}", uri.toString());
@AfterClass public static void teardown() { LOGGER_CONTEXT.setConfigLocation(ORIGINAL_LOGGER_CONFIG); LOGGER_CONTEXT.reconfigure(); brokerRunning.deleteQueues("log4jTest", "log4j2Test"); brokerRunning.deleteExchanges("log4j2Test", "log4j2Test_uri"); }