Search in sources :

Example 11 with LoggingException

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

the class SmtpManager method sendEvents.

/**
 * Send the contents of the cyclic buffer as an e-mail message.
 * @param layout The layout for formatting the events.
 * @param appendEvent The event that triggered the send.
 */
public void sendEvents(final Layout<?> layout, final LogEvent appendEvent) {
    if (message == null) {
        connect(appendEvent);
    }
    try {
        final LogEvent[] priorEvents = removeAllBufferedEvents();
        // LOG4J-310: log appendEvent even if priorEvents is empty
        final byte[] rawBytes = formatContentToBytes(priorEvents, appendEvent, layout);
        final String contentType = layout.getContentType();
        final String encoding = getEncoding(rawBytes, contentType);
        final byte[] encodedBytes = encodeContentToBytes(rawBytes, encoding);
        final InternetHeaders headers = getHeaders(contentType, encoding);
        final MimeMultipart mp = getMimeMultipart(encodedBytes, headers);
        final String subject = data.subject.toSerializable(appendEvent);
        sendMultipartMessage(message, mp, subject);
    } catch (final MessagingException | IOException | RuntimeException e) {
        logError("Caught exception while sending e-mail notification.", e);
        throw new LoggingException("Error occurred while sending email", e);
    }
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) LoggingException(org.apache.logging.log4j.LoggingException) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) MutableLogEvent(org.apache.logging.log4j.core.impl.MutableLogEvent) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException)

Example 12 with LoggingException

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

the class AbstractRolloverStrategy method getEligibleFiles.

protected SortedMap<Integer, Path> getEligibleFiles(final String currentFile, final String path, final String logfilePattern, final boolean isAscending) {
    final TreeMap<Integer, Path> eligibleFiles = new TreeMap<>();
    final File file = new File(path);
    File parent = file.getParentFile();
    if (parent == null) {
        parent = new File(".");
    } else {
        parent.mkdirs();
    }
    if (!PATTERN_COUNTER.matcher(logfilePattern).matches()) {
        return eligibleFiles;
    }
    final Path dir = parent.toPath();
    String fileName = file.getName();
    final int suffixLength = suffixLength(fileName);
    // this fixes issues with filenames containing 'magic' regex characters
    if (suffixLength > 0) {
        fileName = Pattern.quote(fileName.substring(0, fileName.length() - suffixLength)) + ".*";
    } else {
        fileName = Pattern.quote(fileName);
    }
    // since we insert a pattern inside a regex escaped string,
    // surround it with quote characters so that (\d) is treated as a pattern and not a literal
    final String filePattern = fileName.replaceFirst("0?\\u0000", "\\\\E(0?\\\\d+)\\\\Q");
    final Pattern pattern = Pattern.compile(filePattern);
    final Path current = currentFile.length() > 0 ? new File(currentFile).toPath() : null;
    LOGGER.debug("Current file: {}", currentFile);
    try (final DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
        for (final Path entry : stream) {
            final Matcher matcher = pattern.matcher(entry.toFile().getName());
            if (matcher.matches() && !entry.equals(current)) {
                try {
                    final Integer index = Integer.parseInt(matcher.group(1));
                    eligibleFiles.put(index, entry);
                } catch (final NumberFormatException ex) {
                    LOGGER.debug("Ignoring file {} which matches pattern but the index is invalid.", entry.toFile().getName());
                }
            }
        }
    } catch (final IOException ioe) {
        throw new LoggingException("Error reading folder " + dir + " " + ioe.getMessage(), ioe);
    }
    return isAscending ? eligibleFiles : eligibleFiles.descendingMap();
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) LoggingException(org.apache.logging.log4j.LoggingException) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) TreeMap(java.util.TreeMap) File(java.io.File)

Example 13 with LoggingException

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

the class AsyncAppenderTest method exceptionTest.

static void exceptionTest(final LoggerContext context) throws InterruptedException {
    final ExtendedLogger logger = context.getLogger(AsyncAppender.class);
    final Exception parent = new IllegalStateException("Test");
    final Throwable child = new LoggingException("This is a test", parent);
    logger.error("This is a test", child);
    final ListAppender appender = context.getConfiguration().getAppender("List");
    final List<String> messages;
    try {
        messages = appender.getMessages(1, 2, TimeUnit.SECONDS);
    } finally {
        appender.clear();
    }
    assertNotNull(messages);
    assertEquals(1, messages.size());
    assertTrue(messages.get(0).contains(parent.getClass().getName()));
}
Also used : ExtendedLogger(org.apache.logging.log4j.spi.ExtendedLogger) LoggingException(org.apache.logging.log4j.LoggingException) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) LoggingException(org.apache.logging.log4j.LoggingException)

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