diff --git a/.gitignore b/.gitignore
index ea3f31b1..42b9c323 100644
--- a/.gitignore
+++ b/.gitignore
@@ -112,7 +112,7 @@ fabric.properties
### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
-# *.iml
+*.iml
# modules.xml
# .idea/misc.xml
# *.ipr
diff --git a/lite/pom.xml b/lite/pom.xml
index 4909aae7..81497766 100644
--- a/lite/pom.xml
+++ b/lite/pom.xml
@@ -36,7 +36,7 @@
UTF-8
2.0.2-SNAPSHOT
2.0.0-SNAPSHOT
- 5.5.2
+ 5.8.1
@@ -110,6 +110,23 @@
3.5
test
+
+ org.slf4j
+ slf4j-api
+ 1.7.25
+
+
+ org.apache.logging.log4j
+ log4j-slf4j-impl
+ 2.9.1
+
+
+ org.apache.logging.log4j
+ log4j-core
+ 2.9.1
+ tests
+ test
+
@@ -278,7 +295,7 @@
org.apache.maven.plugins
maven-pmd-plugin
- 3.7
+ 3.8
@@ -363,7 +380,7 @@
org.codehaus.mojo
animal-sniffer-maven-plugin
- 1.14
+ 1.16
check-java-version
@@ -570,6 +587,8 @@
.settings/org.eclipse.m2e.core.prefs
*.md
+
+ src/**/log4j2*.xml
fix_throws_javadoc.sed
fix_throws_javadoc.sh
diff --git a/pdf-java-toolkit-samples.iml b/pdf-java-toolkit-samples.iml
deleted file mode 100644
index 295f208d..00000000
--- a/pdf-java-toolkit-samples.iml
+++ /dev/null
@@ -1,65 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 0ce6b543..67005573 100644
--- a/pom.xml
+++ b/pom.xml
@@ -37,7 +37,7 @@
UTF-8
2.1.0-SNAPSHOT
2.0.0-SNAPSHOT
- 5.5.2
+ 5.8.1
@@ -111,6 +111,23 @@
3.5
test
+
+ org.slf4j
+ slf4j-api
+ 1.7.25
+
+
+ org.apache.logging.log4j
+ log4j-slf4j-impl
+ 2.9.1
+
+
+ org.apache.logging.log4j
+ log4j-core
+ 2.9.1
+ tests
+ test
+
@@ -278,7 +295,7 @@
org.apache.maven.plugins
maven-pmd-plugin
- 3.7
+ 3.8
@@ -382,7 +399,7 @@
org.codehaus.mojo
animal-sniffer-maven-plugin
- 1.14
+ 1.16
check-java-version
diff --git a/src/main/java/com/datalogics/pdf/samples/extraction/TextExtract.java b/src/main/java/com/datalogics/pdf/samples/extraction/TextExtract.java
index 3ebda85c..f682ee4e 100644
--- a/src/main/java/com/datalogics/pdf/samples/extraction/TextExtract.java
+++ b/src/main/java/com/datalogics/pdf/samples/extraction/TextExtract.java
@@ -20,22 +20,24 @@
import com.datalogics.pdf.samples.util.FontUtils;
import com.datalogics.pdf.samples.util.IoUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
+import java.lang.invoke.MethodHandles;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
-import java.util.logging.Level;
-import java.util.logging.Logger;
/**
* This sample demonstrates how to extract text from a document. The text is extracted in reading order and then saved
* to a text file.
*/
public final class TextExtract {
- private static final Logger LOGGER = Logger.getLogger(TextExtract.class.getName());
+ private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static final String INPUT_PDF_PATH = "/com/datalogics/pdf/samples/pdfjavatoolkit-ds.pdf";
public static final String OUTPUT_TEXT_PATH = "TextExtract.txt";
@@ -105,7 +107,7 @@ public static void extractTextReadingOrder(final URL inputUrl, final URL outputU
} while (wordsIter.hasNext());
}
} else {
- if (LOGGER.isLoggable(Level.INFO)) {
+ if (LOGGER.isInfoEnabled()) {
LOGGER.info(inputUrl.toURI().getPath() + " did not have any text to extract.");
}
}
diff --git a/src/main/java/com/datalogics/pdf/samples/forms/ExportFormDataToCsv.java b/src/main/java/com/datalogics/pdf/samples/forms/ExportFormDataToCsv.java
index 39dcc9fd..16b58ae6 100644
--- a/src/main/java/com/datalogics/pdf/samples/forms/ExportFormDataToCsv.java
+++ b/src/main/java/com/datalogics/pdf/samples/forms/ExportFormDataToCsv.java
@@ -18,17 +18,18 @@
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
+import java.lang.invoke.MethodHandles;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Iterator;
import java.util.List;
-import java.util.logging.Level;
-import java.util.logging.Logger;
/**
* This sample demonstrates exporting data from PDF form fields with formatting to a CSV file.
@@ -37,7 +38,7 @@ public final class ExportFormDataToCsv {
public static final String DEFAULT_INPUT = "filled_acroform.pdf";
public static final String CSV_OUTPUT = "exported-form-data.csv";
- private static final Logger LOGGER = Logger.getLogger(ExportFormDataToCsv.class.getName());
+ private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
/**
* This is a utility class, and won't be instantiated.
@@ -160,17 +161,17 @@ private static void exportFieldValues(final PDFInteractiveForm form, final CSVPr
final List> value = field.getValueList();
if (value == null) {
printer.print("");
- if (LOGGER.isLoggable(Level.WARNING)) {
- LOGGER.warning(field.getQualifiedName() + " has no value!");
+ if (LOGGER.isWarnEnabled()) {
+ LOGGER.warn(field.getQualifiedName() + " has no value!");
}
} else {
// If the list of values is not empty, write out only the first one.
// For more complex forms, the field type should be inspected to
// determine how to handle extracting the values.
printer.print(value.get(0).toString());
- if (LOGGER.isLoggable(Level.WARNING)) {
- LOGGER.warning(field.getQualifiedName()
- + " has no formatting rules! Writing out non formatted value!");
+ if (LOGGER.isWarnEnabled()) {
+ LOGGER.warn(field.getQualifiedName()
+ + " has no formatting rules! Writing out non formatted value!");
}
}
} else {
diff --git a/src/main/java/com/datalogics/pdf/samples/manipulation/ConvertPdfDocument.java b/src/main/java/com/datalogics/pdf/samples/manipulation/ConvertPdfDocument.java
index 7d9da998..d7137761 100644
--- a/src/main/java/com/datalogics/pdf/samples/manipulation/ConvertPdfDocument.java
+++ b/src/main/java/com/datalogics/pdf/samples/manipulation/ConvertPdfDocument.java
@@ -27,9 +27,12 @@
import com.datalogics.pdf.samples.util.DocumentUtils;
import com.datalogics.pdf.samples.util.IoUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import java.io.IOException;
+import java.lang.invoke.MethodHandles;
import java.net.URL;
-import java.util.logging.Logger;
/**
@@ -44,7 +47,7 @@
*/
public final class ConvertPdfDocument {
- private static final Logger LOGGER = Logger.getLogger(ConvertPdfDocument.class.getName());
+ private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static final String INPUT_UNCONVERTED_PDF_PATH = "UnConvertedPdf.pdf";
public static final String OUTPUT_CONVERTED_PDF_PATH = "ConvertedPdfa-1b.pdf";
diff --git a/src/main/java/com/datalogics/pdf/samples/manipulation/ConvertToPdfA2.java b/src/main/java/com/datalogics/pdf/samples/manipulation/ConvertToPdfA2.java
index 1981cbc9..7d9cd641 100644
--- a/src/main/java/com/datalogics/pdf/samples/manipulation/ConvertToPdfA2.java
+++ b/src/main/java/com/datalogics/pdf/samples/manipulation/ConvertToPdfA2.java
@@ -29,15 +29,18 @@
import com.datalogics.pdf.samples.util.DocumentUtils;
import com.datalogics.pdf.samples.util.IoUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import java.io.IOException;
+import java.lang.invoke.MethodHandles;
import java.net.URL;
-import java.util.logging.Logger;
/**
* This sample demonstrates how to convert a PDF to a PDF/A-2 archive file. (PDF/A-2b by default)
*/
public final class ConvertToPdfA2 {
- private static final Logger LOGGER = Logger.getLogger(ConvertToPdfA2.class.getName());
+ private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static final String INPUT_PDF_PATH = "UnconvertedPdfA2.pdf";
public static final String OUTPUT_PDF_PATH = "ConvertedToPdfA2.pdf";
diff --git a/src/main/java/com/datalogics/pdf/samples/manipulation/RedactAndSanitizeDocument.java b/src/main/java/com/datalogics/pdf/samples/manipulation/RedactAndSanitizeDocument.java
index 0321d331..e9db783d 100644
--- a/src/main/java/com/datalogics/pdf/samples/manipulation/RedactAndSanitizeDocument.java
+++ b/src/main/java/com/datalogics/pdf/samples/manipulation/RedactAndSanitizeDocument.java
@@ -45,11 +45,14 @@
import com.datalogics.pdf.samples.util.DocumentUtils;
import com.datalogics.pdf.samples.util.IoUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import java.io.IOException;
+import java.lang.invoke.MethodHandles;
import java.net.URL;
import java.util.EnumSet;
import java.util.Locale;
-import java.util.logging.Logger;
/**
* This sample demonstrates how to open a document, add redaction annotations to it, and then apply the redaction. The
@@ -95,7 +98,7 @@
*
*/
public final class RedactAndSanitizeDocument {
- private static final Logger LOGGER = Logger.getLogger(RedactAndSanitizeDocument.class.getName());
+ private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static final String SEARCH_PDF_STRING = "Reader";
public static final String INPUT_PDF_PATH = "pdfjavatoolkit-ds.pdf";
@@ -314,7 +317,7 @@ public static void sanitizeDocument(final PDFDocument document, final URL saniti
PDFConfigurationException, PDFInvalidParameterException, PDFUnableToCompleteOperationException {
if (!canSanitizeDocument(document)) {
- LOGGER.warning("The document was not sanitized");
+ LOGGER.warn("The document was not sanitized");
return;
}
ByteWriter writer = null;
diff --git a/src/main/java/com/datalogics/pdf/samples/printing/PrintPdf.java b/src/main/java/com/datalogics/pdf/samples/printing/PrintPdf.java
index 18c8abb8..119c2f87 100644
--- a/src/main/java/com/datalogics/pdf/samples/printing/PrintPdf.java
+++ b/src/main/java/com/datalogics/pdf/samples/printing/PrintPdf.java
@@ -19,6 +19,9 @@
import com.datalogics.pdf.samples.util.DocumentUtils;
import com.datalogics.pdf.samples.util.IoUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
@@ -30,9 +33,8 @@
import java.awt.print.PrinterIOException;
import java.awt.print.PrinterJob;
import java.io.IOException;
+import java.lang.invoke.MethodHandles;
import java.net.URL;
-import java.util.logging.Level;
-import java.util.logging.Logger;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
@@ -49,7 +51,7 @@
*/
public class PrintPdf {
- private static final Logger LOGGER = Logger.getLogger(PrintPdf.class.getName());
+ private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static final String DEFAULT_INPUT = "pdfjavatoolkit-ds.pdf";
private static PageRasterizer pageRasterizer;
@@ -88,16 +90,13 @@ public static void main(final String... args) throws Exception {
* @throws Exception a general exception was thrown
*/
public static void printPdf(final URL inputUrl) throws Exception {
- // Only log info messages and above
- LOGGER.setLevel(Level.INFO);
-
// Find the default printer.
final PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
// If no printer is available, give up: we can't go any further.
if (printService == null) {
- if (LOGGER.isLoggable(Level.SEVERE)) {
- LOGGER.severe("No printer available, aborting.");
+ if (LOGGER.isErrorEnabled()) {
+ LOGGER.error("No printer available, aborting.");
}
throw new PrinterException("Printer failed to exist.");
}
@@ -111,7 +110,7 @@ public static void printPdf(final URL inputUrl) throws Exception {
final int pdfPageHeight = (int) pdfPage.getMediaBox().height();
// Describe the selected printer.
- if (LOGGER.isLoggable(Level.INFO)) {
+ if (LOGGER.isInfoEnabled()) {
LOGGER.info("Printer: " + printService.getName());
}
@@ -121,7 +120,7 @@ public static void printPdf(final URL inputUrl) throws Exception {
if (printerResolution != null) {
resolution = printerResolution.getResolution(PrinterResolution.DPI)[0];
}
- if (LOGGER.isLoggable(Level.INFO)) {
+ if (LOGGER.isInfoEnabled()) {
LOGGER.info("Resolution: " + resolution + " DPI");
}
@@ -155,8 +154,8 @@ public static void printPdf(final URL inputUrl) throws Exception {
printerJob.setPrintable(new BufferedImagePrintable(), validatePage);
printerJob.print();
} catch (final IOException | PrinterException exp) {
- if (LOGGER.isLoggable(Level.WARNING)) {
- LOGGER.warning(exp.getMessage());
+ if (LOGGER.isWarnEnabled()) {
+ LOGGER.warn(exp.getMessage());
}
}
}
@@ -211,8 +210,8 @@ public int print(final Graphics gfx, final PageFormat pageFormat, final int page
}
} catch (final PDFFontException | PDFInvalidDocumentException | PDFInvalidParameterException
| PDFIOException | PDFSecurityException e) {
- if (LOGGER.isLoggable(Level.SEVERE)) {
- LOGGER.log(Level.SEVERE, "Error rasterizing a page", e);
+ if (LOGGER.isErrorEnabled()) {
+ LOGGER.error("Error rasterizing a page", e);
}
// This double-wrap allows us to throw the rasterizer exception to the PrinterJob.
throw new PrinterIOException(new IOException("Error rasterizing a page", e));
diff --git a/src/main/java/com/datalogics/pdf/samples/rendering/RenderPdf.java b/src/main/java/com/datalogics/pdf/samples/rendering/RenderPdf.java
index 86df44e4..8b920ef3 100644
--- a/src/main/java/com/datalogics/pdf/samples/rendering/RenderPdf.java
+++ b/src/main/java/com/datalogics/pdf/samples/rendering/RenderPdf.java
@@ -14,15 +14,17 @@
import com.datalogics.pdf.samples.util.FontUtils;
import com.datalogics.pdf.samples.util.IoUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
+import java.lang.invoke.MethodHandles;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
-import java.util.logging.Level;
-import java.util.logging.Logger;
import javax.imageio.ImageIO;
@@ -32,7 +34,7 @@
public final class RenderPdf {
private static final int RESOLUTION = 300;
- private static final Logger LOGGER = Logger.getLogger(RenderPdf.class.getName());
+ private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static final String DEFAULT_INPUT = "/com/datalogics/pdf/samples/printing/pdfjavatoolkit-ds.pdf";
private static PageRasterizer pageRasterizer;
@@ -99,9 +101,6 @@ public static void renderPdf(final URL inputUrl, final int resolution) throws Ex
* @throws Exception a general exception was thrown
*/
public static void renderPdf(final URL inputUrl, final int resolution, final URL outputBaseUrl) throws Exception {
- // Only log info messages and above
- LOGGER.setLevel(Level.INFO);
-
// Read the PDF input file and detect the page size of the first page. This sample assumes all pages in
// the document are the same size.
final PDFDocument pdfDocument = DocumentUtils.openPdfDocument(inputUrl);
@@ -109,7 +108,7 @@ public static void renderPdf(final URL inputUrl, final int resolution, final URL
final int pdfPageWidth = (int) pdfPage.getMediaBox().width();
final int pdfPageHeight = (int) pdfPage.getMediaBox().height();
- if (LOGGER.isLoggable(Level.INFO)) {
+ if (LOGGER.isInfoEnabled()) {
LOGGER.info("Resolution: " + resolution + " DPI");
}
diff --git a/src/main/java/com/datalogics/pdf/samples/signature/SignDocument.java b/src/main/java/com/datalogics/pdf/samples/signature/SignDocument.java
index 152b9701..0dff29ef 100644
--- a/src/main/java/com/datalogics/pdf/samples/signature/SignDocument.java
+++ b/src/main/java/com/datalogics/pdf/samples/signature/SignDocument.java
@@ -33,14 +33,16 @@
import com.datalogics.pdf.samples.util.IoUtils;
import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.w3c.dom.NodeList;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
+import java.lang.invoke.MethodHandles;
import java.net.URL;
import java.util.Iterator;
-import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
@@ -62,7 +64,7 @@
*/
public final class SignDocument {
- private static final Logger LOGGER = Logger.getLogger(SignDocument.class.getName());
+ private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final String DER_KEY_PATH = "pdfjt-key.der";
@@ -153,7 +155,7 @@ public static void signExistingSignatureFields(final URL inputUrl, final URL out
pdfDoc.close();
}
} catch (final PDFException e) {
- LOGGER.severe(e.getMessage());
+ LOGGER.error(e.getMessage());
}
}
}
@@ -237,10 +239,8 @@ private static Credentials createCredentialsFromDerBytes(final InputStream certS
.newInstance()
.createPrivateKey(derEncodedPrivateKey,
sigAlgorithm);
- final Credentials credentials = CredentialFactory.newInstance()
- .createCredentials(privateKeyHolder, derEncodedCert,
- null);
- return credentials;
+ return CredentialFactory.newInstance()
+ .createCredentials(privateKeyHolder, derEncodedCert, null);
}
private static byte[] getDerEncodedData(final InputStream inputStream) throws IOException {
diff --git a/src/main/java/com/datalogics/pdf/samples/util/DocumentUtils.java b/src/main/java/com/datalogics/pdf/samples/util/DocumentUtils.java
index 9d7fc287..b896e2cb 100644
--- a/src/main/java/com/datalogics/pdf/samples/util/DocumentUtils.java
+++ b/src/main/java/com/datalogics/pdf/samples/util/DocumentUtils.java
@@ -118,9 +118,7 @@ public static PDFDocument openPdfDocumentWithOptions(final URL inputUrl, final P
*/
public static PDFFontSet getDocumentFontSet(final PDFDocument document)
throws PDFInvalidDocumentException, PDFIOException, PDFFontException, PDFSecurityException {
- final PDFFontSet docFontSet = PDFFontSetUtil.buildWorkingFontSet(document, null, document.getDocumentLocale(),
- null);
- return docFontSet;
+ return PDFFontSetUtil.buildWorkingFontSet(document, null, document.getDocumentLocale(), null);
}
/**
diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml
new file mode 100644
index 00000000..7eb73b2f
--- /dev/null
+++ b/src/main/resources/log4j2.xml
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/test/java/com/datalogics/pdf/samples/extraction/TextExtractTest.java b/src/test/java/com/datalogics/pdf/samples/extraction/TextExtractTest.java
index a1ccad89..e754c18a 100644
--- a/src/test/java/com/datalogics/pdf/samples/extraction/TextExtractTest.java
+++ b/src/test/java/com/datalogics/pdf/samples/extraction/TextExtractTest.java
@@ -19,8 +19,10 @@
import com.datalogics.pdf.document.DocumentHelper;
import com.datalogics.pdf.samples.SampleTest;
-import com.datalogics.pdf.samples.util.LogRecordListCollector;
+import com.datalogics.pdf.samples.util.LogEventListCollector;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.LogEvent;
import org.junit.Test;
import java.io.File;
@@ -29,10 +31,7 @@
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
-import java.util.ArrayList;
-import java.util.logging.Level;
-import java.util.logging.LogRecord;
-import java.util.logging.Logger;
+import java.util.List;
/**
* Tests the TextExtract sample.
@@ -74,17 +73,18 @@ public void testDocumentWithNoText() throws Exception {
final URL inputUrl = emptyPdf.toURI().toURL();
final URL outputUrl = emptyTxt.toURI().toURL();
- final ArrayList logRecords = new ArrayList<>();
- final Logger logger = Logger.getLogger(TextExtract.class.getName());
- try (LogRecordListCollector collector = new LogRecordListCollector(logger, logRecords)) {
+ try (LogEventListCollector logEventListCollector = new LogEventListCollector()) {
TextExtract.extractTextReadingOrder(inputUrl, outputUrl);
- }
- // Verify that we got the expected log message
- assertEquals("Must have one log record", 1, logRecords.size());
- final LogRecord logRecord = logRecords.get(0);
- assertEquals(inputUrl.toURI().getPath() + " did not have any text to extract.", logRecord.getMessage());
- assertEquals(Level.INFO, logRecord.getLevel());
+ final List events = logEventListCollector.getEvents();
+
+ // Verify that we got the expected log message
+ assertEquals("Must have one log record", 1, events.size());
+ final LogEvent logEvent = events.get(0);
+ assertEquals(inputUrl.toURI().getPath() + " did not have any text to extract.",
+ logEvent.getMessage().getFormattedMessage());
+ assertEquals(Level.INFO, logEvent.getLevel());
+ }
// Verify that the output file was not created
final Path path = Paths.get(outputUrl.toURI());
diff --git a/src/test/java/com/datalogics/pdf/samples/util/LogEventListCollector.java b/src/test/java/com/datalogics/pdf/samples/util/LogEventListCollector.java
new file mode 100644
index 00000000..11178141
--- /dev/null
+++ b/src/test/java/com/datalogics/pdf/samples/util/LogEventListCollector.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2017 Datalogics, Inc.
+ */
+
+package com.datalogics.pdf.samples.util;
+
+
+import static org.apache.logging.log4j.core.Filter.Result.ACCEPT;
+import static org.apache.logging.log4j.core.Filter.Result.DENY;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.filter.ThresholdFilter;
+import org.apache.logging.log4j.test.appender.ListAppender;
+
+import java.lang.invoke.MethodHandles;
+import java.util.List;
+
+/**
+ * Collect up {@link LogEvent} objects for testing.
+ */
+public class LogEventListCollector implements AutoCloseable {
+ private static final String CLASS_NAME = MethodHandles.lookup().lookupClass().getSimpleName();
+
+ private ListAppender appender;
+
+ private final LoggerContext loggerContext;
+
+ /**
+ * Push a new {@link org.apache.logging.log4j.core.Appender} onto the root logger, and collect up events.
+ */
+ public LogEventListCollector() {
+ // Getting the context this way gets the same context that slf4j uses. The ClassLoader is used to distinguish
+ // between different web apps on the same server, but in an effort to simplify things, slf4j always gets a
+ // context this way. Thus, getting the default context isn't sufficient; one must get the context via the class
+ // loader.
+ loggerContext = LoggerContext.getContext(getClass().getClassLoader(), false, null);
+ appender = ListAppender.getListAppender(CLASS_NAME);
+ if (appender == null) {
+ appender = new ListAppender.Builder().setName(CLASS_NAME)
+ // If you were to alter the level, it'd happen here.
+ .setFilter(ThresholdFilter.createFilter(Level.TRACE, ACCEPT, DENY))
+ .build();
+ }
+
+ // If we have removed the appender, it gets stopped, so make sure it's started here.
+ appender.start();
+
+ // Appenders are persistent, therefore remove any previous events.
+ appender.clear();
+ loggerContext.getRootLogger().addAppender(appender);
+ }
+
+ public List getEvents() {
+ return appender.getEvents();
+ }
+
+ @Override
+ public void close() throws Exception {
+ loggerContext.getRootLogger().removeAppender(appender);
+ }
+}
diff --git a/src/test/java/com/datalogics/pdf/samples/util/LogRecordListCollector.java b/src/test/java/com/datalogics/pdf/samples/util/LogRecordListCollector.java
deleted file mode 100644
index ab895f12..00000000
--- a/src/test/java/com/datalogics/pdf/samples/util/LogRecordListCollector.java
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * Copyright 2015 Datalogics, Inc.
- */
-
-package com.datalogics.pdf.samples.util;
-
-import java.util.List;
-import java.util.logging.Handler;
-import java.util.logging.Level;
-import java.util.logging.LogRecord;
-import java.util.logging.Logger;
-
-public class LogRecordListCollector implements AutoCloseable {
- private static class LogRecordHandler extends Handler {
- private final List logRecordList;
-
- /**
- * Create a handler that records records to a list.
- *
- * @param logRecordList the list to receive the log records.
- */
- public LogRecordHandler(final List logRecordList) {
- super();
- this.logRecordList = logRecordList;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see java.util.logging.Handler#publish(java.util.logging.LogRecord)
- */
- @Override
- public void publish(final LogRecord record) {
- logRecordList.add(record);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see java.util.logging.Handler#flush()
- */
- @Override
- public void flush() {}
-
- /*
- * (non-Javadoc)
- *
- * @see java.util.logging.Handler#close()
- */
- @Override
- public void close() throws SecurityException {}
- }
-
- private final Logger targetLogger;
- private final Handler handler;
-
-
- /**
- * Make a list of every {@link LogRecord} sent to a {@link Logger}.
- *
- * @param targetLogger the logger to monitor
- * @param logRecordList a list to receive the
- */
- public LogRecordListCollector(final Logger targetLogger, final List logRecordList) {
- super();
- this.targetLogger = targetLogger;
- this.handler = new LogRecordHandler(logRecordList);
- this.handler.setLevel(Level.ALL);
- this.targetLogger.addHandler(this.handler);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see java.lang.AutoCloseable#close()
- */
- @Override
- public void close() throws Exception {
- targetLogger.removeHandler(handler);
- }
-
-}
diff --git a/src/test/resources/log4j2-test.xml b/src/test/resources/log4j2-test.xml
new file mode 100644
index 00000000..300d2d3c
--- /dev/null
+++ b/src/test/resources/log4j2-test.xml
@@ -0,0 +1,25 @@
+
+
+
+
+
+
+
+
+ %d %p %c{1.} [%t] %m%n
+
+
+
+
+
+
+
+
+
+
+
+
+
+