use of org.apache.logging.log4j.LoggingException 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.LoggingException in project logging-log4j2 by apache.
the class FailoverAppender method failover.
private void failover(final LogEvent event, final Exception ex) {
final RuntimeException re = ex != null ? (ex instanceof LoggingException ? (LoggingException) ex : new LoggingException(ex)) : null;
boolean written = false;
Exception failoverException = null;
for (final AppenderControl control : failoverAppenders) {
try {
control.callAppender(event);
written = true;
break;
} catch (final Exception fex) {
if (failoverException == null) {
failoverException = fex;
}
}
}
if (!written && !ignoreExceptions()) {
if (re != null) {
throw re;
}
throw new LoggingException("Unable to write to failover appenders", failoverException);
}
}
use of org.apache.logging.log4j.LoggingException in project logging-log4j2 by apache.
the class OverflowTest method log.
@Test
public void log() {
try {
final Logger logger = LoggerFactory.getLogger(OverflowTest.class);
fail("Failed to detect inclusion of log4j-to-slf4j");
} catch (LoggingException ex) {
// Expected exception.
} catch (StackOverflowError error) {
fail("Failed to detect inclusion of log4j-to-slf4j, caught StackOverflowError");
}
}
use of org.apache.logging.log4j.LoggingException in project logging-log4j2 by apache.
the class SocketAppenderTest method testTcpAppender.
static void testTcpAppender(final TcpSocketTestServer tcpTestServer, final Logger logger, final int bufferSize) throws Exception {
// @formatter:off
final SocketAppender appender = SocketAppender.newBuilder().setHost("localhost").setPort(tcpTestServer.getLocalPort()).setReconnectDelayMillis(-1).setName("test").setImmediateFail(false).setBufferSize(bufferSize).setLayout(JsonLayout.newBuilder().setProperties(true).build()).build();
// @formatter:on
appender.start();
Assert.assertEquals(bufferSize, appender.getManager().getByteBuffer().capacity());
// set appender on root and set level to debug
logger.addAppender(appender);
logger.setAdditive(false);
logger.setLevel(Level.DEBUG);
final String tcKey = "UUID";
final String expectedUuidStr = UUID.randomUUID().toString();
ThreadContext.put(tcKey, expectedUuidStr);
ThreadContext.push(expectedUuidStr);
final String expectedExMsg = "This is a test";
try {
logger.debug("This is a test message");
final Throwable child = new LoggingException(expectedExMsg);
logger.error("Throwing an exception", child);
logger.debug("This is another test message");
} finally {
ThreadContext.remove(tcKey);
ThreadContext.pop();
}
Thread.sleep(250);
LogEvent event = tcpTestServer.getQueue().poll(3, TimeUnit.SECONDS);
assertNotNull("No event retrieved", event);
assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("This is a test message"));
assertTrue("Message not delivered via TCP", tcpTestServer.getCount() > 0);
assertEquals(expectedUuidStr, event.getContextData().getValue(tcKey));
event = tcpTestServer.getQueue().poll(3, TimeUnit.SECONDS);
assertNotNull("No event retrieved", event);
assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("Throwing an exception"));
assertTrue("Message not delivered via TCP", tcpTestServer.getCount() > 1);
assertEquals(expectedUuidStr, event.getContextStack().pop());
assertNotNull(event.getThrownProxy());
assertEquals(expectedExMsg, event.getThrownProxy().getMessage());
}
use of org.apache.logging.log4j.LoggingException in project logging-log4j2 by apache.
the class SerializedLayoutTest method testLayout.
/**
* Test case for MDC conversion pattern.
*/
@Test
public void testLayout() throws Exception {
final Map<String, Appender> appenders = root.getAppenders();
for (final Appender appender : appenders.values()) {
root.removeAppender(appender);
}
// set up appender
final SerializedLayout layout = SerializedLayout.createLayout();
final ListAppender appender = new ListAppender("List", null, layout, false, true);
appender.start();
// set appender on root and set level to debug
root.addAppender(appender);
root.setLevel(Level.DEBUG);
// output starting message
root.debug("starting mdc pattern test");
root.debug("empty mdc");
ThreadContext.put("key1", "value1");
ThreadContext.put("key2", "value2");
root.debug("filled mdc");
ThreadContext.remove("key1");
ThreadContext.remove("key2");
root.error("finished mdc pattern test", new NullPointerException("test"));
final Exception parent = new IllegalStateException("Test");
final Throwable child = new LoggingException("This is a test", parent);
root.error("Throwing an exception", child);
appender.stop();
final List<byte[]> data = appender.getData();
assertTrue(data.size() > 0);
int i = 0;
for (final byte[] item : data) {
final ByteArrayInputStream bais = new ByteArrayInputStream(item);
final ObjectInputStream ois = new ObjectInputStream(bais);
LogEvent event;
try {
event = (LogEvent) ois.readObject();
} catch (final IOException ioe) {
System.err.println("Exception processing item " + i);
throw ioe;
}
assertTrue("Incorrect event", event.toString().equals(expected[i]));
++i;
}
for (final Appender app : appenders.values()) {
root.addAppender(app);
}
}
Aggregations