Java Code Examples for org.apache.log4j.Appender#doAppend()

The following examples show how to use org.apache.log4j.Appender#doAppend() . 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: SchedulerStateRestLiveLogsTest.java    From scheduling with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testLiveLogs_TwoJobsAtTheSameTime() throws Exception {

    String firstJobId = "42";
    String secondJobId = "43";

    String firstJobLogs = client.getLiveLogJob(sessionId, firstJobId);
    Appender firstAppender = verifyListenAndGetAppender(firstJobId);
    String secondJobLogs = client.getLiveLogJob(sessionId, secondJobId);
    Appender secondAppender = verifyListenAndGetAppender(secondJobId);

    assertTrue(firstJobLogs.isEmpty());
    assertTrue(secondJobLogs.isEmpty());

    firstAppender.doAppend(createLoggingEvent(firstJobId, "first job"));
    secondAppender.doAppend(createLoggingEvent(secondJobId, "second job"));

    firstJobLogs = client.getLiveLogJob(sessionId, firstJobId);
    assertThat(firstJobLogs, containsString("first job"));

    secondJobLogs = client.getLiveLogJob(sessionId, secondJobId);
    assertThat(secondJobLogs, containsString("second job"));
}
 
Example 2
Source File: ThreadLocalAwareLogger.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * Logger worker method - does the actual checks whether the given message should be logged at the given priority - taking into account this logger's loglevel plus
 * the threadLocalLogLevel.
 * 
 * @param priority
 *            the priority at which the given message should be logged
 * @param message
 *            the message which should be logged (or not, depending on levels)
 */
private final void threadLocalAwareLog(String fQCN, Priority priority, Object message, Throwable t) {
    if (isForcedToLog(priority)) {

        final Appender forcedAppender = getForcedAppender();

        // force the logging and modify the log message (the latter might return the original message)
        if (forcedAppender != null) {
            // only call the forced appender
            forcedAppender.doAppend(new LoggingEvent(fQCN, this, priority, modifyLogMessage(message), t));
        } else {
            // go via the normal appenders
            forcedLog(fQCN, priority, modifyLogMessage(message), t);
        }

    } else {

        // else not forced to log - use default behaviour
        super.log(fQCN, priority, message, t);

    }
}
 
Example 3
Source File: ThreadLocalAwareLogger.java    From olat with Apache License 2.0 6 votes vote down vote up
@Override
public void l7dlog(Priority priority, String key, Object[] params, Throwable t) {
    if (isForcedToLog(priority)) {
        // from super.l7dlog:
        String pattern = getResourceBundleString(key);
        String msg;
        if (pattern == null)
            msg = key;
        else
            msg = java.text.MessageFormat.format(pattern, params);

        final Appender forcedAppender = getForcedAppender();

        // force the logging and modify the log message (the latter might return the original message)
        if (forcedAppender != null) {
            // only call the forced appender
            forcedAppender.doAppend(new LoggingEvent(FQCN, this, priority, modifyLogMessage(msg), t));
        } else {
            // go via the normal appenders
            forcedLog(FQCN, priority, modifyLogMessage(msg), t);
        }

    } else {
        super.l7dlog(priority, key, params, t);
    }
}
 
Example 4
Source File: ThreadLocalAwareLogger.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * Logger worker method - does the actual checks whether the given message should be logged at the given priority - taking into account this logger's loglevel plus
 * the threadLocalLogLevel.
 * 
 * @param priority
 *            the priority at which the given message should be logged
 * @param message
 *            the message which should be logged (or not, depending on levels)
 */
private final void threadLocalAwareLog(String fQCN, Priority priority, Object message, Throwable t) {
    if (isForcedToLog(priority)) {

        final Appender forcedAppender = getForcedAppender();

        // force the logging and modify the log message (the latter might return the original message)
        if (forcedAppender != null) {
            // only call the forced appender
            forcedAppender.doAppend(new LoggingEvent(fQCN, this, priority, modifyLogMessage(message), t));
        } else {
            // go via the normal appenders
            forcedLog(fQCN, priority, modifyLogMessage(message), t);
        }

    } else {

        // else not forced to log - use default behaviour
        super.log(fQCN, priority, message, t);

    }
}
 
Example 5
Source File: ThreadLocalAwareLogger.java    From olat with Apache License 2.0 6 votes vote down vote up
@Override
public void l7dlog(Priority priority, String key, Object[] params, Throwable t) {
    if (isForcedToLog(priority)) {
        // from super.l7dlog:
        String pattern = getResourceBundleString(key);
        String msg;
        if (pattern == null)
            msg = key;
        else
            msg = java.text.MessageFormat.format(pattern, params);

        final Appender forcedAppender = getForcedAppender();

        // force the logging and modify the log message (the latter might return the original message)
        if (forcedAppender != null) {
            // only call the forced appender
            forcedAppender.doAppend(new LoggingEvent(FQCN, this, priority, modifyLogMessage(msg), t));
        } else {
            // go via the normal appenders
            forcedLog(FQCN, priority, modifyLogMessage(msg), t);
        }

    } else {
        super.l7dlog(priority, key, params, t);
    }
}
 
Example 6
Source File: EnabledListenJobLogsSupport.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
private void flushTaskLogs(TaskResult tr, Appender a, JobId jobId) {
    // if taskResult is not awaited, task is terminated
    TaskLogs logs = tr.getOutput();
    if (logs instanceof Log4JTaskLogs) {
        for (LoggingEvent le : ((Log4JTaskLogs) logs).getAllEvents()) {
            // write into socket appender directly to avoid double lines on other listeners
            a.doAppend(le);
        }
    } else {
        a.doAppend(createLoggingEvent(jobId, logs.getStdoutLogs(false), Level.INFO));
        a.doAppend(createLoggingEvent(jobId, logs.getStderrLogs(false), Level.DEBUG));
    }
}
 
Example 7
Source File: ThreadLocalAwareLogger.java    From olat with Apache License 2.0 5 votes vote down vote up
@Override
public void l7dlog(Priority priority, String key, Throwable t) {
    if (isForcedToLog(priority)) {
        // from super.l7dlog:

        String msg = getResourceBundleString(key);
        // if message corresponding to 'key' could not be found in the
        // resource bundle, then default to 'key'.
        if (msg == null) {
            msg = key;
        }

        final Appender forcedAppender = getForcedAppender();

        // force the logging and modify the log message (the latter might return the original message)
        if (forcedAppender != null) {
            // only call the forced appender
            forcedAppender.doAppend(new LoggingEvent(FQCN, this, priority, modifyLogMessage(msg), t));
        } else {
            // go via the normal appenders
            forcedLog(FQCN, priority, modifyLogMessage(msg), t);
        }

    } else {
        super.l7dlog(priority, key, t);
    }
}
 
Example 8
Source File: ThreadLocalAwareLogger.java    From olat with Apache License 2.0 5 votes vote down vote up
@Override
public void l7dlog(Priority priority, String key, Throwable t) {
    if (isForcedToLog(priority)) {
        // from super.l7dlog:

        String msg = getResourceBundleString(key);
        // if message corresponding to 'key' could not be found in the
        // resource bundle, then default to 'key'.
        if (msg == null) {
            msg = key;
        }

        final Appender forcedAppender = getForcedAppender();

        // force the logging and modify the log message (the latter might return the original message)
        if (forcedAppender != null) {
            // only call the forced appender
            forcedAppender.doAppend(new LoggingEvent(FQCN, this, priority, modifyLogMessage(msg), t));
        } else {
            // go via the normal appenders
            forcedLog(FQCN, priority, modifyLogMessage(msg), t);
        }

    } else {
        super.l7dlog(priority, key, t);
    }
}
 
Example 9
Source File: Log4jAppenderHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
protected void doPublish(final ExtLogRecord record) {
    final Appender appender = this.appender;
    if (appender == null) {
        throw LoggingLogger.ROOT_LOGGER.handlerClosed();
    }
    final LoggingEvent event = new LoggingEvent(record, DummyCategory.of(record.getLoggerName()));
    appender.doAppend(event);
    super.doPublish(record);
}
 
Example 10
Source File: AppenderAttachableImpl.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Call the <code>doAppend</code> method on all attached appenders.
 * @param event The event to log.
 * @return The number of appenders.
 */
public int appendLoopOnAppenders(LoggingEvent event) {
    for (Appender appender : appenders.values()) {
        appender.doAppend(event);
    }
    return appenders.size();
}
 
Example 11
Source File: SchedulerStateRestLiveLogsTest.java    From scheduling with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testLiveLogs_OutputRemovedAtEachCall() throws Exception {

    String firstJobId = "42";

    String firstJobLogs = client.getLiveLogJob(sessionId, firstJobId);

    Appender appender = verifyListenAndGetAppender("42");

    assertTrue(firstJobLogs.isEmpty());

    appender.doAppend(createLoggingEvent(firstJobId, "first log"));

    firstJobLogs = client.getLiveLogJob(sessionId, firstJobId);
    assertThat(firstJobLogs, containsString("first log"));

    appender.doAppend(createLoggingEvent(firstJobId, "other log"));

    firstJobLogs = client.getLiveLogJob(sessionId, firstJobId);
    assertThat(firstJobLogs, not(containsString("first log")));
    assertThat(firstJobLogs, containsString("other log"));

    firstJobLogs = client.getLiveLogJob(sessionId, firstJobId);
    assertTrue(firstJobLogs.isEmpty());
}
 
Example 12
Source File: AsyncAppenderWithStorage.java    From scheduling with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Append stored events to the given appender.
 */
public synchronized void appendStoredEvents(Appender appender) {
    for (LoggingEvent e : this.storage) {
        appender.doAppend(e);
    }
}
 
Example 13
Source File: SchedulerStateRestLiveLogsTest.java    From scheduling with GNU Affero General Public License v3.0 3 votes vote down vote up
@Test
public void testLiveLogs_AvailableAndDelete() throws Exception {

    String firstJobId = "42";

    assertEquals(-1, client.getLiveLogJobAvailable(sessionId, "42"));

    String logs = client.getLiveLogJob(sessionId, firstJobId);

    Appender appender = verifyListenAndGetAppender("42");

    assertEquals(0, client.getLiveLogJobAvailable(sessionId, "42"));
    assertTrue(logs.isEmpty());

    appender.doAppend(createLoggingEvent(firstJobId, "first log"));

    assertEquals(1, client.getLiveLogJobAvailable(sessionId, "42"));

    assertTrue(client.deleteLiveLogJob(sessionId, "42"));
    assertEquals(-1, client.getLiveLogJobAvailable(sessionId, "42"));

    // will be lost
    appender.doAppend(createLoggingEvent(firstJobId, "second log"));

    logs = client.getLiveLogJob(sessionId, firstJobId);
    assertTrue(logs.isEmpty());

    appender.doAppend(createLoggingEvent(firstJobId, "other log"));
    appender.doAppend(createLoggingEvent(firstJobId, "more log"));

    assertEquals(2, client.getLiveLogJobAvailable(sessionId, "42"));
    logs = client.getLiveLogJob(sessionId, firstJobId);
    assertThat(logs, not(containsString("first log")));
    assertThat(logs, containsString("other log"));
    assertThat(logs, containsString("more log"));

    assertEquals(0, client.getLiveLogJobAvailable(sessionId, "42"));
    logs = client.getLiveLogJob(sessionId, firstJobId);
    assertTrue(logs.isEmpty());
}