use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class Log4jLogEventTest method testBuilderCorrectlyCopiesAllEventAttributesInclContextData.
@Test
public void testBuilderCorrectlyCopiesAllEventAttributesInclContextData() {
final StringMap contextData = new SortedArrayStringMap();
contextData.putValue("A", "B");
final ContextStack contextStack = ThreadContext.getImmutableStack();
final Exception exception = new Exception("test");
final Marker marker = MarkerManager.getMarker("EVENTTEST");
final Message message = new SimpleMessage("foo");
final StackTraceElement stackTraceElement = new StackTraceElement("A", "B", "file", 123);
final String fqcn = "qualified";
final String name = "Ceci n'est pas une pipe";
final String threadName = "threadName";
final Log4jLogEvent event = //
Log4jLogEvent.newBuilder().setContextData(//
contextData).setContextStack(//
contextStack).setEndOfBatch(//
true).setIncludeLocation(//
true).setLevel(//
Level.FATAL).setLoggerFqcn(//
fqcn).setLoggerName(//
name).setMarker(//
marker).setMessage(//
message).setNanoTime(//
1234567890L).setSource(//
stackTraceElement).setThreadName(//
threadName).setThrown(//
exception).setTimeMillis(987654321L).build();
assertSame(contextData, event.getContextData());
assertSame(contextStack, event.getContextStack());
assertEquals(true, event.isEndOfBatch());
assertEquals(true, event.isIncludeLocation());
assertSame(Level.FATAL, event.getLevel());
assertSame(fqcn, event.getLoggerFqcn());
assertSame(name, event.getLoggerName());
assertSame(marker, event.getMarker());
assertSame(message, event.getMessage());
assertEquals(1234567890L, event.getNanoTime());
assertSame(stackTraceElement, event.getSource());
assertSame(threadName, event.getThreadName());
assertSame(exception, event.getThrown());
assertEquals(987654321L, event.getTimeMillis());
final LogEvent event2 = new Log4jLogEvent.Builder(event).build();
assertEquals("copy constructor builder", event2, event);
assertEquals("same hashCode", event2.hashCode(), event.hashCode());
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class Log4jLogEventTest method testBuilderCorrectlyCopiesMutableLogEvent.
@Test
public void testBuilderCorrectlyCopiesMutableLogEvent() throws Exception {
final StringMap contextData = new SortedArrayStringMap();
contextData.putValue("A", "B");
final ContextStack contextStack = ThreadContext.getImmutableStack();
final Exception exception = new Exception("test");
final Marker marker = MarkerManager.getMarker("EVENTTEST");
final Message message = new SimpleMessage("foo");
new StackTraceElement("A", "B", "file", 123);
final String fqcn = "qualified";
final String name = "Ceci n'est pas une pipe";
final String threadName = "threadName";
final MutableLogEvent event = new MutableLogEvent();
event.setContextData(contextData);
event.setContextStack(contextStack);
event.setEndOfBatch(true);
event.setIncludeLocation(true);
//event.setSource(stackTraceElement); // cannot be explicitly set
event.setLevel(Level.FATAL);
event.setLoggerFqcn(fqcn);
event.setLoggerName(name);
event.setMarker(marker);
event.setMessage(message);
event.setNanoTime(1234567890L);
event.setThreadName(threadName);
event.setThrown(exception);
event.setTimeMillis(987654321L);
assertSame(contextData, event.getContextData());
assertSame(contextStack, event.getContextStack());
assertEquals(true, event.isEndOfBatch());
assertEquals(true, event.isIncludeLocation());
assertSame(Level.FATAL, event.getLevel());
assertSame(fqcn, event.getLoggerFqcn());
assertSame(name, event.getLoggerName());
assertSame(marker, event.getMarker());
assertSame(message, event.getMessage());
assertEquals(1234567890L, event.getNanoTime());
//assertSame(stackTraceElement, event.getSource()); // don't invoke
assertSame(threadName, event.getThreadName());
assertSame(exception, event.getThrown());
assertEquals(987654321L, event.getTimeMillis());
final LogEvent e2 = new Log4jLogEvent.Builder(event).build();
assertEquals(contextData, e2.getContextData());
assertSame(contextStack, e2.getContextStack());
assertEquals(true, e2.isEndOfBatch());
assertEquals(true, e2.isIncludeLocation());
assertSame(Level.FATAL, e2.getLevel());
assertSame(fqcn, e2.getLoggerFqcn());
assertSame(name, e2.getLoggerName());
assertSame(marker, e2.getMarker());
assertSame(message, e2.getMessage());
assertEquals(1234567890L, e2.getNanoTime());
//assertSame(stackTraceElement, e2.getSource()); // don't invoke
assertSame(threadName, e2.getThreadName());
assertSame(exception, e2.getThrown());
assertEquals(987654321L, e2.getTimeMillis());
// use reflection to get value of source field in log event copy:
// invoking the getSource() method would initialize the field
final Field fieldSource = Log4jLogEvent.class.getDeclaredField("source");
fieldSource.setAccessible(true);
final Object value = fieldSource.get(e2);
assertNull("source in copy", value);
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class PatternLayoutComparisonBenchmark method createLog4j2Event.
private static LogEvent createLog4j2Event() {
final Marker marker = null;
final String fqcn = "com.mycom.myproject.mypackage.MyClass";
final Level level = Level.DEBUG;
final Message message = new SimpleMessage(STR);
final Throwable t = null;
final Map<String, String> mdc = null;
final ContextStack ndc = null;
final String threadName = null;
final StackTraceElement location = null;
final long timestamp = 12345678;
return //
Log4jLogEvent.newBuilder().setLoggerName(//
"name(ignored)").setMarker(//
marker).setLoggerFqcn(//
fqcn).setLevel(//
level).setMessage(//
message).setThrown(//
t).setContextMap(//
mdc).setContextStack(//
ndc).setThreadName(//
threadName).setSource(//
location).setTimeMillis(//
timestamp).build();
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class RingBufferLogEventTest method testGetMillisReturnsConstructorMillisForNormalMessage.
@Test
public void testGetMillisReturnsConstructorMillisForNormalMessage() {
final RingBufferLogEvent evt = new RingBufferLogEvent();
final String loggerName = null;
final Marker marker = null;
final String fqcn = null;
final Level level = null;
final Message data = null;
final Throwable t = null;
final ContextStack contextStack = null;
final String threadName = null;
final StackTraceElement location = null;
final long currentTimeMillis = 123;
final long nanoTime = 1;
evt.setValues(null, loggerName, marker, fqcn, level, data, t, (StringMap) evt.getContextData(), contextStack, -1, threadName, -1, location, currentTimeMillis, nanoTime);
assertEquals(123, evt.getTimeMillis());
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class LoggingMessageTagSupport method doEndTag.
@Override
public final int doEndTag() throws JspException {
final Log4jTaglibLogger logger = this.getLogger();
final Level level = this.getLevel();
final Marker marker = this.getMarker();
if (TagUtils.isEnabled(logger, level, marker)) {
final Object message = this.getMessage();
final Throwable exception = this.getException();
if (message instanceof Message) {
logger.logIfEnabled(FQCN, level, marker, (Message) message, exception);
} else if (message instanceof String) {
Message data;
if (this.attributes.size() > 0) {
data = logger.getMessageFactory().newMessage((String) message, this.attributes.toArray());
} else {
data = logger.getMessageFactory().newMessage((String) message);
}
logger.logIfEnabled(FQCN, level, marker, data, exception);
} else {
logger.logIfEnabled(FQCN, level, marker, logger.getMessageFactory().newMessage(message), exception);
}
}
return Tag.EVAL_PAGE;
}
Aggregations