You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by kk...@apache.org on 2011/07/11 17:13:14 UTC

svn commit: r1145200 - in /tomcat/trunk: java/org/apache/catalina/valves/AccessLogValve.java java/org/apache/catalina/valves/LocalStrings.properties webapps/docs/changelog.xml webapps/docs/config/valve.xml

Author: kkolinko
Date: Mon Jul 11 15:13:13 2011
New Revision: 1145200

URL: http://svn.apache.org/viewvc?rev=1145200&view=rev
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=46252
Allow to specify character set to be used to write the access log in AccessLogValve.

Modified:
    tomcat/trunk/java/org/apache/catalina/valves/AccessLogValve.java
    tomcat/trunk/java/org/apache/catalina/valves/LocalStrings.properties
    tomcat/trunk/webapps/docs/changelog.xml
    tomcat/trunk/webapps/docs/config/valve.xml

Modified: tomcat/trunk/java/org/apache/catalina/valves/AccessLogValve.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/valves/AccessLogValve.java?rev=1145200&r1=1145199&r2=1145200&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/valves/AccessLogValve.java (original)
+++ tomcat/trunk/java/org/apache/catalina/valves/AccessLogValve.java Mon Jul 11 15:13:13 2011
@@ -21,10 +21,13 @@ package org.apache.catalina.valves;
 
 import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileWriter;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
 import java.net.InetAddress;
+import java.nio.charset.Charset;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
@@ -49,6 +52,7 @@ import org.apache.coyote.RequestInfo;
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.ExceptionUtils;
+import org.apache.tomcat.util.buf.B2CConverter;
 
 
 /**
@@ -518,7 +522,14 @@ public class AccessLogValve extends Valv
      * log file name suffix.
      */
     protected Locale locale = Locale.getDefault();
-    
+
+    /**
+     * Character set used by the log file. If it is <code>null</code>, the
+     * system default character set will be used. An empty string will be
+     * treated as <code>null</code> when this property is assigned.
+     */
+    protected String encoding = null;
+
     /**
      * Array of AccessLogElement, they will be used to make log message.
      */
@@ -786,6 +797,29 @@ public class AccessLogValve extends Valv
         locale = findLocale(localeName, locale);
     }
 
+    /**
+     * Return the character set name that is used to write the log file.
+     *
+     * @return Character set name, or <code>null</code> if the system default
+     *  character set is used.
+     */
+    public String getEncoding() {
+        return encoding;
+    }
+
+    /**
+     * Set the character set that is used to write the log file.
+     * 
+     * @param encoding The name of the character set.
+     */
+    public void setEncoding(String encoding) {
+        if (encoding != null && encoding.length() > 0) {
+            this.encoding = encoding;
+        } else {
+            this.encoding = null;
+        }
+    }
+
     // --------------------------------------------------------- Public Methods
 
     /**
@@ -984,9 +1018,22 @@ public class AccessLogValve extends Valv
                 pathname = dir.getAbsolutePath() + File.separator + prefix
                         + suffix;
             }
-            writer = new PrintWriter(new BufferedWriter(new FileWriter(
-                    pathname, true), 128000), false);
-            
+            Charset charset = null;
+            if (encoding != null) {
+                try {
+                    charset = B2CConverter.getCharset(encoding);
+                } catch (UnsupportedEncodingException ex) {
+                    log.error(sm.getString(
+                            "accessLogValve.unsupportedEncoding", encoding), ex);
+                }
+            }
+            if (charset == null) {
+                charset = Charset.defaultCharset();
+            }
+            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
+                    new FileOutputStream(pathname, true), charset), 128000),
+                    false);
+
             currentLogFile = new File(pathname);
         } catch (IOException e) {
             writer = null;

Modified: tomcat/trunk/java/org/apache/catalina/valves/LocalStrings.properties
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/valves/LocalStrings.properties?rev=1145200&r1=1145199&r2=1145200&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/valves/LocalStrings.properties (original)
+++ tomcat/trunk/java/org/apache/catalina/valves/LocalStrings.properties Mon Jul 11 15:13:13 2011
@@ -26,6 +26,7 @@ accessLogValve.closeFail=Failed to close
 accessLogValve.openDirFail=Failed to create directory [{0}] for access logs
 accessLogValve.rotateFail=Failed to rotate access log
 accessLogValve.invalidLocale=Failed to set locale to [{0}]
+accessLogValve.unsupportedEncoding=Failed to set encoding to [{0}], will use the system default character set.
 
 # Error report valve
 errorReportValve.errorReport=Error report

Modified: tomcat/trunk/webapps/docs/changelog.xml
URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1145200&r1=1145199&r2=1145200&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/trunk/webapps/docs/changelog.xml Mon Jul 11 15:13:13 2011
@@ -62,6 +62,10 @@
       <fix>
         Fix regression in year number formatting for AccessLogValve. (rjung)
       </fix>
+      <add>
+        <bug>46252</bug>: Allow to specify character set to be used to write
+        the access log in AccessLogValve. (kkolinko)
+      </add>
       <fix>
         <bug>51494</bug>: Prevent an NPE when a long running request completes
         if the associated web application was destroyed while the request was

Modified: tomcat/trunk/webapps/docs/config/valve.xml
URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/valve.xml?rev=1145200&r1=1145199&r2=1145200&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/config/valve.xml (original)
+++ tomcat/trunk/webapps/docs/config/valve.xml Mon Jul 11 15:13:13 2011
@@ -103,6 +103,13 @@
         (relative to $CATALINA_BASE).</p>
       </attribute>
 
+      <attribute name="encoding" required="false">
+        <p>Character set used to write the log file. An empty string means
+        to use the system default character set. Default value: use the
+        system default character set.
+        </p>
+      </attribute>
+
       <attribute name="locale" required="false">
         <p>The locale used to format timestamps in the access log
            lines. Any timestamps configured using an



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org