How can I log the number of documents / bytes I've processed?
I want to log the number of documents I've processed using iText, as well as the number of bytes I've read or produced. Is there a logging mechanism that allows me to do this?
Question asked at an internal meeting at iText Group NV.
Please take a look at the CounterDemoSyso. In this example, we get an instance of the CounterFactory
. Simultaneously, we create a new SysoCounter
object, which is an implementation of the Counter
interface that writes information to the console (System.out
):
CounterFactory.getInstance().setCounter(new SysoCounter());
We can now use ordinary iText code without having to worry about counting bytes or documents; that single line activated the counting mechanism.
For instance, we can create a PDF:
public void createPdf(String filename) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
document.add(new Paragraph("Hello World!"));
document.close();
}
Or we can manipulate a PDF:
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
PdfContentByte pagecontent = stamper.getOverContent(1);
ColumnText.showTextAligned(pagecontent, Element.ALIGN_RIGHT,
new Phrase("Stamped text"), 559, 806, 0);
stamper.close();
reader.close();
}
If we execute createPdf()
and manipulatePdf()
, the SysoCounter
will automatically write the following information to the System.out
:
[com.itextpdf.text.pdf.PdfWriter] 915 bytes written
[com.itextpdf.text.pdf.PdfReader] 915 bytes read
[com.itextpdf.text.pdf.PdfStamper] 1380 bytes written
In the first line, we see that PdfWriter
wrote 915 bytes (this happened in the createPdf()
method). In the second and third line, we see what happened in the manipulatePdf()
method: PdfReader
has read 915 bytes and we added the text "Stamped text"
resulting in 1380 bytes being written by PdfStamper
.
This is only a simple example that writes to the System.out
. It's not difficult to write some code that stores this information (and more) in another place. Let's take a look at the CounterDemo example. In this example, we write our own implementation of the Counter
interface so that information about the processed documents is written to a file.
We need to implement three methods:
getCounter()
: this method will return an instance of your counter,read(long l)
: this method will be triggered when a file is read,written(long l)
: this method will be triggered when a file is written.
We implemented these methods as follows:
public class MyCounter implements Counter {
public static final String LOG = "results/logging/counter.txt";
protected FileWriter writer;
protected String yourClass;
protected String iTextClass;
public MyCounter(<Class> klass) throws IOException {
this.yourClass = klass.getName();
writer = new FileWriter(LOG, true);
}
private MyCounter(<Class> klass, String yourClass, FileWriter writer)
throws IOException {
this.yourClass = yourClass;
this.iTextClass = klass.getName();
this.writer = writer;
}
public Counter getCounter(<Class> klass) {
try {
return new MyCounter(klass, yourClass, writer);
} catch (IOException e) {
throw new ExceptionConverter(e);
}
}
public void read(long l) {
if (writer == null)
throw new RuntimeException("No writer defined!");
try {
writer.write(String.format(
"[%s:%s] %s: %s read\n",
yourClass, iTextClass, new Date().toString(), l));
writer.flush();
} catch (IOException e) {
throw new ExceptionConverter(e);
}
}
public void written(long l) {
if (writer == null)
throw new RuntimeException("No writer defined!");
try {
writer.write(String.format(
"[%s:%s] %s: %s written\n",
yourClass, iTextClass, new Date().toString(), l));
writer.flush();
} catch (IOException e) {
throw new ExceptionConverter(e);
}
}
public void close() throws IOException {
writer.close();
}
}
In line 3, we see a path to a log file. Instead of writing to the System.out
, we will write to that file using the writer
object declared in line 4. The yourClass
object declared in line 5 will store the class name of your application that uses the Counter
; the iTextClass
object declared in line 6 will store the iText class that reads, writes or manipulates the PDF document.
We have a public constructor that initializes the yourClass
variable (line 8-11). We use in our own code like this:
public void initCounter() throws IOException {
counter = new MyCounter(getClass());
CounterFactory.getInstance().setCounter(counter);
}
The private constructor (line 13-18) will be called by iText internally through the getCounter()
method (line 20-26). It initializes the iTextClass
variable and passes the yourClass
and writer
variables as parameters.
When bytes are read (line 28-39), we will write information to the log file that looks like this:
String.format("[%s:%s] %s: %s read\n", yourClass, iTextClass, new Date().toString(), l)
When bytes are written (line 41-52), we will write information to the log file that looks like:
String.format("[%s:%s] %s: %s written\n", yourClass, iTextClass, new Date().toString(), l)
If we execute the same createPdf()
and manipulatePdf()
methods as before, we get the following result:
[sandbox.logging.CounterDemo:com.itextpdf.text.pdf.PdfWriter] Mon Nov 09 10:42:59 CET 2015: 915 written
[sandbox.logging.CounterDemo:com.itextpdf.text.pdf.PdfReader] Mon Nov 09 10:42:59 CET 2015: 915 read
[sandbox.logging.CounterDemo:com.itextpdf.text.pdf.PdfStamper] Mon Nov 09 10:42:59 CET 2015: 1380 written
As we are writing to a file, we mustn't forget to close the FileWriter
(line 54-56) once we've finished reading and writing documents:
public void closeCounter() throws IOException {
counter.close();
}
This example is provided FYI. In a real-world situation, you typically won't write to a file because writing to disk is a slow operation and it's not a good idea to write to a single file in a multi-threaded environment. Typically, you'll write this information to a database, so that you can output information that looks for instance like this:
Month | application | operation | amount | size |
---|---|---|---|---|
2015-09 | InvoiceApp | PdfWriter | 8451 | 10479242 |
2015-09 | WatermarkApp | PdfReader | 798 | 518700 |
2015-09 | WatermarkApp | PdfStamper | 798 | 638400 |
2015-10 | InvoiceApp | PdfWriter | 8757 | 10957278 |
2015-10 | WatermarkApp | PdfReader | 804 | 522600 |
2015-10 | WatermarkApp | PdfStamper | 804 | 643200 |
A possible use case could be to use such a table to monitor the iText use of customers running their custom PDF application in a specific JVM on your servers. You could use these metrics to charge your customers based on the number of PDF documents or the number of PDF bytes they are processing.
Important: the CounterFactory
is currently implemented as a singleton that stores a single Counter
instance. This means that you can only store a single Counter
at a time for each JVM. In the above table, we'd have an InvoiceApp
running on one JVM and a WatermarkApp
on another JVM.
Another use case (that is more realistic) would be to count the use of iText on different JVMs:
Month | JVM | operation | amount | size |
---|---|---|---|---|
2015-09 | server1 | PdfWriter | 8451 | 10479242 |
2015-09 | server2 | PdfWriter | 8757 | 10957278 |
2015-09 | server1 | PdfReader | 798 | 518700 |
2015-09 | server2 | PdfReader | 804 | 522600 |
2015-09 | server1 | PdfStamper | 798 | 638400 |
2015-09 | server2 | PdfStamper | 804 | 643200 |