From 32a53ef0a3bcd39ae1f112a8a6b27a59cc31283e Mon Sep 17 00:00:00 2001 From: kkolinko Date: Mon, 11 Jul 2011 15:13:13 +0000 Subject: [PATCH] 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. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1145200 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/catalina/valves/AccessLogValve.java | 57 ++++++++++++++++++++-- .../apache/catalina/valves/LocalStrings.properties | 1 + webapps/docs/changelog.xml | 4 ++ webapps/docs/config/valve.xml | 7 +++ 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/java/org/apache/catalina/valves/AccessLogValve.java b/java/org/apache/catalina/valves/AccessLogValve.java index ad441d0d0..10a176b76 100644 --- a/java/org/apache/catalina/valves/AccessLogValve.java +++ b/java/org/apache/catalina/valves/AccessLogValve.java @@ -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 ValveBase implements AccessLog { * log file name suffix. */ protected Locale locale = Locale.getDefault(); - + + /** + * 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. */ @@ -786,6 +797,29 @@ public class AccessLogValve extends ValveBase implements AccessLog { locale = findLocale(localeName, locale); } + /** + * 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 /** @@ -984,9 +1018,22 @@ public class AccessLogValve extends ValveBase implements AccessLog { 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; diff --git a/java/org/apache/catalina/valves/LocalStrings.properties b/java/org/apache/catalina/valves/LocalStrings.properties index a2b791ef7..751fa1280 100644 --- a/java/org/apache/catalina/valves/LocalStrings.properties +++ b/java/org/apache/catalina/valves/LocalStrings.properties @@ -26,6 +26,7 @@ accessLogValve.closeFail=Failed to close 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}] +accessLogValve.unsupportedEncoding=Failed to set encoding to [{0}], will use the system default character set. # Error report valve errorReportValve.errorReport=Error report diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index e84024a77..29a9d6c95 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -62,6 +62,10 @@ Fix regression in year number formatting for AccessLogValve. (rjung) + + 46252: Allow to specify character set to be used to write + the access log in AccessLogValve. (kkolinko) + 51494: Prevent an NPE when a long running request completes if the associated web application was destroyed while the request was diff --git a/webapps/docs/config/valve.xml b/webapps/docs/config/valve.xml index 5bce40984..a4cf4ee67 100644 --- a/webapps/docs/config/valve.xml +++ b/webapps/docs/config/valve.xml @@ -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. +

+
+

The locale used to format timestamps in the access log lines. Any timestamps configured using an -- 2.11.0