Java Code Examples for org.apache.logging.log4j.message.Message#getParameters()

The following examples show how to use org.apache.logging.log4j.message.Message#getParameters() . 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: TestLogger.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
protected void log(final Level level, final Marker marker, final String fqcn, final StackTraceElement location,
        final Message message, final Throwable throwable) {
    final StringBuilder sb = new StringBuilder();
    if (marker != null) {
        sb.append(marker);
    }
    sb.append(' ');
    sb.append(level.toString());
    sb.append(' ');
    if (location != null) {
        sb.append(location.toString());
        sb.append(' ');
    }
    sb.append(message.getFormattedMessage());
    final Map<String, String> mdc = ThreadContext.getImmutableContext();
    if (mdc.size() > 0) {
        sb.append(' ');
        sb.append(mdc.toString());
        sb.append(' ');
    }
    final Object[] params = message.getParameters();
    Throwable t;
    if (throwable == null && params != null && params.length > 0 && params[params.length - 1] instanceof Throwable) {
        t = (Throwable) params[params.length - 1];
    } else {
        t = throwable;
    }
    if (t != null) {
        sb.append(' ');
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        t.printStackTrace(new PrintStream(baos));
        sb.append(baos.toString());
    }
    list.add(sb.toString());
}
 
Example 2
Source File: CsvParameterLayout.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public String toSerializable(final LogEvent event) {
    final Message message = event.getMessage();
    final Object[] parameters = message.getParameters();
    final StringBuilder buffer = getStringBuilder();
    try {
        getFormat().printRecord(buffer, parameters);
        return buffer.toString();
    } catch (final IOException e) {
        StatusLogger.getLogger().error(message, e);
        return getFormat().getCommentMarker() + " " + e;
    }
}
 
Example 3
Source File: CategoryTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Tests Category.forcedLog.
 */
@Test
@SuppressWarnings("deprecation")
public void testForcedLog() {
    final MockCategory category = new MockCategory("org.example.foo");
    category.setAdditivity(false);
    ((org.apache.logging.log4j.core.Logger) category.getLogger()).addAppender(appender);
    category.info("Hello, World");
    final List<LogEvent> list = appender.getEvents();
    int events = list.size();
    assertTrue("Number of events should be 1, was " + events, events == 1);
    LogEvent event = list.get(0);
    Message msg = event.getMessage();
    assertNotNull("No message", msg);
    assertTrue("Incorrect Message type", msg instanceof ObjectMessage);
    Object[] objects = msg.getParameters();
    assertTrue("Incorrect Object type", objects[0] instanceof String);
    appender.clear();
    category.log(Priority.INFO, "Hello, World");
    events = list.size();
    assertTrue("Number of events should be 1, was " + events, events == 1);
    event = list.get(0);
    msg = event.getMessage();
    assertNotNull("No message", msg);
    assertTrue("Incorrect Message type", msg instanceof ObjectMessage);
    objects = msg.getParameters();
    assertTrue("Incorrect Object type", objects[0] instanceof String);
    appender.clear();
}
 
Example 4
Source File: CategoryTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Tests Category.forcedLog.
 */
@Test
@SuppressWarnings("deprecation")
public void testForcedLog() {
    final MockCategory category = new MockCategory("org.example.foo");
    category.setAdditivity(false);
    ((org.apache.logging.log4j.core.Logger) category.getLogger()).addAppender(appender);
    category.info("Hello, World");
    final List<LogEvent> list = appender.getEvents();
    int events = list.size();
    assertTrue("Number of events should be 1, was " + events, events == 1);
    LogEvent event = list.get(0);
    Message msg = event.getMessage();
    assertNotNull("No message", msg);
    assertTrue("Incorrect Message type", msg instanceof ObjectMessage);
    Object[] objects = msg.getParameters();
    assertTrue("Incorrect Object type", objects[0] instanceof String);
    appender.clear();
    category.log(Priority.INFO, "Hello, World");
    events = list.size();
    assertTrue("Number of events should be 1, was " + events, events == 1);
    event = list.get(0);
    msg = event.getMessage();
    assertNotNull("No message", msg);
    assertTrue("Incorrect Message type", msg instanceof ObjectMessage);
    objects = msg.getParameters();
    assertTrue("Incorrect Object type", objects[0] instanceof String);
    appender.clear();
}
 
Example 5
Source File: CustomFieldsConverter.java    From cf-java-logging-support with Apache License 2.0 4 votes vote down vote up
private Object[] getMessageParameters(LogEvent event) {
	Message message = event.getMessage();
	return message == null ? null : message.getParameters();
}
 
Example 6
Source File: MaskingRewritePolicy.java    From owasp-security-logging with Apache License 2.0 4 votes vote down vote up
/**
 * Rewrite the event.
 *
 * @param source a logging event that may be returned or used to create a
 * new logging event.
 * @return The LogEvent after rewriting.
 */
@Override
public LogEvent rewrite(LogEvent source) {
	// get the markers for the log event. If no markers, nothing can be
	// tagged confidential and we can return
	Marker sourceMarker = source.getMarker();
	if (sourceMarker == null) {
		return source;
	}

	// get the message. If no message we can return
	final Message msg = source.getMessage();
	if (msg == null || !(msg instanceof ParameterizedMessage)) {
		return source;
	}

	// get the parameters. If no params we can return
	Object[] params = msg.getParameters();
	if (params == null || params.length == 0) {
		return source;
	}

	// check if this event is actually marked as confidential. If not,
	// return
	Log4jMarker eventMarker = new Log4jMarker(sourceMarker);
	if (!eventMarker.contains(SecurityMarkers.CONFIDENTIAL)) {
		return source;
	}

	// we have a message, parameters, a marker, and it is confidential.
	// Process
	for (int i = 0; i < params.length; i++) {
		params[i] = MASKED_PASSWORD;
	}

	// create new message
	Message outMessage = new ParameterizedMessage(msg.getFormat(), params,
			msg.getThrowable());

	// build new log event for output
	LogEvent output = new Log4jLogEvent.Builder(source)
			.setMessage(outMessage).build();

	return output;
}
 
Example 7
Source File: SimpleLogger.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public void logMessage(final String fqcn, final Level mgsLevel, final Marker marker, final Message msg,
        final Throwable throwable) {
    final StringBuilder sb = new StringBuilder();
    // Append date-time if so configured
    if (showDateTime) {
        final Date now = new Date();
        String dateText;
        synchronized (dateFormatter) {
            dateText = dateFormatter.format(now);
        }
        sb.append(dateText);
        sb.append(SPACE);
    }

    sb.append(mgsLevel.toString());
    sb.append(SPACE);
    if (Strings.isNotEmpty(logName)) {
        sb.append(logName);
        sb.append(SPACE);
    }
    sb.append(msg.getFormattedMessage());
    if (showContextMap) {
        final Map<String, String> mdc = ThreadContext.getImmutableContext();
        if (mdc.size() > 0) {
            sb.append(SPACE);
            sb.append(mdc.toString());
            sb.append(SPACE);
        }
    }
    final Object[] params = msg.getParameters();
    Throwable t;
    if (throwable == null && params != null && params.length > 0
            && params[params.length - 1] instanceof Throwable) {
        t = (Throwable) params[params.length - 1];
    } else {
        t = throwable;
    }
    stream.println(sb.toString());
    if (t != null) {
        stream.print(SPACE);
        t.printStackTrace(stream);
    }
}