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());
assertTrue(event.isEndOfBatch());
assertTrue(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());
assertTrue(e2.isEndOfBatch());
assertTrue(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(value, "source in copy");
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class MessageAttributeConverterTest method testConvert01.
@Test
public void testConvert01() {
final Message message = LOGGER.getMessageFactory().newMessage("Message #{} said [{}].", 3, "Hello");
final String converted = this.converter.convertToDatabaseColumn(message);
assertNotNull("The converted value should not be null.", converted);
assertEquals("The converted value is not correct.", "Message #3 said [Hello].", converted);
final Message reversed = this.converter.convertToEntityAttribute(converted);
assertNotNull("The reversed value should not be null.", reversed);
assertEquals("The reversed value is not correct.", "Message #3 said [Hello].", reversed.getFormattedMessage());
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class LevelTest method testLevelLogging.
@Test
public void testLevelLogging(final LoggerContext context) {
final Marker marker = MarkerManager.getMarker("marker");
final Message msg = new ObjectMessage("msg");
final Throwable t = new Throwable("test");
final Level[] levels = new Level[] { Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR, Level.FATAL };
final String[] names = new String[] { "levelTest", "levelTest.Trace", "levelTest.Debug", "levelTest.Info", "levelTest.Warn", "levelTest.Error", "levelTest.Fatal" };
for (final Level level : levels) {
for (final String name : names) {
final Logger logger = context.getLogger(name);
// Message
logger.log(level, msg);
// Object
logger.log(level, 123);
// String
logger.log(level, name);
// Marker, Message
logger.log(level, marker, msg);
// Marker, Object
logger.log(level, marker, 123);
// marker, String
logger.log(level, marker, name);
// Message, Throwable
logger.log(level, msg, t);
// Object, Throwable
logger.log(level, 123, t);
// String, Object...
logger.log(level, name, "param1", "param2");
// String, Throwable
logger.log(level, name, t);
// Marker, Message, Throwable
logger.log(level, marker, msg, t);
// Marker, Object, Throwable
logger.log(level, marker, 123, t);
// Marker, String, Object...
logger.log(level, marker, name, "param1", "param2");
// Marker, String, Throwable
logger.log(level, marker, name, t);
}
}
// Logger "levelTest" will not receive same events as "levelTest.Trace"
int levelCount = names.length - 1;
final int UNIT = 14;
final Expected[] expectedResults = new Expected[] { //
new Expected(listAll, UNIT * levelCount, "TRACE", "All"), //
new Expected(listTrace, UNIT * levelCount--, "TRACE", "Trace"), //
new Expected(listDebug, UNIT * levelCount--, "DEBUG", "Debug"), //
new Expected(listInfo, UNIT * levelCount--, "INFO", "Info"), //
new Expected(listWarn, UNIT * levelCount--, "WARN", "Warn"), //
new Expected(listError, UNIT * levelCount--, "ERROR", "Error"), //
new Expected(listFatal, UNIT * levelCount--, "FATAL", "Fatal") };
for (final Expected expected : expectedResults) {
final String description = expected.description;
final List<LogEvent> events = expected.appender.getEvents();
assertNotNull(events, description + ": No events");
assertThat(events, hasSize(expected.expectedEventCount));
final LogEvent event = events.get(0);
assertEquals(event.getLevel().name(), expected.expectedInitialEventLevel, description + ": Expected level " + expected.expectedInitialEventLevel + ", got" + event.getLevel());
}
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class LevelPatternConverterTest method testLevelMapWithLengthAndLowerCase.
@Test
public void testLevelMapWithLengthAndLowerCase() {
final Message msg = new SimpleMessage("Hello");
LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(//
"MyLogger").setLevel(//
Level.DEBUG).setMessage(msg).build();
final StringBuilder sb = new StringBuilder();
LevelPatternConverter converter = LevelPatternConverter.newInstance(null);
converter.format(event, sb);
assertEquals(Level.DEBUG.toString(), sb.toString());
final String[] opts = new String[] { "WARN=Warning, length=2, lowerCase=true" };
converter = LevelPatternConverter.newInstance(opts);
sb.setLength(0);
converter.format(event, sb);
assertEquals("de", sb.toString());
event = //
Log4jLogEvent.newBuilder().setLoggerName(//
"MyLogger").setLevel(//
Level.WARN).setMessage(msg).build();
sb.setLength(0);
converter.format(event, sb);
assertEquals("Warning", sb.toString());
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class LevelPatternConverterTest method testLevelLength.
private void testLevelLength(final int length, final String debug, final String warn) {
final Message msg = new SimpleMessage("Hello");
LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(//
"MyLogger").setLevel(//
Level.DEBUG).setMessage(msg).build();
final StringBuilder sb = new StringBuilder();
LevelPatternConverter converter = LevelPatternConverter.newInstance(null);
converter.format(event, sb);
assertEquals(Level.DEBUG.toString(), sb.toString());
final String[] opts = new String[] { "length=" + length };
converter = LevelPatternConverter.newInstance(opts);
sb.setLength(0);
converter.format(event, sb);
assertEquals(debug, sb.toString());
event = //
Log4jLogEvent.newBuilder().setLoggerName(//
"MyLogger").setLevel(//
Level.WARN).setMessage(msg).build();
sb.setLength(0);
converter.format(event, sb);
assertEquals(warn, sb.toString());
}
Aggregations