From 66011b62ae9508cf435726ae3d560cfcac7a30c3 Mon Sep 17 00:00:00 2001 From: kkolinko Date: Mon, 11 Jul 2011 16:46:28 +0000 Subject: [PATCH] * JULI FileHandler, AccessLogValve: 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 --- .../org/apache/catalina/valves/AccessLogValve.java | 50 +++++++++++++--------- .../apache/catalina/valves/LocalStrings.properties | 3 +- java/org/apache/juli/FileHandler.java | 8 +++- webapps/docs/changelog.xml | 11 ++++- 4 files changed, 47 insertions(+), 25 deletions(-) diff --git a/java/org/apache/catalina/valves/AccessLogValve.java b/java/org/apache/catalina/valves/AccessLogValve.java index 10a176b76..0c630729b 100644 --- a/java/org/apache/catalina/valves/AccessLogValve.java +++ b/java/org/apache/catalina/valves/AccessLogValve.java @@ -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); } } diff --git a/java/org/apache/catalina/valves/LocalStrings.properties b/java/org/apache/catalina/valves/LocalStrings.properties index 751fa1280..9ee19cf76 100644 --- a/java/org/apache/catalina/valves/LocalStrings.properties +++ b/java/org/apache/catalina/valves/LocalStrings.properties @@ -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}] diff --git a/java/org/apache/juli/FileHandler.java b/java/org/apache/juli/FileHandler.java index 500a245c3..857b87f49 100644 --- a/java/org/apache/juli/FileHandler.java +++ b/java/org/apache/juli/FileHandler.java @@ -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; diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 1f2f86f68..7421da225 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -79,6 +79,15 @@ 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) + + 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 prefix attribute. Earlier this happened only if it was + specified with the directory attribute. (kkolinko) + + + Log a failure if access log file cannot be opened. (kkolinko) + @@ -109,7 +118,7 @@ - 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) -- 2.11.0