5.log4j.properties例子:HTMLLayout

package com.v512.log4j;

import org.apache.log4j.Logger;

public class HelloLog4J {
	// 构造记录器,形参是记录器所在的类,表示要在该类做日志
	private static Logger logger = Logger.getLogger(HelloLog4J.class);
	/** 
	 * @param args
	 */
	public static void main(String[] args) {
		getMessage();
	}
	
	private static void getMessage() {
		// 记录下各种级别的信息,这些信息放在哪儿,以哪种方式存放,在log4j.properties文件中配置.
		logger.debug("This is debug message.");
		logger.info("This is a info message.");
		logger.warn("This is a warn message.");
		logger.error("This is a error message.");
	}

}

log4j.rootLogger=info,file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=Message.html
log4j.appender.file.layout=org.apache.log4j.HTMLLayout

Message.html(工程目录下),时间显示有问题
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Log4J Log Messages</title>
<style type="text/css">
<!--
body, table {font-family: arial,sans-serif; font-size: x-small;}
th {background: #336699; color: #FFFFFF; text-align: left;}
-->
</style>
</head>
<body bgcolor="#FFFFFF" topmargin="6" leftmargin="6">
<hr size="1" noshade>
Log session start time Fri Jul 27 14:22:23 CST 2012<br>
<br>
<table cellspacing="0" cellpadding="4" border="1" bordercolor="#224466" width="100%">
<tr>
<th>Time</th>
<th>Thread</th>
<th>Level</th>
<th>Category</th>
<th>Message</th>
</tr>

<tr>
<td>0</td>
<td title="main thread">main</td>
<td title="Level">INFO</td>
<td title="com.v512.log4j.HelloLog4J category">com.v512.log4j.HelloLog4J</td>
<td title="Message">This is a info message.</td>
</tr>

<tr>
<td>1</td>
<td title="main thread">main</td>
<td title="Level"><font color="#993300"><strong>WARN</strong></font></td>
<td title="com.v512.log4j.HelloLog4J category">com.v512.log4j.HelloLog4J</td>
<td title="Message">This is a warn message.</td>
</tr>

<tr>
<td>2</td>
<td title="main thread">main</td>
<td title="Level"><font color="#993300"><strong>ERROR</strong></font></td>
<td title="com.v512.log4j.HelloLog4J category">com.v512.log4j.HelloLog4J</td>
<td title="Message">This is a error message.</td>
</tr>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值