Java Code Examples for org.apache.logging.log4j.status.StatusLogger#getLogger()

The following examples show how to use org.apache.logging.log4j.status.StatusLogger#getLogger() . 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: AbstractActionTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testExceptionsAreLoggedToStatusLogger() {
    StatusLogger statusLogger = StatusLogger.getLogger();
    statusLogger.clear();
    new TestAction().run();
    List<StatusData> statusDataList = statusLogger.getStatusData();
    assertEquals(1, statusDataList.size());
    StatusData statusData = statusDataList.get(0);
    assertEquals(Level.WARN, statusData.getLevel());
    String formattedMessage = statusData.getFormattedStatus();
    assertTrue(formattedMessage, formattedMessage.contains("Exception reported by action 'class org.apache."
            + "logging.log4j.core.appender.rolling.action.AbstractActionTest$TestAction' java.io.IOException: "
            + "failed" + System.lineSeparator()
            + "\tat org.apache.logging.log4j.core.appender.rolling.action.AbstractActionTest"
            + "$TestAction.execute(AbstractActionTest.java:"));
}
 
Example 2
Source File: AbstractActionTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testRuntimeExceptionsAreLoggedToStatusLogger() {
    StatusLogger statusLogger = StatusLogger.getLogger();
    statusLogger.clear();
    new AbstractAction() {
        @Override
        public boolean execute() {
            throw new IllegalStateException();
        }
    }.run();
    List<StatusData> statusDataList = statusLogger.getStatusData();
    assertEquals(1, statusDataList.size());
    StatusData statusData = statusDataList.get(0);
    assertEquals(Level.WARN, statusData.getLevel());
    String formattedMessage = statusData.getFormattedStatus();
    assertTrue(formattedMessage.contains("Exception reported by action"));
}
 
Example 3
Source File: AbstractActionTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testErrorsAreLoggedToStatusLogger() {
    StatusLogger statusLogger = StatusLogger.getLogger();
    statusLogger.clear();
    new AbstractAction() {
        @Override
        public boolean execute() {
            throw new AssertionError();
        }
    }.run();
    List<StatusData> statusDataList = statusLogger.getStatusData();
    assertEquals(1, statusDataList.size());
    StatusData statusData = statusDataList.get(0);
    assertEquals(Level.WARN, statusData.getLevel());
    String formattedMessage = statusData.getFormattedStatus();
    assertTrue(formattedMessage.contains("Exception reported by action"));
}
 
Example 4
Source File: LocalizedMessage.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    formattedMessage = in.readUTF();
    key = in.readUTF();
    baseName = in.readUTF();
    in.readInt();
    stringArgs = (String[]) in.readObject();
    logger = StatusLogger.getLogger();
    resourceBundle = null;
    argArray = null;
}
 
Example 5
Source File: Log4jServletContainerInitializer.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartup(final Set<Class<?>> classes, final ServletContext servletContext) throws ServletException {
    if (servletContext.getMajorVersion() > 2 && servletContext.getEffectiveMajorVersion() > 2 &&
            !"true".equalsIgnoreCase(servletContext.getInitParameter(
                    Log4jWebSupport.IS_LOG4J_AUTO_INITIALIZATION_DISABLED
            ))) {
        final Logger LOGGER = StatusLogger.getLogger();

        LOGGER.debug("Log4jServletContainerInitializer starting up Log4j in Servlet 3.0+ environment.");

        final FilterRegistration.Dynamic filter =
                servletContext.addFilter("log4jServletFilter", Log4jServletFilter.class);
        if (filter == null) {
            LOGGER.warn("WARNING: In a Servlet 3.0+ application, you should not define a " +
                "log4jServletFilter in web.xml. Log4j 2 normally does this for you automatically. Log4j 2 " +
                "web auto-initialization has been canceled.");
            return;
        }

        final Log4jWebLifeCycle initializer = WebLoggerContextUtils.getWebLifeCycle(servletContext);
        initializer.start();
        initializer.setLoggerContext(); // the application is just now starting to start up

        servletContext.addListener(new Log4jServletContextListener());

        filter.setAsyncSupported(true); // supporting async when the user isn't using async has no downsides
        filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
    }
}
 
Example 6
Source File: StatusLoggerAdmin.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Add listener to StatusLogger for this context, or replace it if it already exists.
 *
 * @param ctxName
 */
private void removeListeners(final String ctxName) {
    final StatusLogger logger = StatusLogger.getLogger();
    final Iterable<StatusListener> listeners = logger.getListeners();
    // Remove any StatusLoggerAdmin listeners already registered for this context
    for (final StatusListener statusListener : listeners) {
        if (statusListener instanceof StatusLoggerAdmin) {
            final StatusLoggerAdmin adminListener = (StatusLoggerAdmin) statusListener;
            if (ctxName != null && ctxName.equals(adminListener.contextName)) {
                logger.removeListener(adminListener);
            }
        }
    }
}
 
Example 7
Source File: Log4j2StatusLoggerWrapper.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
Log4j2StatusLoggerWrapper() {
    this.logger = StatusLogger.getLogger();
}
 
Example 8
Source File: AbstractManager.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
protected static StatusLogger logger() {
    return StatusLogger.getLogger();
}