log emailの使用
HTML形式:
送信モード
LOGレベル
プロファイル
package com.just.zb.email.log;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.helpers.Transform;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.spi.LoggingEvent;
public class EmailHTMLLayout extends Layout
{
protected int BUF_SIZE = 256;
protected int MAX_CAPACITY = 1024;
static String TRACE_PREFIX = "<br> ";
private StringBuffer sbuf;
/** @deprecated */
public static final String LOCATION_INFO_OPTION = "LocationInfo";
public static final String TITLE_OPTION = "Title";
boolean locationInfo;
String title;
public EmailHTMLLayout()
{
this.BUF_SIZE = 256;
this.MAX_CAPACITY = 1024;
this.sbuf = new StringBuffer(256);
this.locationInfo = false;
this.title = "Log4J Log Messages";
}
public void setLocationInfo(boolean flag)
{
this.locationInfo = flag;
}
public boolean getLocationInfo()
{
return this.locationInfo;
}
public void setTitle(String title)
{
this.title = title;
}
public String getTitle()
{
return this.title;
}
public String getContentType() {
return "text/html;charset=GBK";
}
public void activateOptions()
{
}
private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public String format(LoggingEvent event)
{
if (this.sbuf.capacity() > 1024)
this.sbuf = new StringBuffer(256);
else {
this.sbuf.setLength(0);
}
this.sbuf.append(Layout.LINE_SEP + "<tr>" + Layout.LINE_SEP);
this.sbuf.append("<td>");
this.sbuf.append( format.format(new Date(event.timeStamp)));
this.sbuf.append("</td>" + Layout.LINE_SEP);
String escapedThread = Transform.escapeTags(event.getThreadName());
this.sbuf.append("<td title=\"" + escapedThread + " thread\">");
this.sbuf.append(escapedThread);
this.sbuf.append("</td>" + Layout.LINE_SEP);
this.sbuf.append("<td title=\"Level\">");
if (event.getLevel().equals(Level.INFO)) {
this.sbuf.append("<font color=\"#339933\">");
this.sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
this.sbuf.append("</font>");
}
else if (event.getLevel().isGreaterOrEqual(Level.WARN)) {
this.sbuf.append("<font color=\"#dc143c\"><strong>");
this.sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
this.sbuf.append("</strong></font>");
} else {
this.sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
}
this.sbuf.append("</td>" + Layout.LINE_SEP);
StringBuffer buff= new StringBuffer("");
buff.append(event.getLocationInformation().getClassName() );
buff.append(".");
buff.append(event.getLocationInformation().getMethodName() );
buff.append("(");
buff.append(event.getLocationInformation().getFileName() );
buff.append(":");
buff.append(event.getLocationInformation().getLineNumber());
buff.append(")");
// System.out.println(buff);
String lineLoger = Transform.escapeTags(buff.toString());
this.sbuf.append("<td title=\"" + lineLoger + " category\">");
this.sbuf.append(lineLoger);
this.sbuf.append("</td>" + Layout.LINE_SEP);
if (this.locationInfo) {
LocationInfo locInfo = event.getLocationInformation();
this.sbuf.append("<td>");
this.sbuf.append(Transform.escapeTags(locInfo.getFileName()));
this.sbuf.append(':');
this.sbuf.append(locInfo.getLineNumber());
this.sbuf.append("</td>" + Layout.LINE_SEP);
}
this.sbuf.append("<td title=\"Message\">");
this.sbuf.append(Transform.escapeTags(event.getRenderedMessage()));
this.sbuf.append("</td>" + Layout.LINE_SEP);
this.sbuf.append("</tr>" + Layout.LINE_SEP);
if (event.getNDC() != null) {
this.sbuf.append("<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : xx-small;\" colspan=\"6\" title=\"Nested Diagnostic Context\">");
this.sbuf.append("NDC: " + Transform.escapeTags(event.getNDC()));
this.sbuf.append("</td></tr>" + Layout.LINE_SEP);
}
String[] s = event.getThrowableStrRep();
if (s != null) {
this.sbuf.append("<tr><td bgcolor=\"#87ceeb\" style=\"color:White; font-size : xx-small;\" colspan=\"6\">");
appendThrowableAsHTML(s, this.sbuf);
this.sbuf.append("</td></tr>" + Layout.LINE_SEP);
}
return this.sbuf.toString();
}
void appendThrowableAsHTML(String[] s, StringBuffer sbuf) {
if (s != null) {
int len = s.length;
if (len == 0)
return;
sbuf.append(Transform.escapeTags(s[0]));
sbuf.append(Layout.LINE_SEP);
for (int i = 1; i < len; ++i) {
sbuf.append(TRACE_PREFIX);
sbuf.append(Transform.escapeTags(s[i]));
sbuf.append(Layout.LINE_SEP);
}
}
}
public String getHeader()
{
StringBuffer sbuf = new StringBuffer();
sbuf.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" + Layout.LINE_SEP);
sbuf.append("<html>" + Layout.LINE_SEP);
sbuf.append("<head>" + Layout.LINE_SEP);
sbuf.append("<title>" + this.title + "</title>" + Layout.LINE_SEP);
sbuf.append("<style type=\"text/css\">" + Layout.LINE_SEP);
sbuf.append("<!--" + Layout.LINE_SEP);
sbuf.append("body, table {font-family: arial,sans-serif; font-size: x-small;}" + Layout.LINE_SEP);
sbuf.append("th {background: #336699; color: #FFFFFF; text-align: left;}" + Layout.LINE_SEP);
sbuf.append("-->" + Layout.LINE_SEP);
sbuf.append("</style>" + Layout.LINE_SEP);
sbuf.append("</head>" + Layout.LINE_SEP);
sbuf.append("<body bgcolor=\"#FFFFFF\" topmargin=\"6\" leftmargin=\"6\">" + Layout.LINE_SEP);
sbuf.append("<hr size=\"1\" noshade>" + Layout.LINE_SEP);
sbuf.append("Log session start time " + new Date() + "<br>" + Layout.LINE_SEP);
sbuf.append("<br>" + Layout.LINE_SEP);
sbuf.append("<table cellspacing=\"0\" cellpadding=\"4\" border=\"1\" bordercolor=\"#224466\" width=\"100%\">" + Layout.LINE_SEP);
sbuf.append("<tr>" + Layout.LINE_SEP);
sbuf.append("<th>Time</th>" + Layout.LINE_SEP);
sbuf.append("<th>Thread</th>" + Layout.LINE_SEP);
sbuf.append("<th>Level</th>" + Layout.LINE_SEP);
sbuf.append("<th>Category</th>" + Layout.LINE_SEP);
if (this.locationInfo) {
sbuf.append("<th>File:Line</th>" + Layout.LINE_SEP);
}
sbuf.append("<th>Message</th>" + Layout.LINE_SEP);
sbuf.append("</tr>" + Layout.LINE_SEP);
return sbuf.toString();
}
public String getFooter()
{
StringBuffer sbuf = new StringBuffer();
sbuf.append("</table>" + Layout.LINE_SEP);
sbuf.append("<br>" + Layout.LINE_SEP);
sbuf.append("</body></html>");
return sbuf.toString();
}
public boolean ignoresThrowable()
{
return false;
}
}
送信モード
package com.just.zb.email.log;
import org.apache.log4j.net.SMTPAppender;
import org.apache.log4j.spi.LoggingEvent;
public class EmailSMTPAppender extends SMTPAppender {
public void append(LoggingEvent event) {
if (!checkEntryConditions()) {
return;
}
event.getThreadName();
event.getNDC();
event.getMDCCopy();
if (this.getLocationInfo()) {
event.getLocationInformation();
}
cb.add(event);
if (evaluator.isTriggeringEvent(event)) {
if (cb.length() > this.getBufferSize()-24) {
sendBuffer();
}
}
}
public EmailSMTPAppender() {
super();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
if (cb.length() > 0) {
sendBuffer();
}
}
});
}
}
LOGレベル
package com.just.zb.email.log;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.TriggeringEventEvaluator;
public class EmailTriggeringEventEvaluator implements TriggeringEventEvaluator {
public boolean isTriggeringEvent(LoggingEvent paramLoggingEvent ) {
return paramLoggingEvent.getLevel().isGreaterOrEqual(Level.INFO);
}
}
プロファイル
log4j.rootCategory=INFO,CONSOLE,FILE,EMAIL
log4j.category.com.just=INFO
#log4j.rootCategory=DEBUG,CONSOLE,FILE
#log4j.category.com.just=DEBUG
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%p]: %d{yyyy-MM-dd HH:mm:ss} %n |_[MESSAGE]: %m %n |_[SOURCE] : %l%n%n
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.file=log/utils.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[%p]: %d{yyyy-MM-dd HH:mm:ss} %n |_[MESSAGE]: %m %n |_[SOURCE] : %l%n%n
log4j.appender.EMAIL=com.just.zb.email.log.EmailSMTPAppender
log4j.appender.EMAIL.Threshold=INFO
log4j.appender.EMAIL.BufferSize=1024
[email protected]
log4j.appender.EMAIL.From=www.just-tech.com.cn
log4j.appender.EMAIL.Subject=SYSTEM ERROR[ TEST ]
log4j.appender.MAIL.SMTPDebug=true
log4j.appender.EMAIL.SMTPHost=mail.just-tech.com.cn
#log4j.appender.EMAIL.SMTPUsername=youemail
#log4j.appender.EMAIL.SMTPPassword=youpw
log4j.appender.EMAIL.EvaluatorClass=com.just.zb.email.log.EmailTriggeringEventEvaluator
log4j.appender.EMAIL.layout=com.just.zb.email.log.EmailHTMLLayout