When the project is on-line, the runtime often also has an exception, in the exception throws, want to get feedback immediately. So you need to configure the log4j send email function.
The original version of log4j used in the project is 1.2.9, but this version does not support the authentication function of the mail service, so the latest version 1.2.14 is downloaded again to view the source code
This version supports the authentication feature, the following is the specific configuration of log4j.properties:
# # ROOT
Log4j.rootlogger=info,console,a_default,mail
# # CONSOLE
Log4j.appender.console=org.apache.log4j.consoleappender
Log4j.appender.threshold=info
Log4j.appender.console.target=system.out
Log4j.appender.console.layout=org.apache.log4j.patternlayout
#log4j. Appender.console.layout.conversionpattern=[framework]%d-%c-%-4r [%t]%-5p%c%x-%m%n
log4j.appender.console.layout.conversionpattern=[Debug Information]%-5p%c%x-%m%n
# # A_default
Log4j.appender.a_default=org.apache.log4j.rollingfileappender
Log4j.appender.A_default. Threshold=info
Log4j.appender.A_default. File=${webframe.root}/web-inf/logs/railstone.log
Log4j.appender.A_default. MAXFILESIZE=4000KB
Log4j.appender.A_default. maxbackupindex=10
Log4j.appender.a_default.layout=org.apache.log4j.patternlayout
Log4j.appender.A_ default.layout.conversionpattern=[Debug Information]%-5p%c%x-%m%n
# # MAIL
Log4j.appender.mail=org.apache.log4j.net.smtpappender
# Log Error level
log4j.appender.mail.threshold=error
# cache file size, send email when log reaches 512K
log4j.appender.mail.buffersize=512
# server sending mail
log4j.appender.mail.smtphost=smtp.163.com
# Message title
Log4j.appender.mail.subject=log4j ErrorMessage
# User name
Log4j.appender.mail.smtpusername= mailbox user name
# password
log4j.appender.mail.smtppassword= mailbox password
# Sender Address
[email protected]
# recipient of log mail
[email protected"
# log Patternlayout
log4j.appender.mail.layout=org.apache.log4j.patternlayout
# log Format
log4j.appender.mail.layout.conversionpattern=[errormessage ]%d-%c-%-4r [%t]%-5p%c%x-%m%n
After the configuration is complete, it is found that even if you modify Log4j.appender.mail.threshold=error to the INFO level, only the log output level is ERROR (that is, call Logger.error ("message") in the Code method) to send the message without knowing what the
log4j send log mail to multiple recipients and title, text garbled problem
Using JavaMail to send mail, you need to import the package Mail.jar and Activation.jar These two packages, otherwise it is impossible to send e-mail, the bottom of the configuration file is shown in the Green Line is sent to two receiver AC and AE.
Here will appear in Chinese garbled problem, there are two main aspects of garbled, one is the title garbled, and the second is the body garbled. The two garbled solutions are described below.
First, the title garbled
The title of the LOG4J log message is set in the configuration file Log4j.properties, as follows
Log4j.appender.mail=org.apache.log4j.net.smtpappender
Log4j.appender.mail.threshold=fatal
log4j.appender.mail.buffersize=10
[email protected]
[email protected]
Log4j.appender.mail.subject= log4j reminds you that a serious system error has occurred
Log4j.appender.mail.to=ac @163.com,[email protected]
Log4j.appender.mail.layout=com.sun.definelayout
Log4j.appender.mail.layout.locationinfo=true
Gray Line is the title, log4j configuration file default reading is ISO-88591, encountered in Chinese will appear garbled, we can put this configuration file log4j.properties with the JDK tool native2asii conversion encoding.
Command: native2asii log4j.properties log4jxx.properties
Rename this log4jxx.properties to Log4j.properties to replace the original log4j.properties OK.
After the gray line is re-encoded:
Log4j.appender.mail.subject=log4j\u63d0\u9192\u60a8\uff1a\u7cfb\u7edf\u53d1\u751f\u4e86\u4e25\u91cd\u9519\ U8bef
Second, the body garbled
Text garbled, the solution is relatively simple. Read the log4j source class Smtpappender, we can find that the Sendbuffer () method has such a sentence:
Part.setcontent (Sbuf.tostring (), Layout.getcontenttype ());
We continue to track the layout patterns that are found in configuration files. However, these layout patterns are inherited from layout, and ContentType is only available through the getContentType method and cannot be modified. All layout modes The getContentType method returns "Text/plain";
In order to deal with Chinese garbled characters, we can write a layout mode. If you want to use Htmllayout, we'll write a htmllayout subclass that overrides the Htmllayout getContentType method. If I'm going 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 () {
return "TEXT/HTML;CHARSET=GBK";
}
}
The corresponding configuration file settings are shown in the Yellow line.
Source: http://jlk.iteye.com/blog/354216
Configuration Mode 2:
Log4j the role of sending log messages:
Project error information can be timely (real-time) reflected to the project maintenance staff and the relevant responsible person.
Advantages:
1. Quick response;
2. co-monitoring;
3. The message body directly displays the error message, the copy information is more convenient than the landing server to find;
4. With the log information continuing to write to the file, there is another channel to obtain information.
Supplement: Log4j can implement output to console, file, rollback file, send log mail, database, custom label.
An important class for sending messages is Smtpappender, which was preceded by log4j-1.2.8, and Smtpappender has no Smtppassword and Smtpusername properties in 1.2.8 's version. These two properties are the user name and password that the logon SMTP server sends authentication to.
Dependent jar Packages:
Log4j-1.2.15.jar (version less than Log4j-1.2.14.jar does not support SMTP authentication)
Mail.jar
Activation.jar
Configure in the Log4j.properties file:
# # # Send error through email. #log4j的邮件发送appender, if necessary you can write your own Appenderlog4j.appender.MAIL=org.apache.log4j.net.smtpappender# the threshold at which messages are sent, messages are sent only if they are equal to or higher than error (such as fatal) Log4j.appender.MAIL.Threshold=error# cache file size, send Emaillog4j.appender.MAIL.BufferSize when log reaches 10k=10#发送邮件的邮箱帐号log4j. Appender.MAIL.From[Email protected]. COM#SMTP Mail sending server address Log4j.appender.MAIL.SMTPHost=smtp.163. COM#SMTP send the authenticated account name Log4j.appender.MAIL.SMTPUsername[Email protected]. COM#SMTP send the password for the authentication account Log4j.appender.MAIL.SMTPPassword=xxx# whether to print debug information, if true, the output and the handshake between SMTP and other details Log4j.appender.MAIL.SMTPDebug=false#邮件主题log4j. Appender.MAIL.Subject=log4jerrormessage# to what mailbox, if you want to send to more than one mailbox, separated by commas; #如果需要发副本给某人, add the following line #log4j.appender.mail.bcc=[Email protected]log4j.appender.mail.to=[Email protected]log4j.appender.mail.layout=Org.apache.log4j.PatternLayoutlog4j.appender.MAIL.layout.ConversionPattern=[framework]%d-%c-%-4r[%t]%-5p%c%x-%m%n
The SMTP level of log4j is the error level by default
The default is the error level, that is to say, only the program error, you can receive mail. However, you can customize the level to inherit the Triggeringeventevaluator class, overwriting one of the methods inside:
1 @Override 2 Public Boolean istriggeringevent (loggingevent arg0) { 3 return arg0.getlevel (). Isgreaterorequal (level.info); 4 }
The Smtpappender implementation is that whenever the Istriggeringevent () method returns True, it will send the message. In this case, a program executes, and each event capable of triggering will form an email.
When the number of events reaches half of BufferSize, an e-mail message is sent:
Inherits the Smtpappender class and overrides the Append method;
1 @Override2 Public voidAppend (loggingevent event) {3 if(!checkentryconditions ()) { 4 return; 5 } 6 Event.getthreadname (); 7 EVENT.GETNDC (); 8 event.getmdccopy (); 9 if( This. Getlocationinfo ()) { Ten event.getlocationinformation (); One } A Cb.add (event); - if(Evaluator.istriggeringevent (event)) { - if(Cb.length () > This. GetBufferSize ()/2) { the Sendbuffer (); - } - } -}
Another problem arises, and when the program is finished, the events that are still in the buffer are not sent. Because the number of events is often less than half buffersize.
Public Smtpappender () { runtime.getruntime (). Addshutdownhook(new Thread () { @Override Public void run () { if (cb.length () > 0) { sendbuffer ();}} }); }
The log4j has a Web page format output:
Log4j.appender.mail.layout=org.apache.log4j.htmllayout
Solve the problem of Chinese garbled characters in Mail, inherit htmllayout, overwrite getContentType method
@Override Public String getContentType () { return "TEXT/HTML;CHARSET=GBK"; }
log4j Sending an email configuration