Logging mechanism

Tags: loggingcounteriText 5

These examples were written in the context of the question How can I log the number of documents / bytes I've processed?

Files: 
package sandbox.logging;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.log.CounterFactory;
import com.itextpdf.text.log.SysoCounter;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class CounterDemoSyso {
 
    public static final String HELLO = "results/logging/hello.pdf";
    public static final String HELLO2 = "results/logging/hello2.pdf";
    public static void main(String[] args) throws IOException, DocumentException {
        CounterDemoSyso app = new CounterDemoSyso();
        app.initCounter();
        app.createPdf(HELLO);
        app.manipulatePdf(HELLO, HELLO2);
    }
 
    public void initCounter() throws IOException {
        File file = new File(HELLO);
        file.getParentFile().mkdirs();
        CounterFactory.getInstance().setCounter(new SysoCounter());
    }
 
    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();
    }
 
    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();
    }
}
package sandbox.logging;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.log.Counter;
import com.itextpdf.text.log.CounterFactory;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
 
public class CounterDemo {
 
    public static final String HELLO = "results/logging/hello.pdf";
    public static final String HELLO2 = "results/logging/hello2.pdf";
    protected MyCounter counter;
 
    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();
        }
    }
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(HELLO);
        file.getParentFile().mkdirs();
        CounterDemo app = new CounterDemo();
        app.initCounter();
        app.createPdf(HELLO);
        app.manipulatePdf(HELLO, HELLO2);
        app.closeCounter();
    }
 
    public void initCounter() throws IOException {
        counter = new MyCounter(getClass());
        CounterFactory.getInstance().setCounter(counter);
    }
 
    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World!"));
        // step 5
        document.close();
    }
 
    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();
    }
 
    public void closeCounter() throws IOException {
        counter.close();
    }
}
File nameRaw URLUpdated
CounterDemoSyso.javaCounterDemoSyso.java2015-11-07 5:50 pm
CounterDemo.javaCounterDemo.java2015-11-07 5:50 pm