Search in sources :

Example 96 with SimpleMessage

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));
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 97 with SimpleMessage

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));
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 98 with SimpleMessage

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);
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 99 with SimpleMessage

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());
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage)

Example 100 with SimpleMessage

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&lt;div class=&quot;test&quot;&gt;this&lt;&#x2F;div&gt; &amp; &lt;div class=&apos;test&apos;&gt;that&lt;&#x2F;div&gt;", sb.toString());
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Aggregations

SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)130 Test (org.junit.Test)98 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)96 LogEvent (org.apache.logging.log4j.core.LogEvent)93 Message (org.apache.logging.log4j.message.Message)34 Marker (org.apache.logging.log4j.Marker)19 Level (org.apache.logging.log4j.Level)11 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)10 IOException (java.io.IOException)7 LoggerContext (org.apache.logging.log4j.core.LoggerContext)7 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)6 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)6 HashMap (java.util.HashMap)5 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectInputStream (java.io.ObjectInputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)4