use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class PatternLayoutTest method testPatternSelector.
@Test
public void testPatternSelector() throws Exception {
final PatternMatch[] patterns = new PatternMatch[1];
patterns[0] = new PatternMatch("FLOW", "%d %-5p [%t]: ====== %C{1}.%M:%L %m ======%n");
final PatternSelector selector = MarkerPatternSelector.createSelector(patterns, "%d %-5p [%t]: %m%n", true, true, ctx.getConfiguration());
final PatternLayout layout = PatternLayout.newBuilder().withPatternSelector(selector).withConfiguration(ctx.getConfiguration()).build();
final LogEvent event1 = //
Log4jLogEvent.newBuilder().setLoggerName(this.getClass().getName()).setLoggerFqcn("org.apache.logging.log4j.core.layout.PatternLayoutTest$FauxLogger").setMarker(MarkerManager.getMarker("FLOW")).setLevel(//
Level.TRACE).setIncludeLocation(true).setMessage(new SimpleMessage("entry")).build();
final String result1 = new FauxLogger().formatEvent(event1, layout);
final String expectSuffix1 = String.format("====== PatternLayoutTest.testPatternSelector:248 entry ======%n");
assertTrue("Unexpected result: " + result1, result1.endsWith(expectSuffix1));
final LogEvent event2 = //
Log4jLogEvent.newBuilder().setLoggerName(this.getClass().getName()).setLoggerFqcn(//
"org.apache.logging.log4j.core.Logger").setLevel(//
Level.INFO).setMessage(new SimpleMessage("Hello, world 1!")).build();
final String result2 = new String(layout.toByteArray(event2));
final String expectSuffix2 = String.format("Hello, world 1!%n");
assertTrue("Unexpected result: " + result2, result2.endsWith(expectSuffix2));
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class PatternLayoutTest method testEqualsMarkerWithMessageSubstitution.
@Test
public void testEqualsMarkerWithMessageSubstitution() throws Exception {
// replace "[]" with the empty string
final PatternLayout layout = PatternLayout.newBuilder().withPattern("[%logger]%equals{[%marker]}{[]}{[%msg]}").withConfiguration(ctx.getConfiguration()).build();
// Not empty marker
final LogEvent event1 = //
Log4jLogEvent.newBuilder().setLoggerName(this.getClass().getName()).setLoggerFqcn(//
"org.apache.logging.log4j.core.Logger").setLevel(//
Level.INFO).setMarker(MarkerManager.getMarker("TestMarker")).setMessage(new SimpleMessage("Hello, world!")).build();
final byte[] result1 = layout.toByteArray(event1);
assertEquals("[org.apache.logging.log4j.core.layout.PatternLayoutTest][TestMarker]", new String(result1));
// empty marker
final LogEvent event2 = //
Log4jLogEvent.newBuilder().setLoggerName(this.getClass().getName()).setLoggerFqcn(//
"org.apache.logging.log4j.core.Logger").setLevel(Level.INFO).setMessage(new SimpleMessage("Hello, world!")).build();
final byte[] result2 = layout.toByteArray(event2);
assertEquals("[org.apache.logging.log4j.core.layout.PatternLayoutTest][Hello, world!]", new String(result2));
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class PatternLayoutTest method testSpecialChars.
@Test
public void testSpecialChars() throws Exception {
final PatternLayout layout = PatternLayout.newBuilder().withPattern("\\\\%level\\t%msg\\n\\t%logger\\r\\n\\f").withConfiguration(ctx.getConfiguration()).build();
final LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(this.getClass().getName()).setLoggerFqcn(//
"org.apache.logging.log4j.core.Logger").setLevel(//
Level.INFO).setMessage(new SimpleMessage("Hello, world!")).build();
assertToByteArray("\\INFO\tHello, world!\n" + "\torg.apache.logging.log4j.core.layout.PatternLayoutTest\r\n" + "\f", layout, event);
assertEncode("\\INFO\tHello, world!\n" + "\torg.apache.logging.log4j.core.layout.PatternLayoutTest\r\n" + "\f", layout, event);
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class PatternLayoutTest method testUnixTime.
@SuppressWarnings("unused")
private void testUnixTime(final String pattern) throws Exception {
final PatternLayout layout = PatternLayout.newBuilder().withPattern(pattern + " %m").withConfiguration(ctx.getConfiguration()).build();
final LogEvent event1 = //
Log4jLogEvent.newBuilder().setLoggerName(this.getClass().getName()).setLoggerFqcn(//
"org.apache.logging.log4j.core.Logger").setLevel(//
Level.INFO).setMessage(new SimpleMessage("Hello, world 1!")).build();
final byte[] result1 = layout.toByteArray(event1);
assertEquals(event1.getTimeMillis() + " Hello, world 1!", new String(result1));
// System.out.println("event1=" + event1.getMillis());
final LogEvent event2 = //
Log4jLogEvent.newBuilder().setLoggerName(this.getClass().getName()).setLoggerFqcn(//
"org.apache.logging.log4j.core.Logger").setLevel(//
Level.INFO).setMessage(new SimpleMessage("Hello, world 2!")).build();
final byte[] result2 = layout.toByteArray(event2);
assertEquals(event2.getTimeMillis() + " Hello, world 2!", new String(result2));
// System.out.println("event2=" + event2.getMillis());
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class EncodingPatternConverterTest method testReplacement.
@Test
public void testReplacement() {
final LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(//
EncodingPatternConverterTest.class.getName()).setLevel(//
Level.DEBUG).setMessage(new SimpleMessage("Test \r\n<div class=\"test\">this</div> & <div class='test'>that</div>")).build();
final StringBuilder sb = new StringBuilder();
final LoggerContext ctx = LoggerContext.getContext();
final String[] options = new String[] { "%msg" };
final EncodingPatternConverter converter = EncodingPatternConverter.newInstance(ctx.getConfiguration(), options);
assertNotNull("Error creating converter", converter);
converter.format(event, sb);
assertEquals("Test \\r\\n<div class="test">this</div> & <div class='test'>that</div>", sb.toString());
}
Aggregations