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.LocationInfo;
import org.apache.log4j.spi.LoggingEvent; public class HtmlLogFormat extends HTMLLayout { public HtmlLogFormat() {
} 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); String title="Automation Log"; /**
* A string constant used in naming the option for setting the the HTML
* document title. Current value of this string constant is <b>Title</b>.
*/
public static final String TITLE_OPTION = "Title"; // Print no location info by default
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=\"Level\">");
if (event.getLevel().equals(Level.FATAL)) {
sbuf.append("<font color=\"#339933\">");
sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
sbuf.append("</font>");
} else if (event.getLevel().isGreaterOrEqual(Level.WARN)) {
sbuf.append("<font color=\"#993300\"><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); if (locationInfo) {
LocationInfo locInfo = event.getLocationInformation();
sbuf.append("<td title=\"Source\">");
sbuf.append(Transform.escapeTags(locInfo.getFileName()));
sbuf.append(':');
sbuf.append(locInfo.getLineNumber());
sbuf.append("</td>" + Layout.LINE_SEP);
} sbuf.append("<td title=\"Message\">");
sbuf.append(Transform.escapeTags(event.getRenderedMessage()));
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=\"4\">");
appendThrowableAsHTML(s, sbuf);
sbuf.append("</td></tr>" + Layout.LINE_SEP);
}
return sbuf.toString();
} 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);
}
}
} /**
* Returns appropriate HTML headers.
*/
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("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">" + Layout.LINE_SEP);
sbuf.append("<title>" + 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: 12px;}" + 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("<table cellspacing=\"0\" cellpadding=\"4\" border=\"1\" bordercolor=\"#224466\" width=\"100%\">" + Layout.LINE_SEP);
sbuf.append("<tr>" + Layout.LINE_SEP); sbuf.append("<th width='120'>Time</th>" + Layout.LINE_SEP);
sbuf.append("<th>Level</th>" + Layout.LINE_SEP); if (locationInfo) {
sbuf.append("<th>Source</th>" + Layout.LINE_SEP);
} sbuf.append("<th>Message</th>" + Layout.LINE_SEP);
sbuf.append("</tr>" + Layout.LINE_SEP);
sbuf.append("<br></br>" + Layout.LINE_SEP);
return sbuf.toString();
} }

htmlFormat的更多相关文章

  1. .net 生成 静态页面

    .net 生成 静态页面 <!--Main.Aspx--> <%@ page language="C#" %> <%@ import namespac ...

  2. 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract

    [源码下载] 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之  ...

  3. 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件

    [源码下载] 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件 作者:webabcd 介绍重新想象 Windows 8 Store ...

  4. Python实践:提取文章摘要

    一.概述 二.纯文本摘要 三.HTML摘要 一.概述 在博客系统的文章列表中,为了更有效地呈现文章内容,从而让读者更有针对性地选择阅读,通常会同时提供文章的标题和摘要. 一篇文章的内容可以是纯文本格式 ...

  5. .NET生成静态页面的方案总结

    转载自:http://www.cnblogs.com/cuihongyu3503319/archive/2012/12/06/2804233.html 方法一:在服务器上指定aspx网页,生成html ...

  6. python实现文章或博客的自动摘要(附java版开源项目)

    python实现文章或博客的自动摘要(附java版开源项目) 写博客的时候,都习惯给文章加入一个简介.现在可以自动完成了!TF-IDF与余弦相似性的应用(三):自动摘要 - 阮一峰的网络日志http: ...

  7. 生成html的几种方案

    方案1: ///   <summary > ///   传入URL返回网页的html代码 ///   </summary > ///   <param   name=&q ...

  8. html 转 js 字符串

    看到一个牛人的博客  http://riny.net/lab/#tools_html2js 看了下他的代码  挺棒的 所依赖的两个库在这里 https://github.com/Bubblings/l ...

  9. Play学习 - 体验网页模板

    在经过无数个尝试后,最终用sbt把play所依赖的所有包都下载下来了,现在可以非常快速编译运行了.今天体验了下网页模板,觉得非常不错,在这里做个简单的介绍. 原文说明: A Play Scala te ...

随机推荐

  1. Http TCP/IP 协议的关系

    转自:http://www.cnblogs.com/ymy124/archive/2012/03/18/2404958.html 项目要求Web服务是高安全级别,在选择.net remoting,we ...

  2. 夺命雷公狗-----React---27--小案例之react经典案例todos(清除已完成)

    这个功能其实也是很简单的,就只是让todos里面的内isDown进行取反即可 然后在Zong里面进行下修改即可 效果如下所示: 代码如下所示: <!DOCTYPE html> <ht ...

  3. win2008使用FireDac连接ORACLE数据库问题

    2008上装DELPHI XE7,无论用FireDac 还是Ado都连不上ORACLE数据库 --------------------------- Debugger Exception Notifi ...

  4. ES6里箭头函数的陷阱

    ECMAScript 6新增了箭头函数 原来的匿名函数 function(){},现在可以简化成()=>{} 看起来高大上,像C#什么的语法. 但是箭头函数的this对象,不能更改,总是指向函数 ...

  5. Google-glog 日志库使用手记

    作者:高张远瞩(HiLoveS) 博客:http://www.cnblogs.com/hiloves/ 转载请保留该信息 Google Glog 是一个C++语言的应用级日志记录框架,提供了 C++ ...

  6. ios学习-制作一个浏览图片的Demo

    一.项目要求:制作一个浏览图片的Demo,要求包含夜间模式,以及改变图片大小,能够显示不同的图片描述 二.开发步骤: 1.在storyboard上添加一个空白的View,然后添加”设置“按钮,添加im ...

  7. C语言 04 进制

    %d 或者%i 十进制 %c 输出字符 %p 输出地址 %f 输出小数 %o 八进制 %x 十六进制 一个int类型变量占4字节,占32bit(位) 例子:十进制 int=12  转二进制 0000 ...

  8. 初始angular框架(1)

    作为一个从来没有使用过的angular框架的小白  第一步肯定是 看文档!  没错就是看文档了!

  9. Tween.js的使用示例

    可参考:http://www.htmleaf.com/jQuery/Layout-Interface/201501271284.html 官方文档:https://github.com/tweenjs ...

  10. mongodb使用和下载

    1.下载地址:http://www.mongodb.org/downloads 2.解压缩到自己想要安装的目录,比如d:\mongodb 3.创建文件夹d:\mongodb\data\db.d:\mo ...