org.apache.logging.log4j.core.impl.Log4jContextFactory Java Examples
The following examples show how to use
org.apache.logging.log4j.core.impl.Log4jContextFactory.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Log4JAccessLogTest.java From Poseidon with Apache License 2.0 | 6 votes |
@Before public void setup() throws Exception { // Create mock objects mockLog4jContextFactory = mock(Log4jContextFactory.class); whenNew(Log4jContextFactory.class).withNoArguments().thenReturn(mockLog4jContextFactory); mockLoggerContext = mock(LoggerContext.class); mockLogger = mock(Logger.class); when(mockLog4jContextFactory.getContext(anyString(), any(ClassLoader.class), any(), anyBoolean(), any(URI.class), anyString())).thenReturn(mockLoggerContext); when(mockLoggerContext.getRootLogger()).thenReturn(mockLogger); mockRequest = mock(Request.class); mockResponse = mock(Response.class); mockAccessLog = mock(AccessLog.class); whenNew(AccessLog.class).withArguments(mockRequest, mockResponse).thenReturn(mockAccessLog); // Create actual objects enabledSupplier = () -> true; disabledSupplier = () -> false; failedAccessLog = new TestLog4JAccessLog(NON_EXISTING_FILE_PATH, enabledSupplier); String filePath = getClass().getClassLoader().getResource(ACCESS_CONFIG_FILE_PATH).getPath(); enabledAccessLog = new TestLog4JAccessLog(filePath, enabledSupplier); disabledAccessLog = new TestLog4JAccessLog(filePath, disabledSupplier); }
Example #2
Source File: Configurator.java From logging-log4j2 with Apache License 2.0 | 6 votes |
/** * Initializes the Logging Context. * @param loader The ClassLoader for the Context (or null). * @param source The InputSource for the configuration. * @param externalContext The external context to be attached to the LoggerContext. * @return The LoggerContext. */ public static LoggerContext initialize(final ClassLoader loader, final ConfigurationSource source, final Object externalContext) { try { final Log4jContextFactory factory = getFactory(); return factory == null ? null : factory.getContext(FQCN, loader, externalContext, false, source); } catch (final Exception ex) { LOGGER.error("There was a problem obtaining a LoggerContext using the configuration source [{}]", source, ex); } return null; }
Example #3
Source File: Logging.java From ProjectAres with GNU Affero General Public License v3.0 | 5 votes |
public static List<? extends LoggerContext> getContexts() { LoggerContextFactory factory = org.apache.logging.log4j.LogManager.getFactory(); if(factory instanceof SimpleLoggerContextFactory) { return Collections.singletonList(factory.getContext(null, null, null, true)); } return ((Log4jContextFactory) org.apache.logging.log4j.LogManager.getFactory()).getSelector().getLoggerContexts(); }
Example #4
Source File: Log4JAccessLog.java From Poseidon with Apache License 2.0 | 5 votes |
@Override protected void doStart() throws Exception { File accessConfigFile = new File(accessConfigFilePath); if (!accessConfigFile.isFile()) { throw new Exception("Log4J access config file not found: " + accessConfigFilePath); } loggerContext = new Log4jContextFactory().getContext(Log4JAccessLog.class.getName(), null, null, true, accessConfigFile.toURI(), "PoseidonLog4JAccess"); logger = loggerContext.getRootLogger(); }
Example #5
Source File: Log4jWebInitializerImpl.java From logging-log4j2 with Apache License 2.0 | 5 votes |
private void initializeJndi(final String location) { final URI configLocation = getConfigURI(location); if (this.name == null) { throw new IllegalStateException("A log4jContextName context parameter is required"); } LoggerContext context; final LoggerContextFactory factory = LogManager.getFactory(); if (factory instanceof Log4jContextFactory) { final ContextSelector selector = ((Log4jContextFactory) factory).getSelector(); if (selector instanceof NamedContextSelector) { this.namedContextSelector = (NamedContextSelector) selector; context = this.namedContextSelector.locateContext(this.name, this.servletContext, configLocation); ContextAnchor.THREAD_CONTEXT.set(context); if (context.isInitialized()) { context.start(); } ContextAnchor.THREAD_CONTEXT.remove(); } else { LOGGER.warn("Potential problem: Selector is not an instance of NamedContextSelector."); return; } } else { LOGGER.warn("Potential problem: LoggerContextFactory is not an instance of Log4jContextFactory."); return; } this.loggerContext = context; LOGGER.debug("Created logger context for [{}] using [{}].", this.name, context.getClass().getClassLoader()); }
Example #6
Source File: Server.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Returns the {@code ContextSelector} of the current {@code Log4jContextFactory}. * * @return the {@code ContextSelector} of the current {@code Log4jContextFactory} */ private static ContextSelector getContextSelector() { final LoggerContextFactory factory = LogManager.getFactory(); if (factory instanceof Log4jContextFactory) { final ContextSelector selector = ((Log4jContextFactory) factory).getSelector(); return selector; } return null; }
Example #7
Source File: Configurator.java From logging-log4j2 with Apache License 2.0 | 5 votes |
private static Log4jContextFactory getFactory() { final LoggerContextFactory factory = LogManager.getFactory(); if (factory instanceof Log4jContextFactory) { return (Log4jContextFactory) factory; } else if (factory != null) { LOGGER.error("LogManager returned an instance of {} which does not implement {}. Unable to initialize Log4j.", factory.getClass().getName(), Log4jContextFactory.class.getName()); return null; } else { LOGGER.fatal("LogManager did not return a LoggerContextFactory. This indicates something has gone terribly wrong!"); return null; } }
Example #8
Source File: Configurator.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Initializes the Logging Context. * @param name The Context name. * @param loader The ClassLoader for the Context (or null). * @param configLocation The configuration for the logging context (or null). * @param externalContext The external context to be attached to the LoggerContext * @return The LoggerContext. */ public static LoggerContext initialize(final String name, final ClassLoader loader, final URI configLocation, final Object externalContext) { try { final Log4jContextFactory factory = getFactory(); return factory == null ? null : factory.getContext(FQCN, loader, externalContext, false, configLocation, name); } catch (final Exception ex) { LOGGER.error("There was a problem initializing the LoggerContext [{}] using configuration at [{}].", name, configLocation, ex); } return null; }
Example #9
Source File: Configurator.java From logging-log4j2 with Apache License 2.0 | 5 votes |
public static LoggerContext initialize(final String name, final ClassLoader loader, final List<URI> configLocations, final Object externalContext) { try { final Log4jContextFactory factory = getFactory(); return factory == null ? null : factory.getContext(FQCN, loader, externalContext, false, configLocations, name); } catch (final Exception ex) { LOGGER.error("There was a problem initializing the LoggerContext [{}] using configurations at [{}].", name, configLocations, ex); } return null; }
Example #10
Source File: Configurator.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Initializes the Logging Context. * @param loader The ClassLoader. * @param configuration The Configuration. * @param externalContext - The external context to be attached to the LoggerContext. * @return The LoggerContext. */ public static LoggerContext initialize(final ClassLoader loader, final Configuration configuration, final Object externalContext) { try { final Log4jContextFactory factory = getFactory(); return factory == null ? null : factory.getContext(FQCN, loader, externalContext, false, configuration); } catch (final Exception ex) { LOGGER.error("There was a problem initializing the LoggerContext using configuration {}", configuration.getName(), ex); } return null; }
Example #11
Source File: Configurator.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Reconfigure using an already constructed Configuration. * @param configuration The configuration. * @since 2.13.0 */ public static void reconfigure(final Configuration configuration) { try { final Log4jContextFactory factory = getFactory(); if (factory != null) { factory.getContext(FQCN, null, null, false) .reconfigure(configuration); } } catch (final Exception ex) { LOGGER.error("There was a problem initializing the LoggerContext using configuration {}", configuration.getName(), ex); } }
Example #12
Source File: Configurator.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Reload the existing reconfiguration. * @since 2.12.0 */ public static void reconfigure() { try { Log4jContextFactory factory = getFactory(); if (factory != null) { factory.getSelector().getContext(FQCN, null, false).reconfigure(); } else { LOGGER.warn("Unable to reconfigure - Log4j has not been initialized."); } } catch (final Exception ex) { LOGGER.error("Error encountered trying to reconfigure logging", ex); } }
Example #13
Source File: Configurator.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Reconfigure with a potentially new configuration. * @param uri The location of the configuration. * @since 2.12.0 */ public static void reconfigure(final URI uri) { try { Log4jContextFactory factory = getFactory(); if (factory != null) { factory.getSelector().getContext(FQCN, null, false).setConfigLocation(uri); } else { LOGGER.warn("Unable to reconfigure - Log4j has not been initialized."); } } catch (final Exception ex) { LOGGER.error("Error encountered trying to reconfigure logging", ex); } }
Example #14
Source File: ShutdownCallbackRegistryTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testShutdownCallbackRegistry() throws Exception { final LoggerContext context = ctx.getLoggerContext(); assertTrue("LoggerContext should be started", context.isStarted()); assertThat(Registry.CALLBACKS, hasSize(1)); Registry.shutdown(); assertTrue("LoggerContext should be stopped", context.isStopped()); assertThat(Registry.CALLBACKS, hasSize(0)); final ContextSelector selector = ((Log4jContextFactory) LogManager.getFactory()).getSelector(); assertThat(selector.getLoggerContexts(), not(hasItem(context))); }
Example #15
Source File: PropertyTest.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Test public void testShutdownHookDisabled() { assertFalse( "Shutdown hook should be disabled by default in web applications", ((Log4jContextFactory) LogManager.getFactory()).isShutdownHookEnabled()); }