Search in sources :

Example 1 with LoggingException

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");
    }
}
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 2 with LoggingException

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);
    }
}
Also used : LoggingException(org.apache.logging.log4j.LoggingException) AppenderControl(org.apache.logging.log4j.core.config.AppenderControl) LoggingException(org.apache.logging.log4j.LoggingException)

Example 3 with LoggingException

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");
    }
}
Also used : LoggingException(org.apache.logging.log4j.LoggingException) Logger(org.slf4j.Logger) Test(org.junit.Test)

Example 4 with LoggingException

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

Example 5 with LoggingException

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);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) LoggingException(org.apache.logging.log4j.LoggingException) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) IOException(java.io.IOException) LoggingException(org.apache.logging.log4j.LoggingException) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Aggregations

LoggingException (org.apache.logging.log4j.LoggingException)13 IOException (java.io.IOException)6 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)6 LogEvent (org.apache.logging.log4j.core.LogEvent)5 Test (org.junit.Test)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 MessagingException (javax.mail.MessagingException)2 InternetHeaders (javax.mail.internet.InternetHeaders)2 MimeMultipart (javax.mail.internet.MimeMultipart)2 MutableLogEvent (org.apache.logging.log4j.core.impl.MutableLogEvent)2 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)2 LockConflictException (com.sleepycat.je.LockConflictException)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1