You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pig.apache.org by da...@apache.org on 2015/02/25 01:58:04 UTC

svn commit: r1662152 - in /pig/trunk: CHANGES.txt src/org/apache/pig/Main.java test/org/apache/pig/TestMain.java

Author: daijy
Date: Wed Feb 25 00:58:03 2015
New Revision: 1662152

URL: http://svn.apache.org/r1662152
Log:
PIG-4430: Pig should support reading log4j.properties file from classpath as well

Modified:
    pig/trunk/CHANGES.txt
    pig/trunk/src/org/apache/pig/Main.java
    pig/trunk/test/org/apache/pig/TestMain.java

Modified: pig/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/pig/trunk/CHANGES.txt?rev=1662152&r1=1662151&r2=1662152&view=diff
==============================================================================
--- pig/trunk/CHANGES.txt (original)
+++ pig/trunk/CHANGES.txt Wed Feb 25 00:58:03 2015
@@ -24,6 +24,8 @@ INCOMPATIBLE CHANGES
  
 IMPROVEMENTS
 
+PIG-4430: Pig should support reading log4j.properties file from classpath as well (rdsr via daijy)
+
 PIG-4407: Allow specifying a replication factor for jarcache (jira.shegalov via rohini)
 
 PIG-4401: Add pattern matching to PluckTuple (cheolsoo)

Modified: pig/trunk/src/org/apache/pig/Main.java
URL: http://svn.apache.org/viewvc/pig/trunk/src/org/apache/pig/Main.java?rev=1662152&r1=1662151&r2=1662152&view=diff
==============================================================================
--- pig/trunk/src/org/apache/pig/Main.java (original)
+++ pig/trunk/src/org/apache/pig/Main.java Wed Feb 25 00:58:03 2015
@@ -17,6 +17,11 @@
  */
 package org.apache.pig;
 
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Strings;
+import com.google.common.io.Closeables;
+import com.google.common.io.InputSupplier;
+import com.google.common.io.Resources;
 import java.io.BufferedInputStream;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
@@ -28,8 +33,10 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
+import java.io.Reader;
 import java.io.StringReader;
-import java.io.Writer;
+import java.net.URL;
+import java.nio.charset.Charset;
 import java.text.ParseException;
 import java.util.AbstractList;
 import java.util.ArrayList;
@@ -742,22 +749,7 @@ public class Main {
             logLevel = Level.toLevel(logLevelString, Level.INFO);
         }
 
-        Properties props = new Properties();
-        FileReader propertyReader = null;
-        if (log4jconf != null) {
-            try {
-                propertyReader = new FileReader(log4jconf);
-                props.load(propertyReader);
-            }
-            catch (IOException e)
-            {
-                System.err.println("Warn: Cannot open log4j properties file, use default");
-            }
-            finally
-            {
-                if (propertyReader != null) try {propertyReader.close();} catch(Exception e) {}
-            }
-        }
+        final Properties props = log4jConfAsProperties(log4jconf);
         if (props.size() == 0) {
             props.setProperty("log4j.logger.org.apache.pig", logLevel.toString());
             if((logLevelString = System.getProperty("pig.logfile.level")) == null){
@@ -790,8 +782,37 @@ public class Main {
         pigContext.setDefaultLogLevel(logLevel);
     }
 
+   @VisibleForTesting
+   static Properties log4jConfAsProperties(String log4jconf) {
+       final Properties properties = new Properties();
+       if (!Strings.isNullOrEmpty(log4jconf)) {
+           Reader propertyReader = null;
+           try {
+               final File file = new File(log4jconf);
+               if (file.exists()) {
+                   propertyReader = new FileReader(file);
+                   properties.load(propertyReader);
+                   log.info("Loaded log4j properties from file: " + file);
+               } else {
+                   final URL resource = Main.class.getClassLoader().getResource(log4jconf);
+                   if (resource != null) {
+                       propertyReader = new InputStreamReader(resource.openStream(), Charset.forName("UTF-8"));
+                       properties.load(propertyReader);
+                       log.info("Loaded log4j properties from resource: " +  resource);
+                   } else {
+                       log.warn("No file or resource found by the name: " + log4jconf);
+                   }
+               }
+           } catch (IOException e)  {
+               log.warn("Cannot open log4j properties file " + log4jconf + ", using default");
+           } finally {
+               Closeables.closeQuietly(propertyReader);
+           }
+       }
+       return properties;
+  }
 
-    private static List<String> fetchRemoteParamFiles(List<String> paramFiles, Properties properties)
+  private static List<String> fetchRemoteParamFiles(List<String> paramFiles, Properties properties)
             throws IOException {
         List<String> paramFiles2 = new ArrayList<String>();
         for (String param: paramFiles) {

Modified: pig/trunk/test/org/apache/pig/TestMain.java
URL: http://svn.apache.org/viewvc/pig/trunk/test/org/apache/pig/TestMain.java?rev=1662152&r1=1662151&r2=1662152&view=diff
==============================================================================
--- pig/trunk/test/org/apache/pig/TestMain.java (original)
+++ pig/trunk/test/org/apache/pig/TestMain.java Wed Feb 25 00:58:03 2015
@@ -39,6 +39,9 @@ import org.apache.pig.tools.parameters.P
 import org.apache.pig.tools.pigstats.PigStats;
 import org.junit.Test;
 
+import java.nio.charset.Charset;
+import com.google.common.io.Files;
+
 public class TestMain {
     private Log log = LogFactory.getLog(TestMain.class);
 
@@ -126,6 +129,29 @@ public class TestMain {
         }
     }
 
+    @Test
+    public void testlog4jConf() throws Exception {
+        Properties properties = Main.log4jConfAsProperties(null);
+        assertTrue(properties.isEmpty());
+        properties = Main.log4jConfAsProperties("");
+        assertTrue(properties.isEmpty());
+        // Test for non-existent file
+        properties = Main.log4jConfAsProperties("non-existing-" + System.currentTimeMillis());
+        assertTrue(properties.isEmpty());
+
+        // Create tmp file in under build/test/classes
+        File tmpFile = File.createTempFile("pig-log4jconf", ".properties", new File("build/test/classes"));
+        tmpFile.deleteOnExit();
+        Files.write("A=B", tmpFile, Charset.forName("UTF-8"));
+        // Read it as a resource
+        properties = Main.log4jConfAsProperties(tmpFile.getName());
+        assertEquals("B", properties.getProperty("A"));
+        // Read it as a file
+        properties = Main.log4jConfAsProperties(tmpFile.getAbsolutePath());
+        assertEquals("B", properties.getProperty("A"));
+    }
+
+
     public static class TestNotificationListener2 extends TestNotificationListener {
         protected boolean hadArgs = false;
         public TestNotificationListener2() {}