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