views:

784

answers:

4

I configured log4j SMTPAppender to send emails to me on errors. It works, but I have problem with non ASCII data. Configuration uses HTMLLayout and their documentation states:

Appenders using this layout should have their encoding set to UTF-8 or UTF-16, otherwise events containing non ASCII characters could result in corrupted log files.

My log files must contains UTF-8 data, but I can't find where I can set encoding for appender. Here is appender configuration:

<appender name="MAIL" class="org.apache.log4j.net.SMTPAppender">
 <param name="BufferSize" value="25" />
 <param name="SMTPHost" value="localhost" />
 <param name="From" value="[email protected]" />
 <param name="Threshold" value="error" />
 <param name="To" value="[email protected]" />
 <param name="Subject" value="Error Occurred" />
 <param name="LocationInfo" value="true"/>
 <param name="encoding" value="UTF-8" />
 <!--  <param name="Encoding" value="UTF-8"/> -->
 <layout class="org.apache.log4j.HTMLLayout">
 </layout>
</appender>

When application runs I got:

log4j:WARN No such property [encoding] in org.apache.log4j.net.SMTPAppender
A: 

According to the code of the latest SMTPAppender (1.2.15), not only there no setEncoding, but the encoding does not even mentioned there.

Do you actually see issues with it? If so, why not inheriting from SMTPAppender or provide your own implementation?

David Rabinowitz
Thank you, I have thought about it, but I hoped there are standart solution.
valodzka
A: 

set this property in log4j configuration: log4j.appender.MyAppender.Encoding=UTF-8

KingInk
Didn't work for me. Have you tried it?
cherouvim
I did what version of log4j are you running?
KingInk
+1  A: 

Have you looked at logback? Its SMTPAppender supports setting the character encoding.

Ceki
A: 

The latest (1.2.16) version solves this.

http://svn.apache.org/viewvc/logging/log4j/tags/v1_2_16/src/main/java/org/apache/log4j/net/SMTPAppender.java?revision=929374&amp;view=markup

subject: line 144
body: line 390
content type: line 394

It's also mentioned in the changelog http://logging.apache.org/log4j/1.2/changes-report.html#a1.2.16

SMTPAppender does not properly encode subject or content containing non-ASCII characters. Fixes 44644.
cherouvim