Log4j 如何发送到邮箱

 
log4j发错误日志到邮箱
log配置文件:
log4j.rootLogger=INFO, A1 ,MAIL
#A1:输出到控制台
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n
#MAIL:发送到邮件
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.Threshold=ERROR
log4j.appender.MAIL.BufferSize=10
log4j[email protected]
log4j.appender.MAIL.SMTPHost=smtp.163.com
log4j[email protected]
log4j.appender.MAIL.SMTPPassword=password
log4j.appender.MAIL.SMTPDebug=false
log4j[email protected]
log4j.appender.MAIL.Subject=Log4J\u63d0\u9192\u60a8\uff1a\u7cfb\u7edf\u53d1\u751f\u4e86\u4e25\u91cd\u9519\u8bef #国际化转换了,邮件标题不是乱码
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
#log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#log4j.appender.MAIL.layout.ConversionPattern=[ErrorMessage] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#log4j.appender.MAIL.layout=org.apache.log4j.HTMLLayout
log4j.appender.MAIL.layout=examples.net.stmp.DefineLayOut #处理了乱码的layout
log4j.appender.MAIL.layout.LocationInfo=true


日志输出类StmpAppenders :

package examples.net.stmp;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import java.io.IOException;
import java.net.URL;
public class StmpAppenders {

	private static Logger logger =
		Logger.getLogger(StmpAppenders.class);

	public static void main(String argv[]) {
		// Use a PropertyConfigurator to initialize from a property file.
		String resource =	"/examples/net/Stmp/example.properties";
		URL configFileResource = StmpAppenders.class.getResource(resource);
		PropertyConfigurator.configure(configFileResource);
		// Add a bunch of logging statements ...
		logger.debug("Hello, my name is Homer Simpson.");
		logger.info("We are the Simpsons!");
		logger.warn("Truly, yours is a butt that won't quit." +
		"- Bart, writing as Woodrow to Ms. Krabappel.");
		logger.error("日本語:あいうえお");
		logger.error("日本語:たちちてと",
		new IOException("Dumpsville, USA"));
	
		logger.fatal("Eep.");
		logger.fatal("恩 .... 巧克力.",
		new SecurityException("Fatal Exception"));
	}
}



乱码处理类:
DefineLayOut:
package examples.net.stmp;
import org.apache.log4j.HTMLLayout; 
public class DefineLayOut extends HTMLLayout{ 
	public String getContentType() { 
		return "text/html;charset=UTF-8"; 
	} 
}

你可能感兴趣的:(Log4j 如何发送到邮箱)