Search in sources :

Example 56 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class FileAppenderTest method writer.

private static void writer(final boolean locking, final int logEventCount, final String name, final boolean createOnDemand, final boolean concurrent) throws Exception {
    final Layout<String> layout = createPatternLayout();
    // @formatter:off
    final FileAppender appender = FileAppender.newBuilder().withFileName(FILE_NAME).withName("test").withImmediateFlush(false).withIgnoreExceptions(false).withLocking(locking).withBufferedIo(false).withLayout(layout).withCreateOnDemand(createOnDemand).build();
    // @formatter:on
    Assert.assertEquals(createOnDemand, appender.getManager().isCreateOnDemand());
    try {
        appender.start();
        assertTrue("Appender did not start", appender.isStarted());
        final boolean exists = Files.exists(PATH);
        final String msg = String.format("concurrent = %s, createOnDemand = %s, file exists = %s", concurrent, createOnDemand, exists);
        // If concurrent the file might have been created (or not.)
        // Can't really test createOnDemand && concurrent.
        final boolean expectFileCreated = !createOnDemand;
        if (concurrent && expectFileCreated) {
            Assert.assertTrue(msg, exists);
        } else if (expectFileCreated) {
            Assert.assertNotEquals(msg, createOnDemand, exists);
        }
        for (int i = 0; i < logEventCount; ++i) {
            final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName("TestLogger").setLoggerFqcn(FileAppenderTest.class.getName()).setLevel(Level.INFO).setMessage(new SimpleMessage("Test")).setThreadName(name).setTimeMillis(System.currentTimeMillis()).build();
            try {
                appender.append(logEvent);
                // Give up control long enough for another thread/process to occasionally do
                Thread.sleep(25);
            // something.
            } catch (final Exception ex) {
                throw ex;
            }
        }
    } finally {
        appender.stop();
    }
    assertFalse("Appender did not stop", appender.isStarted());
}
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 57 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class InMemoryAppenderTest method assertMessage.

private void assertMessage(final String string, final InMemoryAppender app, final String header) {
    final LogEvent event = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "TestLogger").setLoggerFqcn(//
    InMemoryAppenderTest.class.getName()).setLevel(//
    Level.INFO).setMessage(//
    new SimpleMessage("Test")).build();
    app.start();
    assertTrue("Appender did not start", app.isStarted());
    app.append(event);
    app.append(event);
    final String msg = app.toString();
    assertNotNull("No message", msg);
    final String expectedHeader = header == null ? "" : header;
    final String expected = expectedHeader + "Test" + Strings.LINE_SEPARATOR + "Test" + Strings.LINE_SEPARATOR;
    assertTrue("Incorrect message: " + msg, msg.equals(expected));
    app.stop();
    assertFalse("Appender did not stop", app.isStarted());
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage)

Example 58 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class Log4jLogEventTest method testBuilderCorrectlyCopiesAllEventAttributes.

@SuppressWarnings("deprecation")
@Test
public void testBuilderCorrectlyCopiesAllEventAttributes() {
    final Map<String, String> contextMap = new HashMap<>();
    contextMap.put("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().setContextMap(//
    contextMap).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();
    assertEquals(contextMap, event.getContextMap());
    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());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ReusableObjectMessage(org.apache.logging.log4j.message.ReusableObjectMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) ReusableMessage(org.apache.logging.log4j.message.ReusableMessage) Message(org.apache.logging.log4j.message.Message) HashMap(java.util.HashMap) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) IOException(java.io.IOException) Test(org.junit.Test) ClockFactoryTest(org.apache.logging.log4j.core.util.ClockFactoryTest)

Example 59 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class Log4jLogEventTest method testJavaIoSerializableWithUnknownThrowable.

// DO NOT REMOVE THIS COMMENT:
// UNCOMMENT WHEN GENERATING SERIALIZED EVENT FOR #testJavaIoSerializableWithUnknownThrowable
// public static class DeletedException extends Exception {
// private static final long serialVersionUID = 1L;
// public DeletedException(String msg) {
// super(msg);
// }
// };
@Test
public void testJavaIoSerializableWithUnknownThrowable() throws Exception {
    final String loggerName = "some.test";
    final Marker marker = null;
    final String loggerFQN = Strings.EMPTY;
    final Level level = Level.INFO;
    final Message msg = new SimpleMessage("abc");
    final String threadName = Thread.currentThread().getName();
    final String errorMessage = "OMG I've been deleted!";
    // DO NOT DELETE THIS COMMENT:
    // UNCOMMENT TO RE-GENERATE SERIALIZED EVENT WHEN UPDATING THIS TEST.
    // final Exception thrown = new DeletedException(errorMessage);
    // final Log4jLogEvent evt = new Log4jLogEvent(loggerName, marker, loggerFQN, level, msg, thrown);
    // final byte[] binary = serialize(evt);
    // String base64Str = DatatypeConverter.printBase64Binary(binary);
    // System.out.println("final String base64 = \"" + base64Str.replaceAll("\r\n", "\\\\r\\\\n\" +\r\n\"") +
    // "\";");
    final String base64 = "rO0ABXNyAD5vcmcuYXBhY2hlLmxvZ2dpbmcubG9nNGouY29yZS5pbXBsLkxvZzRqTG9nRXZlbnQkTG9nRXZlbnRQcm94eYgtmn+yXsP9AwAQWgAMaXNFbmRPZkJhdGNoWgASaXNMb2NhdGlvblJlcXVpcmVkSgAIdGhyZWFkSWRJAA50aHJlYWRQcmlvcml0eUoACnRpbWVNaWxsaXNMAAtjb250ZXh0RGF0YXQAKUxvcmcvYXBhY2hlL2xvZ2dpbmcvbG9nNGovdXRpbC9TdHJpbmdNYXA7TAAMY29udGV4dFN0YWNrdAA1TG9yZy9hcGFjaGUvbG9nZ2luZy9sb2c0ai9UaHJlYWRDb250ZXh0JENvbnRleHRTdGFjaztMAAVsZXZlbHQAIExvcmcvYXBhY2hlL2xvZ2dpbmcvbG9nNGovTGV2ZWw7TAAKbG9nZ2VyRlFDTnQAEkxqYXZhL2xhbmcvU3RyaW5nO0wACmxvZ2dlck5hbWVxAH4ABEwABm1hcmtlcnQAIUxvcmcvYXBhY2hlL2xvZ2dpbmcvbG9nNGovTWFya2VyO0wAEW1hcnNoYWxsZWRNZXNzYWdldAAbTGphdmEvcm1pL01hcnNoYWxsZWRPYmplY3Q7TAANbWVzc2FnZVN0cmluZ3EAfgAETAAGc291cmNldAAdTGphdmEvbGFuZy9TdGFja1RyYWNlRWxlbWVudDtMAAp0aHJlYWROYW1lcQB+AARMAAt0aHJvd25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zcQB+ACVzcQB+ACgBdAAManVuaXQtcnQuamFycQB+ACtzcQB+ACwAAABUcQB+ALNxAH4AtHQABG1haW5zcQB+ACVzcQB+ACgAcQB+ACtxAH4AM3NxAH4ALP////5xAH4ANXEAfgA2cQB+ADdzcQB+ACVzcQB+ACgAcQB+ACtxAH4AM3NxAH4ALAAAADlxAH4ANXEAfgA2cQB+ADtzcQB+ACVzcQB+ACgAcQB+ACtxAH4AM3NxAH4ALAAAACtxAH4AP3EAfgBAcQB+ADtzcQB+ACVzcQB+ACgAcQB+ACtxAH4AM3NxAH4ALAAAAl5xAH4ARHEAfgBFcQB+ADtzcQB+ACVzcQB+ACgBdAALaWRlYV9ydC5qYXJxAH4AK3NxAH4ALAAAAJN0AC1jb20uaW50ZWxsaWoucnQuZXhlY3V0aW9uLmFwcGxpY2F0aW9uLkFwcE1haW50AAxBcHBNYWluLmphdmFxAH4AunQAFk9NRyBJJ3ZlIGJlZW4gZGVsZXRlZCFxAH4AzXQARW9yZy5hcGFjaGUubG9nZ2luZy5sb2c0ai5jb3JlLmltcGwuTG9nNGpMb2dFdmVudFRlc3QkRGVsZXRlZEV4Y2VwdGlvbnVyADRbTG9yZy5hcGFjaGUubG9nZ2luZy5sb2c0ai5jb3JlLmltcGwuVGhyb3dhYmxlUHJveHk7+u0B4IWi6zkCAAB4cAAAAAB4";
    final byte[] binaryDecoded = Base64Converter.parseBase64Binary(base64);
    final Log4jLogEvent evt2 = deserialize(binaryDecoded);
    assertEquals(loggerFQN, evt2.getLoggerFqcn());
    assertEquals(level, evt2.getLevel());
    assertEquals(loggerName, evt2.getLoggerName());
    assertEquals(marker, evt2.getMarker());
    assertEquals(msg, evt2.getMessage());
    assertEquals(threadName, evt2.getThreadName());
    assertEquals(null, evt2.getThrown());
    assertEquals(this.getClass().getName() + "$DeletedException", evt2.getThrownProxy().getName());
    assertEquals(errorMessage, evt2.getThrownProxy().getMessage());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ReusableObjectMessage(org.apache.logging.log4j.message.ReusableObjectMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) ReusableMessage(org.apache.logging.log4j.message.ReusableMessage) Message(org.apache.logging.log4j.message.Message) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Level(org.apache.logging.log4j.Level) Marker(org.apache.logging.log4j.Marker) Test(org.junit.Test) ClockFactoryTest(org.apache.logging.log4j.core.util.ClockFactoryTest)

Example 60 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class MutableLogEventTest method testJavaIoSerializableWithThrown.

@Test
public void testJavaIoSerializableWithThrown() throws Exception {
    new InternalError("test error");
    final MutableLogEvent evt = new MutableLogEvent();
    evt.setContextData(CONTEXT_DATA);
    evt.setContextStack(STACK);
    evt.setEndOfBatch(true);
    evt.setIncludeLocation(true);
    evt.setLevel(Level.WARN);
    evt.setLoggerFqcn(getClass().getName());
    evt.setLoggerName("loggername");
    evt.setMarker(MarkerManager.getMarker("marked man"));
    //evt.setMessage(new ParameterizedMessage("message in a {}", "bottle")); // TODO ParameterizedMessage serialization
    evt.setMessage(new SimpleMessage("peace for all"));
    evt.setNanoTime(1234);
    evt.setThreadId(987);
    evt.setThreadName("ito");
    evt.setThreadPriority(9);
    evt.setThrown(new Exception());
    evt.setTimeMillis(56789);
    final byte[] binary = serialize(evt);
    final Log4jLogEvent evt2 = deserialize(binary);
    assertEquals(evt.getTimeMillis(), evt2.getTimeMillis());
    assertEquals(evt.getLoggerFqcn(), evt2.getLoggerFqcn());
    assertEquals(evt.getLevel(), evt2.getLevel());
    assertEquals(evt.getLoggerName(), evt2.getLoggerName());
    assertEquals(evt.getMarker(), evt2.getMarker());
    assertEquals(evt.getContextData(), evt2.getContextData());
    assertEquals(evt.getContextMap(), evt2.getContextMap());
    assertEquals(evt.getContextStack(), evt2.getContextStack());
    assertEquals(evt.getMessage(), evt2.getMessage());
    assertNotNull(evt2.getSource());
    assertEquals(evt.getSource(), evt2.getSource());
    assertEquals(evt.getThreadName(), evt2.getThreadName());
    assertNull(evt2.getThrown());
    assertNotNull(evt2.getThrownProxy());
    assertEquals(evt.getThrownProxy(), evt2.getThrownProxy());
    assertEquals(evt.isEndOfBatch(), evt2.isEndOfBatch());
    assertEquals(evt.isIncludeLocation(), evt2.isIncludeLocation());
    // nano time is transient in log4j log event
    assertNotEquals(evt.getNanoTime(), evt2.getNanoTime());
    assertEquals(0, evt2.getNanoTime());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) IOException(java.io.IOException) 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