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");
}
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");
}
}
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");
}
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();
}
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");
}
Aggregations