log4j send log mail to multiple receiver and title, text garbled problem

Source: Internet
Author: User
Tags log4j

Previously developed systems do not have a separate log management, all the log unified output to a file in tomcat background, a few days is several G, now to the overall increase of a log4j management log function, in fact, the information on the Internet is many. The configuration of the message also has, but the specific how to send, garbled problem how to solve that less.

Use JavaMail to send mail, you need to import package Mail.jar and Activation.jar These two packages, otherwise it is impossible to send mail, the bottom of the Green Line in the configuration file is sent to two recipients AC and AE.

Here will appear the Chinese garbled problem, mainly has two aspects of garbled, one is the title garbled, the second is the text garbled. Below specify these two kinds of garbled solution.
First, the title garbled
The title of the LOG4J log message is set in the configuration file Log4j.properties, as follows
Log4j.appender.mail.subject= log4j remind you that a critical error has occurred in the system
Log4j.appender.mail.to=ac @163.com,ae@163.com

Gray Line is the title, log4j profile default read mode is ISO-88591, encountered in Chinese will be garbled, we can put this profile log4j.properties with JDK tool native2asii to convert the encoding mode.
Command: native2asii log4j.properties log4jxx.properties
This log4jxx.properties renamed as Log4j.properties instead of the original log4j.properties on the OK.
After the gray line is encoded:
log4j.appender.mail.subject=log4j/u63d0/u9192/u60a8/uff1a/u7cfb/u7edf/u53d1/u751f/u4e86/u4e25/u91cd/u9519/ U8bef

Second, the text garbled
Text garbled, the solution is relatively simple. Read the log4j source class Smtpappender, we can see that the Sendbuffer () method has this sentence:
Part.setcontent (Sbuf.tostring (), Layout.getcontenttype ());
We continue to trace the discovery that layout is the layout pattern for the layout attribute in the configuration file. However, these layout patterns are inherited from layout, and ContentType can only be obtained through the getContentType method and cannot be modified. All layout modes getContentType method return is "Text/plain";
To handle Chinese garbled, we can write a layout pattern. If you want to use Htmllayout, we'll write a htmllayout subclass that overrides the Htmllayout getContentType method. If I want to use Org.apache.log4j.HTMLLayout. We can write a Definelayout class with the following code:
Package Com.sun;

Import Org.apache.log4j.HTMLLayout;
public class Definelayout extends htmllayout{
Public String getContentType () {
The corresponding configuration file settings are shown in yellow lines.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.