use of org.apache.logging.log4j.LoggingException in project logging-log4j2 by apache.
the class AbstractRolloverStrategy method getEligibleFiles.
protected SortedMap<Integer, Path> getEligibleFiles(String path, String logfilePattern, boolean isAscending) {
TreeMap<Integer, Path> eligibleFiles = new TreeMap<>();
File file = new File(path);
File parent = file.getParentFile();
if (parent == null) {
parent = new File(".");
} else {
parent.mkdirs();
}
if (!logfilePattern.contains("%i")) {
return eligibleFiles;
}
Path dir = parent.toPath();
String fileName = file.getName();
int suffixLength = suffixLength(fileName);
if (suffixLength > 0) {
fileName = fileName.substring(0, fileName.length() - suffixLength) + ".*";
}
String filePattern = fileName.replace(NotANumber.VALUE, "(\\d+)");
Pattern pattern = Pattern.compile(filePattern);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path entry : stream) {
Matcher matcher = pattern.matcher(entry.toFile().getName());
if (matcher.matches()) {
Integer index = Integer.parseInt(matcher.group(1));
eligibleFiles.put(index, entry);
}
}
} catch (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 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);
}
}
use of org.apache.logging.log4j.LoggingException in project logging-log4j2 by apache.
the class SerializedLayoutTest method testSerialization.
@Test
public void testSerialization() throws Exception {
final SerializedLayout layout = SerializedLayout.createLayout();
final Throwable throwable = new LoggingException("Test");
final LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(//
this.getClass().getName()).setLoggerFqcn(//
"org.apache.logging.log4j.core.Logger").setLevel(//
Level.INFO).setMessage(//
new SimpleMessage("Hello, world!")).setThrown(//
throwable).build();
final byte[] result = layout.toByteArray(event);
assertNotNull(result);
final FileOutputStream fos = new FileOutputStream(DAT_PATH);
fos.write(layout.getHeader());
fos.write(result);
fos.close();
}
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 = buffer.removeAll();
// 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);
sendMultipartMessage(message, mp);
} 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 AsyncAppenderTest method testException.
@Test
public void testException() throws Exception {
final Logger logger = LogManager.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 long timeoutMillis = TIMEOUT_MILLIS;
final TimeUnit timeUnit = TimeUnit.MILLISECONDS;
final List<String> list = listAppender.getMessages(1, timeoutMillis, timeUnit);
assertNotNull("No events generated", list);
assertTrue("Incorrect number of events after " + timeoutMillis + " " + timeUnit + ". Expected 1, got " + list.size(), list.size() == 1);
final String msg = list.get(0);
assertTrue("No parent exception", msg.contains("java.lang.IllegalStateException"));
}
Aggregations