Log4j提供的layout有以下几种:
org.apache.log4j.HTMLLayout(以HTML表格形式布局),
org.apache.log4j.PatternLayout(可以灵活地指定布局模式),
org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),
org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)
输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL
log4j配置配置自定义输出html格式日志
首先要自定义自己的输出的格式继承HTMLLayout
package com.pressure.log;
import java.text.SimpleDateFormat;
import org.apache.log4j.HTMLLayout;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.helpers.Transform;
import org.apache.log4j.spi.LoggingEvent;
// 解析日志的问题—生成html的日志
public class HTMLLogFormatLayOut extends HTMLLayout {
public HTMLLogFormatLayOut() {
}
protected final int BUF_SIZE = 256;
protected final int MAX_CAPACITY = 1024;
static String TRACE_PREFIX = "<br> ";
private StringBuffer sbuf = new StringBuffer(BUF_SIZE);
public static final String TITLE_OPTION = "Title";
boolean locationInfo = true;
public String format(LoggingEvent event) {
if (sbuf.capacity() > MAX_CAPACITY) {
sbuf = new StringBuffer(BUF_SIZE);
} else {
sbuf.setLength(0);
}
sbuf.append(Layout.LINE_SEP + "<tr>" + Layout.LINE_SEP);
/* 日志生成时间 */
sbuf.append("<td>");
sbuf.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new java.util.Date()));
sbuf.append("</td>" + Layout.LINE_SEP);
/* 日志级别 */
sbuf.append("<td title='日志级别'>");
if (event.getLevel().equals(Level.FATAL)) {
sbuf.append("<font color=\"red\">");
sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
sbuf.append("</font>");
} else if (event.getLevel().isGreaterOrEqual(Level.WARN)) {
sbuf.append("<font color=\"yellow\"><strong>");
sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
sbuf.append("</strong></font>");
} else {
sbuf.append("<font color=\"green\">");
sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
sbuf.append("</font>");
}
sbuf.append("</td>" + Layout.LINE_SEP);
/* 日志内容 */
String content = Transform.escapeTags(event.getRenderedMessage());
if (content.contains("|")) {
String[] temp = content.split("\\|");
sbuf.append("<td title='类型'>");
sbuf.append(temp[0]);
sbuf.append("</td>" + Layout.LINE_SEP);
sbuf.append("<td title='手机号'>");
sbuf.append(temp[1]);
sbuf.append("</td>" + Layout.LINE_SEP);
sbuf.append("<td title='验证码'>");
sbuf.append(temp[2]);
sbuf.append("</td>" + Layout.LINE_SEP);
} else {
sbuf.append("<td title='日志内容'>");
sbuf.append(content);
sbuf.append("</td>" + Layout.LINE_SEP);
}
sbuf.append("</tr>" + Layout.LINE_SEP);
if (event.getNDC() != null) {
sbuf.append("<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : xx-small;\" colspan=\"6\" title=\"Nested Diagnostic Context\">");
sbuf.append("NDC: " + Transform.escapeTags(event.getNDC()));
sbuf.append("</td></tr>" + Layout.LINE_SEP);
}
String[] s = event.getThrowableStrRep();
if (s != null) {
sbuf.append("<tr><td bgcolor=\"#993300\" style=\"color:White; font-size : xx-small;\" colspan=\"6\">");
appendThrowableAsHTML(s, sbuf);
sbuf.append("</td></tr>" + Layout.LINE_SEP);
}
return sbuf.toString();
}
/**
* 生成HTML
*
* @param s
* @param sbuf
*/
private 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() {
// 日志文件 的头
String title = "日志文件";
// 日志文件 表格的 头
StringBuffer sbuf = new StringBuffer();
sbuf.append("<!DOCTYPE HTML>" + Layout.LINE_SEP);
sbuf.append("<html>" + Layout.LINE_SEP);
sbuf.append("<head>" + Layout.LINE_SEP);
sbuf.append("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">");
sbuf.append("<title>" + title + "</title>" + Layout.LINE_SEP);
sbuf.append("<style type=\"text/css\">" + Layout.LINE_SEP);
sbuf.append("body, table {font-family: 'Courier New',arial,sans-serif; font-size: 13px;}"
+ Layout.LINE_SEP);
sbuf.append("th {background: #336699; color: #FFFFFF; text-align: center;}"
+ 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("<table cellspacing=\"0\" cellpadding=\"1\" border=\"1\" bordercolor=\"#224466\" width=\"100%\">"
+ Layout.LINE_SEP);
sbuf.append("<tr>" + Layout.LINE_SEP);
sbuf.append("<th style='width:10%'>时间</th>" + Layout.LINE_SEP);
sbuf.append("<th style='width:10%'>日志级别</th>" + Layout.LINE_SEP);
sbuf.append("<th style='width:10%'>类型</th>" + Layout.LINE_SEP);
sbuf.append("<th style='width:10%'>手机号</th>" + Layout.LINE_SEP);
sbuf.append("<th style='width:10%'>验证码</th>" + Layout.LINE_SEP);
sbuf.append("</tr>" + Layout.LINE_SEP);
sbuf.append("<br></br>" + Layout.LINE_SEP);
return sbuf.toString();
}
}
log4j的配置如下:
log4j.appender.HTMLLogFormatLayOut=org.apache.log4j.DailyRollingFileAppender
log4j.appender.HTMLLogFormatLayOut.File=/logs/log_html.html
log4j.appender.HTMLLogFormatLayOut.encoding=UTF-8
log4j.appender.HTMLLogFormatLayOut.layout = 自定义的HTMLLogFormatLayOut完整路径