Index: java/org/apache/catalina/valves/AccessLogValve.java =================================================================== --- java/org/apache/catalina/valves/AccessLogValve.java (revision 1145184) +++ java/org/apache/catalina/valves/AccessLogValve.java (working copy) @@ -21,10 +21,13 @@ 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.coyote.RequestInfo; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; +import org.apache.tomcat.util.buf.B2CConverter; /** @@ -301,8 +305,15 @@ * 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 null, the + * system default character set will be used. An empty string will be + * treated as null when this property is assigned. + */ + protected String encoding = null; + + /** * Array of AccessLogElement, they will be used to make log message. */ protected AccessLogElement[] logElements = null; @@ -522,6 +533,29 @@ this.fileDateFormat = fileDateFormat; } + /** + * Return the character set name that is used to write the log file. + * + * @return Character set name, or null 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 @@ 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 @@ 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 @@ 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 + 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)); + } + } + + 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 { - 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; - } - writer = new PrintWriter(new BufferedWriter(new FileWriter( - pathname, true), 128000), false); - - currentLogFile = new File(pathname); + 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); } } Index: java/org/apache/catalina/valves/LocalStrings.properties =================================================================== --- java/org/apache/catalina/valves/LocalStrings.properties (revision 1145207) +++ java/org/apache/catalina/valves/LocalStrings.properties (working copy) @@ -30,6 +30,11 @@ # 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 Index: java/org/apache/juli/FileHandler.java =================================================================== --- java/org/apache/juli/FileHandler.java (revision 1145188) +++ java/org/apache/juli/FileHandler.java (working copy) @@ -364,8 +364,12 @@ // 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: webapps/docs/config/valve.xml =================================================================== --- webapps/docs/config/valve.xml (revision 1145184) +++ webapps/docs/config/valve.xml (working copy) @@ -103,6 +103,13 @@ (relative to $CATALINA_BASE).

+ +

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. +

+
+

A formatting layout identifying the various information fields from the request and response to be logged, or the word