log4j配置生成html形式的日志

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>&nbsp;&nbsp;&nbsp;&nbsp;";

    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完整路径
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值