* JULI FileHandler, AccessLogValve:
authorkkolinko <kkolinko@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 11 Jul 2011 16:46:28 +0000 (16:46 +0000)
committerkkolinko <kkolinko@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 11 Jul 2011 16:46:28 +0000 (16:46 +0000)
Create a directory automatically when it is specified as a part of the file name, e.g. in the prefix attribute. Earlier this happened only if it was specified with the directory attribute.
* AccessLogValve:
Log a failure if access log file cannot be opened.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1145237 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/valves/AccessLogValve.java
java/org/apache/catalina/valves/LocalStrings.properties
java/org/apache/juli/FileHandler.java
webapps/docs/changelog.xml

index 10a176b..0c63072 100644 (file)
@@ -1008,36 +1008,44 @@ public class AccessLogValve extends ValveBase implements AccessLog {
         }
 
         // 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;
-            }
-            Charset charset = null;
-            if (encoding != null) {
-                try {
-                    charset = B2CConverter.getCharset(encoding);
-                } catch (UnsupportedEncodingException ex) {
-                    log.error(sm.getString(
-                            "accessLogValve.unsupportedEncoding", encoding), ex);
-                }
+        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));
             }
-            if (charset == null) {
-                charset = Charset.defaultCharset();
+        }
+
+        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 = new File(pathname);
+            currentLogFile = pathname;
         } catch (IOException e) {
             writer = null;
             currentLogFile = null;
+            log.error(sm.getString("accessLogValve.openFail", pathname), e);
         }
     }
  
index 751fa12..9ee19cf 100644 (file)
@@ -22,7 +22,8 @@ cometConnectionManagerValve.event=Exception processing event
 cometConnectionManagerValve.listenerEvent=Exception processing session listener event
 
 # Access log valve
-accessLogValve.closeFail=Failed to close log file
+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.invalidLocale=Failed to set locale to [{0}]
index 500a245..857b87f 100644 (file)
@@ -367,8 +367,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;
index 1f2f86f..7421da2 100644 (file)
         When generating access logs for errors, log at the Context/Host level if
         a Context or Host can be identified for the failed request. (markt)
       </fix>
+      <update>
+        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. (kkolinko)
+      </update>
+      <fix>
+        Log a failure if access log file cannot be opened. (kkolinko)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">
   <subsection name="Cluster">
     <changelog>
       <update>
-        Remove unnecessary serverl.xml parsing code for old cluster
+        Remove unnecessary server.xml parsing code for old cluster
         implementation that does not ship as part of Tomcat 7. (markt)
       </update>
     </changelog>