Search in sources :

Example 41 with SimpleMessage

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

the class LogEventTest method testEquals.

@Test
@Disabled
public void testEquals() {
    final LogEvent event1 = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    this.getClass().getName()).setLoggerFqcn(// 
    "org.apache.logging.log4j.core.Logger").setLevel(// 
    Level.INFO).setMessage(// 
    new SimpleMessage("Hello, world!")).build();
    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 LogEvent event3 = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    this.getClass().getName()).setLoggerFqcn(// 
    "org.apache.logging.log4j.core.Logger").setLevel(// 
    Level.INFO).setMessage(// 
    new SimpleMessage("Hello, world!")).build();
    assertNotEquals(event1, event2, "Events should not be equal");
    assertEquals(event2, event3, "Events should be equal");
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 42 with SimpleMessage

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

the class LogEventTest method testSerialization.

@SuppressWarnings("BanSerializableRead")
@Test
public void testSerialization() throws Exception {
    final LogEvent event1 = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    this.getClass().getName()).setLoggerFqcn(// 
    "org.apache.logging.log4j.core.Logger").setLevel(// 
    Level.INFO).setMessage(// 
    new SimpleMessage("Hello, world!")).build();
    final Exception parent = new IllegalStateException("Test");
    final Throwable child = new LoggingException("This is a test", parent);
    final LogEvent event2 = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    this.getClass().getName()).setLoggerFqcn(// 
    "org.apache.logging.log4j.core.Logger").setLevel(// 
    Level.INFO).setMessage(// 
    new SimpleMessage("Hello, world!")).setThrown(// 
    child).build();
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(event1);
    oos.writeObject(event2);
    final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    final ObjectInputStream ois = new FilteredObjectInputStream(bais);
    try {
        ois.readObject();
    } catch (final IOException ioe) {
        fail("Exception processing event1");
    }
    try {
        ois.readObject();
    } catch (final IOException ioe) {
        fail("Exception processing event2");
    }
}
Also used : LoggingException(org.apache.logging.log4j.LoggingException) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) ByteArrayInputStream(java.io.ByteArrayInputStream) FilteredObjectInputStream(org.apache.logging.log4j.util.FilteredObjectInputStream) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) LoggingException(org.apache.logging.log4j.LoggingException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream) FilteredObjectInputStream(org.apache.logging.log4j.util.FilteredObjectInputStream) Test(org.junit.jupiter.api.Test)

Example 43 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(app.isStarted(), "Appender did not start");
    app.append(event);
    app.append(event);
    final String msg = app.toString();
    assertNotNull(msg, "No message");
    final String expectedHeader = header == null ? "" : header;
    final String expected = expectedHeader + "Test" + Strings.LINE_SEPARATOR + "Test" + Strings.LINE_SEPARATOR;
    assertEquals(expected, msg, "Incorrect message: " + msg);
    app.stop();
    assertFalse(app.isStarted(), "Appender did not stop");
}
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 44 with SimpleMessage

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

the class ConsoleAppenderTest method testConsoleStreamManagerDoesNotClose.

private void testConsoleStreamManagerDoesNotClose(final PrintStream ps, final Target targetName, final SystemSetter systemSetter) {
    try {
        systemSetter.systemSet(psMock);
        final Layout<String> layout = PatternLayout.newBuilder().setAlwaysWriteExceptions(true).build();
        final ConsoleAppender app = ConsoleAppender.newBuilder().setLayout(layout).setTarget(targetName).setName("Console").setIgnoreExceptions(false).build();
        app.start();
        assertTrue(app.isStarted(), "Appender did not start");
        final LogEvent event = // 
        Log4jLogEvent.newBuilder().setLoggerName(// 
        "TestLogger").setLoggerFqcn(// 
        ConsoleAppenderTest.class.getName()).setLevel(// 
        Level.INFO).setMessage(// 
        new SimpleMessage("Test")).build();
        app.append(event);
        app.stop();
        assertFalse(app.isStarted(), "Appender did not stop");
    } finally {
        systemSetter.systemSet(ps);
    }
    then(psMock).should(atLeastOnce()).write(any(byte[].class), anyInt(), anyInt());
    then(psMock).should(atLeastOnce()).flush();
}
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 45 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().setFileName(FILE_NAME).setName("test").setImmediateFlush(false).setIgnoreExceptions(false).setLocking(locking).setBufferedIo(false).setLayout(layout).setCreateOnDemand(createOnDemand).build();
    // @formatter:on
    assertEquals(createOnDemand, appender.getManager().isCreateOnDemand());
    try {
        appender.start();
        assertTrue(appender.isStarted(), "Appender did not start");
        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) {
            assertTrue(exists, msg);
        } else if (expectFileCreated) {
            assertNotEquals(createOnDemand, exists, msg);
        }
        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();
            appender.append(logEvent);
            // Give up control long enough for another thread/process to occasionally do something.
            Thread.sleep(25);
        }
    } finally {
        appender.stop();
    }
    assertFalse(appender.isStarted(), "Appender did not stop");
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage)

Aggregations

SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)198 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)148 LogEvent (org.apache.logging.log4j.core.LogEvent)142 Test (org.junit.jupiter.api.Test)128 Message (org.apache.logging.log4j.message.Message)43 Marker (org.apache.logging.log4j.Marker)23 Test (org.junit.Test)21 StringMap (org.apache.logging.log4j.util.StringMap)20 Level (org.apache.logging.log4j.Level)17 IOException (java.io.IOException)13 SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)12 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)11 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)10 LoggerContext (org.apache.logging.log4j.core.LoggerContext)10 ClockFactoryTest (org.apache.logging.log4j.core.time.ClockFactoryTest)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4