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>