Author: kkolinko
Date: Thu Jul 21 10:25:17 2011
New Revision: 1149099

URL: http://svn.apache.org/viewvc?rev=1149099&view=rev
Log:
AccessLogValve and FileHandler improvements
Fixes https://issues.apache.org/bugzilla/show_bug.cgi?id=46252
It is backport of r1145200, r1145237, r1145268

* AccessLogValve:
  - Allow to specify character set to be used to write the access log in 
AccessLogValve
  - Create directory automatically if it is a part of the file name
  - Log a failure if access log file cannot be opened.
  - I18n of messages
  - Expose the new "encoding" option through JMX. Fix wrong mapping for 
"enabled" property.
* JULI FileHandler:
  - Create directory automatically if it is a part of the file name

Modified:
    tomcat/tc6.0.x/trunk/STATUS.txt
    tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/AccessLogValve.java
    tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/LocalStrings.properties
    tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/mbeans-descriptors.xml
    tomcat/tc6.0.x/trunk/java/org/apache/juli/FileHandler.java
    tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml
    tomcat/tc6.0.x/trunk/webapps/docs/config/valve.xml

Modified: tomcat/tc6.0.x/trunk/STATUS.txt
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/STATUS.txt?rev=1149099&r1=1149098&r2=1149099&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/STATUS.txt (original)
+++ tomcat/tc6.0.x/trunk/STATUS.txt Thu Jul 21 10:25:17 2011
@@ -215,24 +215,6 @@ PATCHES PROPOSED TO BACKPORT:
   +1: markt, kkolinko, kfujino
   -1:
 
-* Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=46252
-  AccessLogValve and FileHandler improvements
-  It is backport of r1145200, r1145237, r1145268
-  1) Allow to specify character set to be used to write the access log
-     in AccessLogValve
-  2) In JULI FileHandler and in AccessLogValve create a directory
-     automatically when it is specified as a part of the file name, e.g. in
-     the <code>prefix</code> attribute. Earlier this happened only if it was
-     specified with the <code>directory</code> attribute.
-  3) Log a failure if access log file cannot be opened.
-  4) I18n of messages in AccessLogValve.
-  5) Expose the new "encoding" option through JMX.
-     and also fix wrong mapping for "enabled" property - it is getEnabled().
-  https://issues.apache.org/bugzilla/attachment.cgi?id=27279
-  https://issues.apache.org/bugzilla/attachment.cgi?id=27280 (JMX)
-  +1: kkolinko, markt, kfujino
-  -1:
-
 * Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49595
   http://svn.apache.org/viewvc?rev=1148216&view=rev
   +1: jfclere

Modified: 
tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/AccessLogValve.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/AccessLogValve.java?rev=1149099&r1=1149098&r2=1149099&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/AccessLogValve.java 
(original)
+++ tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/AccessLogValve.java 
Thu Jul 21 10:25:17 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;
@@ -47,6 +50,7 @@ import org.apache.catalina.util.StringMa
 import org.apache.coyote.RequestInfo;
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
+import org.apache.tomcat.util.buf.B2CConverter;
 
 
 /**
@@ -301,7 +305,14 @@ public class AccessLogValve extends Valv
      * Date format to place in log file name. Use at your own risk!
      */
     protected String fileDateFormat = null;
-    
+
+    /**
+     * 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.
      */
@@ -522,6 +533,29 @@ public class AccessLogValve extends Valv
         this.fileDateFormat =  fileDateFormat;
     }
 
+    /**
+     * 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
 
     /**
@@ -597,7 +631,7 @@ public class AccessLogValve extends Valv
             try {
                 holder.renameTo(new File(newFileName));
             } catch (Throwable e) {
-                log.error("rotate failed", e);
+                log.error(sm.getString("accessLogValve.rotateFail"), e);
             }
 
             /* Make sure date is correct */
@@ -667,7 +701,7 @@ public class AccessLogValve extends Valv
                     try {
                         close();
                     } catch (Throwable e) {
-                        log.info("at least this wasn't swallowed", e);
+                        log.info(sm.getString("accessLogValve.closeFail"), e);
                     }
 
                     /* Make sure date is correct */
@@ -717,26 +751,51 @@ public class AccessLogValve extends Valv
         File dir = new File(directory);
         if (!dir.isAbsolute())
             dir = new File(System.getProperty("catalina.base"), directory);
-        dir.mkdirs();
+        if (!dir.exists()) {
+            if (!dir.mkdirs()) {
+                log.error(sm.getString("accessLogValve.openDirFail", dir));
+            }
+        }
 
         // Open the current log file
-        try {
-            String pathname;
-            // If no rotate - no need for dateStamp in fileName
-            if (rotatable) {
-                pathname = dir.getAbsolutePath() + File.separator + prefix
-                        + dateStamp + suffix;
-            } else {
-                pathname = dir.getAbsolutePath() + File.separator + prefix
-                        + suffix;
+        File pathname;
+        // If no rotate - no need for dateStamp in fileName
+        if (rotatable) {
+            pathname = new File(dir.getAbsoluteFile(), prefix + dateStamp
+                    + suffix);
+        } else {
+            pathname = new File(dir.getAbsoluteFile(), prefix + suffix);
+        }
+        File parent = pathname.getParentFile();
+        if (!parent.exists()) {
+            if (!parent.mkdirs()) {
+                log.error(sm.getString("accessLogValve.openDirFail", parent));
             }
-            writer = new PrintWriter(new BufferedWriter(new FileWriter(
-                    pathname, true), 128000), false);
-            
-            currentLogFile = new File(pathname);
+        }
+
+        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();
+        }
+
+        try {
+            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
+                    new FileOutputStream(pathname, true), charset), 128000),
+                    false);
+
+            currentLogFile = pathname;
         } catch (IOException e) {
             writer = null;
             currentLogFile = null;
+            log.error(sm.getString("accessLogValve.openFail", pathname), e);
         }
     }
  

Modified: 
tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/LocalStrings.properties
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/LocalStrings.properties?rev=1149099&r1=1149098&r2=1149099&view=diff
==============================================================================
--- 
tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/LocalStrings.properties 
(original)
+++ 
tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/LocalStrings.properties 
Thu Jul 21 10:25:17 2011
@@ -30,6 +30,11 @@ cometConnectionManagerValve.listenerEven
 # Access log valve
 accessLogValve.alreadyStarted=Access Logger has already been started
 accessLogValve.notStarted=Access Logger has not yet been started
+accessLogValve.openFail=Failed to open access log file [{0}]
+accessLogValve.closeFail=Failed to close access log file
+accessLogValve.openDirFail=Failed to create directory [{0}] for access logs
+accessLogValve.rotateFail=Failed to rotate access log
+accessLogValve.unsupportedEncoding=Failed to set encoding to [{0}], will use 
the system default character set.
 
 # Error report valve
 errorReportValve.errorReport=Error report

Modified: 
tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/mbeans-descriptors.xml
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/mbeans-descriptors.xml?rev=1149099&r1=1149098&r2=1149099&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/mbeans-descriptors.xml 
(original)
+++ tomcat/tc6.0.x/trunk/java/org/apache/catalina/valves/mbeans-descriptors.xml 
Thu Jul 21 10:25:17 2011
@@ -35,7 +35,7 @@
 
      <attribute name="enabled"
                description="Enable Access Logging"
-               is="true"
+               is="false"
                type="boolean"/>
 
     <attribute name="containerName"
@@ -46,6 +46,10 @@
                description="The directory in which log files are created"
                type="java.lang.String"/>
 
+    <attribute name="encoding"
+               description="Character set used to write the log file"
+               type="java.lang.String"/>
+
     <attribute   name="pattern"
                description="The pattern used to format our access log lines"
                type="java.lang.String"/>
@@ -132,7 +136,7 @@
 
      <attribute name="enabled"
                description="Enable Access Logging"
-               is="true"
+               is="false"
                type="boolean"/>
  
     <attribute name="containerName"
@@ -143,6 +147,10 @@
                description="The directory in which log files are created"
                type="java.lang.String"/>
 
+    <attribute name="encoding"
+               description="Character set used to write the log file"
+               type="java.lang.String"/>
+
     <attribute   name="pattern"
                description="The pattern used to format our access log lines"
                type="java.lang.String"/>

Modified: tomcat/tc6.0.x/trunk/java/org/apache/juli/FileHandler.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/juli/FileHandler.java?rev=1149099&r1=1149098&r2=1149099&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/java/org/apache/juli/FileHandler.java (original)
+++ tomcat/tc6.0.x/trunk/java/org/apache/juli/FileHandler.java Thu Jul 21 
10:25:17 2011
@@ -364,8 +364,12 @@ public class FileHandler
         // Open the current log file
         writerLock.writeLock().lock();
         try {
-            String pathname = dir.getAbsolutePath() + File.separator +
-                prefix + (rotatable ? date : "")  + suffix;
+            File pathname = new File(dir.getAbsoluteFile(), prefix
+                    + (rotatable ? date : "") + suffix);
+            File parent = pathname.getParentFile();
+            if (!parent.exists()) {
+                parent.mkdirs();
+            }
             String encoding = getEncoding();
             FileOutputStream fos = new FileOutputStream(pathname, true);
             OutputStream os = bufferSize>0?new 
BufferedOutputStream(fos,bufferSize):fos;

Modified: tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml?rev=1149099&r1=1149098&r2=1149099&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml Thu Jul 21 10:25:17 2011
@@ -55,6 +55,10 @@
         Stephane Bailliez. (markt) 
       </fix>
       <add>
+        <bug>46252</bug>: Allow to specify character set to be used to write
+        the access log in AccessLogValve. (kkolinko)
+      </add>
+      <add>
         <bug>48863</bug>: Provide an warning if there is a problem with a class
         path entry but use debug level logging if it is expected due to 
catalina
         home/base split. (kkolinko)
@@ -148,6 +152,17 @@
         <bug>51403</bug>: Avoid NPE in JULI FileHandler if formatter is
         misconfigured. (kkolinko)
       </add>
+      <update>
+        Create a directory for access log or error log (in AccessLogValve and
+        in JULI FileHandler) automatically when it is specified as a part of
+        the file name, e.g. in the <code>prefix</code> attribute. Earlier this
+        happened only if it was specified with the <code>directory</code>
+        attribute. (kkolinko)
+      </update>
+      <fix>
+        Log a failure if access log file cannot be opened. Improve i18n
+        of messages. (kkolinko)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">

Modified: tomcat/tc6.0.x/trunk/webapps/docs/config/valve.xml
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/webapps/docs/config/valve.xml?rev=1149099&r1=1149098&r2=1149099&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/webapps/docs/config/valve.xml (original)
+++ tomcat/tc6.0.x/trunk/webapps/docs/config/valve.xml Thu Jul 21 10:25:17 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="pattern" required="false">
         <p>A formatting layout identifying the various information fields
         from the request and response to be logged, or the word



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

Reply via email to