LOG4J configuration error message send mail

Source: Internet
Author: User
Tags log4j

Recently not very busy, looked at the log4j, and then see log4j inside there are appender to mail functions, the individual did a bit of understanding and experiments, wrote a simple demo did a point test.

Log4j the role of sending log messages:
Project error information can be reflected in time (real-time) to project maintenance personnel and related principals.

Advantages:
1. Quick response;
2. Joint supervision;
3. Message text directly shows the error message, copy information than the landing server to find more convenient;
4. As the log information continues to be written to the document, there is another channel for obtaining information.

First: Download the latest jar package on the Apache website and refer to it in the project (Activation.jar,mail.jar), in addition to the log4j jar package, which is (Log4j-1.2.16.jar)

In the process of testing has found a number of issues and solutions, and you share the following:

1. The first time I test is the level of info used, so in the process of sending mail error, the information will not print out, and Mail also can not receive, I am very strange, and then I put the log level to error, the console only print out mail sent failed log information, Lookup found: log4j SMTP level defaults to the error level
The default is the error level, that is to say, only the program error, you can receive the message. However, you can customize the level, inherit the Triggeringeventevaluator class, concrete implementation see the following code:

public class Imtriggeringeventevaluator implements Triggeringeventevaluator {

	@Override public
	Boolean Istriggeringevent (Loggingevent arg0) {return

		arg0.getlevel (). isgreaterorequal (Level.debug);
	}

}
Custom Appender:
Import Org.apache.log4j.net.SMTPAppender;
Import org.apache.log4j.spi.LoggingEvent;

public class Imsmtpappender extends Smtpappender {public
	Imsmtpappender () {
		super (new Imtriggeringeventevaluator ());
		Runtime.getruntime (). Addshutdownhook (New Thread () {public
			void run () {
				if (cb.length () > 0) {
					Sendbuffer ();}}}
		);
	

This can be achieved at this time than the >=debug level.

2 Log error information seen, reported the following errors:

Javax.mail.MessagingException:Could not connect to SMTP host:smtp.163.com, port:25;

This time through the Internet to find, only to find that my local machine installed MacFee anti-virus software, the software inside a port protection rule will be port 25 to prevent the mass mail worm to send mail. And then turn off the kill or get rid of the rule, OK.

3 will be reported after it is removed.

530 5.7.0 must issue a STARTTLS command a
At this time I used the Gamil SMTP server, I replaced 163 of the error, the lookup is that some mail server will deny this way to connect to the mail server

4 Although connected to 163 mail server success, but at this time will be reported authenticationfailedexception abnormal, after lookup, is the mail server to the from the user name and password to do authentication, authenticated to send, But this time I did not find my log4j parameter file can be configured to configure Smtpusername properties, through the lookup found that I am currently using log4j1.2.6, in the 1.2.6 version, Smtpappender did not Smtppassword and Smtpusername properties, not to mention validation. These two properties are the logon SMTP server to send authenticated username and password. These two properties are only available after the 1.2.9 version.

LOG4J also provides a mail-certified API, but online users have also encountered such problems, solution a custom Appender Inherit Org.apache.log4j.net.SMTPAppender, which encapsulates the authentication logic in this class, B uses log4j1.2. More than 14 version, 1.2.14 and above the version of the integration of the authentication function, so I decisively put my 1.2 6 version of 1.2.16, decisive OK.

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.