Java Code Examples for org.apache.logging.log4j.core.LogEvent#setIncludeLocation()

The following examples show how to use org.apache.logging.log4j.core.LogEvent#setIncludeLocation() . 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: SystemdJournalAppenderTest.java    From log4j-systemd-journal-appender with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testAppend_DoNotLogException() {

    SystemdJournalAppender journalAppender = new SystemdJournalAppender("Journal", null, null, false, journalLibrary,
            false, false, false, false, false, false, null, null, null);

    when(message.getFormattedMessage()).thenReturn("some message");

    LogEvent event = new Log4jLogEvent.Builder() //
            .setMessage(message)//
            .setLoggerFqcn(journalAppender.getClass().getName())//
            .setThrown(new Throwable()) //
            .setLevel(Level.INFO).build();
    event.setIncludeLocation(true);

    journalAppender.append(event);

    List<Object> expectedArgs = new ArrayList<>();
    expectedArgs.add("some message");
    expectedArgs.add("PRIORITY=%d");
    expectedArgs.add(6);
    expectedArgs.add(null);

    verify(journalLibrary).sd_journal_send("MESSAGE=%s", expectedArgs.toArray());
}
 
Example 2
Source File: SystemdJournalAppenderTest.java    From log4j-systemd-journal-appender with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testAppend_LogSource() {

    SystemdJournalAppender journalAppender = new SystemdJournalAppender("Journal", null, null, false, journalLibrary,
            true, false, false, false, false, false, null, null, null);

    when(message.getFormattedMessage()).thenReturn("some message");
    LogEvent event = new Log4jLogEvent.Builder() //
            .setMessage(message)//
            .setLoggerFqcn(journalAppender.getClass().getName())//
            .setLevel(Level.INFO).build();
    event.setIncludeLocation(true);

    journalAppender.append(event);

    List<Object> expectedArgs = new ArrayList<>();
    expectedArgs.add("some message");
    expectedArgs.add("PRIORITY=%d");
    expectedArgs.add(6);
    expectedArgs.add("CODE_FILE=%s");
    expectedArgs.add("SystemdJournalAppenderTest.java");
    expectedArgs.add("CODE_FUNC=%s");
    expectedArgs.add("testAppend_LogSource");
    expectedArgs.add("CODE_LINE=%d");
    expectedArgs.add(Integer.valueOf(66));
    expectedArgs.add(null);

    verify(journalLibrary).sd_journal_send("MESSAGE=%s", expectedArgs.toArray());
}
 
Example 3
Source File: LoggerConfig.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void processLogEvent(final LogEvent event, LoggerConfigPredicate predicate) {
    event.setIncludeLocation(isIncludeLocation());
    if (predicate.allow(this)) {
        callAppenders(event);
    }
    logParent(event, predicate);
}
 
Example 4
Source File: LoghubAppender.java    From aliyun-log-log4j2-appender with Apache License 2.0 4 votes vote down vote up
@Override
public void append(LogEvent event) {
    List<LogItem> logItems = new ArrayList<LogItem>();
    LogItem item = new LogItem();
    logItems.add(item);
    item.SetTime((int) (event.getTimeMillis() / 1000));
    DateTime dateTime = new DateTime(event.getTimeMillis());
    item.PushBack("time", dateTime.toString(formatter));
    item.PushBack("level", event.getLevel().toString());
    item.PushBack("thread", event.getThreadName());

    StackTraceElement source = event.getSource();
    if (source == null && (!event.isIncludeLocation())) {
        event.setIncludeLocation(true);
        source = event.getSource();
        event.setIncludeLocation(false);
    }

    item.PushBack("location", source == null ? "Unknown(Unknown Source)" : source.toString());

    String message = event.getMessage().getFormattedMessage();
    item.PushBack("message", message);

    String throwable = getThrowableStr(event.getThrown());
    if (throwable != null) {
        item.PushBack("throwable", throwable);
    }

    if (getLayout() != null) {
        item.PushBack("log", new String(getLayout().toByteArray(event)));
    }

    Optional.ofNullable(mdcFields).ifPresent(
            f->event.getContextMap().entrySet().stream()
                    .filter(v->Arrays.stream(f.split(",")).anyMatch(i->i.equals(v.getKey())))
                    .forEach(map-> item.PushBack(map.getKey(),map.getValue()))
    );
    try {
        producer.send(this.project, this.logStore, this.topic, this.source, logItems, new LoghubAppenderCallback(LOGGER,
                this.project, this.logStore, this.topic, this.source, logItems));
    } catch (Exception e) {
        this.error(
                "Failed to send log, project=" + project
                        + ", logStore=" + logStore
                        + ", topic=" + topic
                        + ", source=" + source
                        + ", logItem=" + logItems, e);
    }
}
 
Example 5
Source File: LoggerConfigBenchmark.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
private void processLogEvent(final LogEvent event) {
    event.setIncludeLocation(isIncludeLocation());
    callAppenders(event);
    logParent(event);
}
 
Example 6
Source File: Log4jLogEventBenchmark.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public StackTraceElement getEventSource(final String loggerName) {
    final LogEvent event = Log4jLogEvent.newBuilder().setLoggerName(loggerName)
            .setLoggerFqcn(FQCN).setLevel(Level.INFO).setMessage(MESSAGE).build();
    event.setIncludeLocation(true);
    return event.getSource();
}