Logging mechanism

This is a code example of iText PDF, discover more.

31st May 2016
iText PDF

Switch code for this example

CounterDemo.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. package com.itextpdf.samples.sandbox.logging;
  9.  
  10. import com.itextpdf.kernel.log.Counter;
  11. import com.itextpdf.kernel.log.CounterFactory;
  12. import com.itextpdf.kernel.log.DefaultCounter;
  13. import com.itextpdf.kernel.pdf.PdfDocument;
  14. import com.itextpdf.kernel.pdf.PdfReader;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.layout.Document;
  17. import com.itextpdf.layout.element.Paragraph;
  18. import com.itextpdf.layout.property.TextAlignment;
  19. import com.itextpdf.samples.GenericTest;
  20. import com.itextpdf.test.annotations.type.SampleTest;
  21.  
  22. import java.io.BufferedReader;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.FileNotFoundException;
  26. import java.io.FileWriter;
  27. import java.io.IOException;
  28. import java.io.InputStreamReader;
  29. import java.util.Date;
  30.  
  31. import org.junit.Assert;
  32. import org.junit.experimental.categories.Category;
  33.  
  34. @Category(SampleTest.class)
  35. public class CounterDemo extends GenericTest {
  36.     public static final String SRC = "./target/test/resources/sandbox/logging/hello.pdf";
  37.     public static final String DEST = "./target/test/resources/sandbox/logging/stamp.pdf";
  38.     public static final String LOG_RESULT = "./target/test/resources/sandbox/logging/log.txt";
  39.     public static final String CMP_LOG = "./src/test/resources/sandbox/logging/cmp_log.txt";
  40.  
  41.     MyCounter myCounter;
  42.  
  43.     public static void main(String[] args) throws IOException {
  44.         File file = new File(DEST);
  45.         file.getParentFile().mkdirs();
  46.         new CounterDemo().manipulatePdf(DEST);
  47.     }
  48.  
  49.     private void createPdf() throws FileNotFoundException {
  50.         Document document = new Document(new PdfDocument(new PdfWriter(SRC)));
  51.         document.add(new Paragraph("Hello World!"));
  52.         document.close();
  53.     }
  54.  
  55.     @Override
  56.     public void manipulatePdf(String dest) throws IOException {
  57.         myCounter = new MyCounter(getClass());
  58.         CounterFactory.getInstance().setCounter(myCounter);
  59.  
  60.         createPdf();
  61.         PdfReader reader = new PdfReader(SRC);
  62.         PdfDocument pdfDocument = new PdfDocument(reader, new PdfWriter(dest));
  63.         Document document = new Document(pdfDocument).showTextAligned(new Paragraph("Stamped text"), 559, 806, TextAlignment.RIGHT);
  64.         document.close();
  65.  
  66.         myCounter.close();
  67.         CounterFactory.getInstance().setCounter(new DefaultCounter());
  68.  
  69.         compareTxt(CMP_LOG, LOG_RESULT);
  70.     }
  71.  
  72.     public class MyCounter implements Counter {
  73.  
  74.         protected FileWriter writer;
  75.         protected String yourClass;
  76.         protected String iTextClass;
  77.  
  78.         public MyCounter(Class<?> klass) throws IOException {
  79.             this.yourClass = klass.getName();
  80.             writer = new FileWriter(LOG_RESULT, false);
  81.         }
  82.  
  83.         private MyCounter(Class<?> klass, String yourClass, FileWriter writer)
  84.                 throws IOException {
  85.             this.yourClass = yourClass;
  86.             this.iTextClass = klass.getName();
  87.             this.writer = writer;
  88.         }
  89.  
  90.         @Override
  91.         public Counter getCounter(Class<?> klass) {
  92.             try {
  93.                 return new MyCounter(klass, yourClass, writer);
  94.             } catch (IOException e) {
  95.                 throw new RuntimeException(e);
  96.             }
  97.         }
  98.  
  99.         @Override
  100.         public void onDocumentRead(long size) {
  101.             if (writer == null)
  102.                 throw new RuntimeException("No writer defined!");
  103.             try {
  104.                 writer.write(String.format(
  105.                         "[%s:%s] %s: %s read\n", yourClass, iTextClass, new Date().toString(), size));
  106.                 writer.flush();
  107.             } catch (IOException e) {
  108.                 throw new RuntimeException(e);
  109.             }
  110.         }
  111.  
  112.         @Override
  113.         public void onDocumentWritten(long size) {
  114.             if (writer == null)
  115.                 throw new RuntimeException("No writer defined!");
  116.             try {
  117.                 writer.write(String.format(
  118.                         "[%s:%s] %s: %s written\n", yourClass, iTextClass, new Date().toString(), size));
  119.                 writer.flush();
  120.             } catch (IOException e) {
  121.                 throw new RuntimeException(e);
  122.             }
  123.         }
  124.  
  125.         public void close() throws IOException {
  126.             writer.close();
  127.         }
  128.     }
  129.  
  130.     private void compareTxt(String cmp, String out) throws IOException {
  131.         Assert.assertEquals(fileToString(cmp), fileToString(out));
  132.     }
  133.  
  134.     private String fileToString(String filePath) throws IOException {
  135.         FileInputStream fin = new FileInputStream(filePath);
  136.         BufferedReader myInput = new BufferedReader(new InputStreamReader(fin));
  137.         StringBuilder sb = new StringBuilder();
  138.         String thisLine;
  139.         while ((thisLine = myInput.readLine()) != null) {
  140.             if (thisLine.length() > 10) {
  141.                 sb.append(thisLine.substring(thisLine.length() - 10, thisLine.length()));
  142.             }
  143.         }
  144.         return sb.toString();
  145.     }
  146.  
  147. }
CounterDemoSystemOut.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. package com.itextpdf.samples.sandbox.logging;
  9.  
  10. import com.itextpdf.kernel.log.CounterFactory;
  11. import com.itextpdf.kernel.log.DefaultCounter;
  12. import com.itextpdf.kernel.log.SystemOutCounter;
  13. import com.itextpdf.kernel.pdf.PdfDocument;
  14. import com.itextpdf.kernel.pdf.PdfReader;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.layout.Document;
  17. import com.itextpdf.layout.element.Paragraph;
  18. import com.itextpdf.layout.property.TextAlignment;
  19. import com.itextpdf.samples.GenericTest;
  20. import com.itextpdf.test.annotations.type.SampleTest;
  21.  
  22. import java.io.File;
  23. import java.io.FileNotFoundException;
  24. import java.io.IOException;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. @Category(SampleTest.class)
  29. public class CounterDemoSystemOut extends GenericTest {
  30.  
  31.     public static final String SRC = "./target/test/resources/sandbox/logging/hello2.pdf";
  32.     public static final String DEST = "./target/test/resources/sandbox/logging/stamp2.pdf";
  33.  
  34.     public static void main(String[] args) throws IOException {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new CounterDemoSystemOut().manipulatePdf(DEST);
  38.     }
  39.  
  40.     private void createPdf() throws FileNotFoundException {
  41.         Document document = new Document(new PdfDocument(new PdfWriter(SRC)));
  42.         document.add(new Paragraph("Hello World!"));
  43.         document.close();
  44.     }
  45.  
  46.     @Override
  47.     public void manipulatePdf(String dest) throws IOException {
  48.         CounterFactory.getInstance().setCounter(new SystemOutCounter());
  49.  
  50.         createPdf();
  51.         PdfReader reader = new PdfReader(SRC);
  52.         PdfDocument pdfDocument = new PdfDocument(reader, new PdfWriter(dest));
  53.         Document document = new Document(pdfDocument).showTextAligned(new Paragraph("Stamped text"), 559, 806, TextAlignment.RIGHT);
  54.         document.close();
  55.  
  56.         CounterFactory.getInstance().setCounter(new DefaultCounter());
  57.     }
  58.  
  59. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now