Author: lehmi
Date: Fri Oct 27 06:04:06 2023
New Revision: 1913376

URL: http://svn.apache.org/viewvc?rev=1913376&view=rev
Log:
PDFBOX-5695: switch to log4j as suggested by Axel Howind

Modified:
    pdfbox/trunk/io/pom.xml
    pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/IOUtils.java
    
pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/RandomAccessInputStream.java
    pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/ScratchFile.java

Modified: pdfbox/trunk/io/pom.xml
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/io/pom.xml?rev=1913376&r1=1913375&r2=1913376&view=diff
==============================================================================
--- pdfbox/trunk/io/pom.xml (original)
+++ pdfbox/trunk/io/pom.xml Fri Oct 27 06:04:06 2023
@@ -39,10 +39,22 @@
 
     <dependencies>
         <dependency>
+            <!-- TODO remove when transition to Log4J is complete -->
             <groupId>commons-logging</groupId>
             <artifactId>commons-logging</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-api</artifactId>
+            <version>${log4j2.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-core</artifactId>
+            <version>${log4j2.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.junit.jupiter</groupId>
             <artifactId>junit-jupiter</artifactId>
         </dependency>

Modified: pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/IOUtils.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/IOUtils.java?rev=1913376&r1=1913375&r2=1913376&view=diff
==============================================================================
--- pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/IOUtils.java (original)
+++ pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/IOUtils.java Fri Oct 27 
06:04:06 2023
@@ -44,7 +44,8 @@ import java.util.Optional;
 import java.util.function.Consumer;
 
 import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.LogManager;
 import org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction;
 
 /**
@@ -66,7 +67,7 @@ public final class IOUtils
     /**
      * Log instance.
      */
-    private static final Log LOG = LogFactory.getLog(IOUtils.class);
+    private static final Logger LOG = LogManager.getLogger(IOUtils.class);
 
     private IOUtils()
     {
@@ -148,7 +149,9 @@ public final class IOUtils
      * @param initialException if set, this exception will be returned even 
where there is another
      * exception while closing the IO resource
      * @return the IOException is there was any but only if initialException 
is null
+     * @deprecated use {@link Logger} instead
      */
+    @Deprecated
     public static IOException closeAndLogException(Closeable closeable, Log 
logger, String resourceName, IOException initialException)
     {
         try
@@ -161,6 +164,35 @@ public final class IOUtils
             if (initialException == null)
             {
                 return ioe;
+            }
+        }
+        return initialException;
+    }
+
+    /**
+     * Try to close an IO resource and log and return if there was an 
exception.
+     *
+     * <p>An exception is only returned if the IOException passed in is null.
+     *
+     * @param closeable to be closed
+     * @param logger the logger to be used so that logging appears under that 
log instance
+     * @param resourceName the name to appear in the log output
+     * @param initialException if set, this exception will be returned even 
where there is another
+     * exception while closing the IO resource
+     * @return the IOException is there was any but only if initialException 
is null
+     */
+    public static IOException closeAndLogException(Closeable closeable, Logger 
logger, String resourceName, IOException initialException)
+    {
+        try
+        {
+            closeable.close();
+        }
+        catch (IOException ioe)
+        {
+            logger.warn("Error closing {}", resourceName, ioe);
+            if (initialException == null)
+            {
+                return ioe;
             }
         }
         return initialException;

Modified: 
pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/RandomAccessInputStream.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/RandomAccessInputStream.java?rev=1913376&r1=1913375&r2=1913376&view=diff
==============================================================================
--- 
pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/RandomAccessInputStream.java 
(original)
+++ 
pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/RandomAccessInputStream.java 
Fri Oct 27 06:04:06 2023
@@ -19,8 +19,8 @@ package org.apache.pdfbox.io;
 import java.io.InputStream;
 import java.io.IOException;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.LogManager;
 
 /**
  * An InputStream which reads from a RandomAccessRead.
@@ -30,7 +30,7 @@ import org.apache.commons.logging.LogFac
  */
 public class RandomAccessInputStream extends InputStream
 {
-    private static final Log LOG = 
LogFactory.getLog(RandomAccessInputStream.class);
+    private static final Logger LOG = 
LogManager.getLogger(RandomAccessInputStream.class);
 
     private final RandomAccessRead input;
     private long position;
@@ -75,8 +75,8 @@ public class RandomAccessInputStream ext
         {
             // should never happen due to prior isEOF() check
             // unless there is an unsynchronized concurrent access
-            LOG.error("read() returns -1, assumed position: " +
-                       position + ", actual position: " + input.getPosition());
+            LOG.error("read() returns -1, assumed position: {}, actual 
position: {}", position,
+                    input.getPosition());
         }
         return b;
     }
@@ -98,8 +98,8 @@ public class RandomAccessInputStream ext
         {
             // should never happen due to prior isEOF() check
             // unless there is an unsynchronized concurrent access
-            LOG.error("read() returns -1, assumed position: " +
-                       position + ", actual position: " + input.getPosition());
+            LOG.error("read() returns -1, assumed position: {}, actual 
position: {}", position,
+                    input.getPosition());
         }
         return n;
     }

Modified: pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/ScratchFile.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/ScratchFile.java?rev=1913376&r1=1913375&r2=1913376&view=diff
==============================================================================
--- pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/ScratchFile.java 
(original)
+++ pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/ScratchFile.java Fri Oct 
27 06:04:06 2023
@@ -22,8 +22,8 @@ import java.util.ArrayList;
 import java.util.BitSet;
 import java.util.List;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.LogManager;
 
 /**
  * Implements a memory page handling mechanism as base for creating (multiple)
@@ -50,7 +50,7 @@ import org.apache.commons.logging.LogFac
  */
 public class ScratchFile implements RandomAccessStreamCache
 {
-    private static final Log LOG = LogFactory.getLog(ScratchFile.class);
+    private static final Logger LOG = LogManager.getLogger(ScratchFile.class);
 
     /** number of pages by which we enlarge the scratch file (reduce 
I/O-operations) */
     private static final int ENLARGE_PAGE_COUNT = 16;
@@ -155,7 +155,9 @@ public class ScratchFile implements Rand
         catch (IOException ioe)
         {
             // cannot happen for main memory setup
-            LOG.error("Unexpected exception occurred creating main memory 
scratch file instance: " + ioe.getMessage(), ioe);
+            LOG.error(
+                    "Unexpected exception occurred creating main memory 
scratch file instance: {}",
+                    ioe.getMessage(), ioe);
             return null;
         }
     }
@@ -178,7 +180,9 @@ public class ScratchFile implements Rand
         catch (IOException ioe)
         {
             // cannot happen for main memory setup
-            LOG.error("Unexpected exception occurred creating main memory 
scratch file instance: " + ioe.getMessage(), ioe);
+            LOG.error(
+                    "Unexpected exception occurred creating main memory 
scratch file instance: {}",
+                    ioe.getMessage(), ioe);
             return null;
         }
     }
@@ -257,7 +261,7 @@ public class ScratchFile implements Rand
                     {
                         if (!file.delete())
                         {
-                            LOG.warn("Error deleting scratch file: " + 
file.getAbsolutePath());
+                            LOG.warn("Error deleting scratch file: {}", 
file.getAbsolutePath());
                         }
                         throw e;
                     }


Reply via email to