欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Log4j简单应用 博客分类: J2SE log4jJavathreadApacheJ# 

程序员文章站 2024-03-18 21:28:34
...

Log4j主页:http://logging.apache.org/

Log4j是一款强大的日志记录工具,允许用户对日志记录作出灵活的定义。

测试程序:

package org.eleaf.log4j;

import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Log4jTest {
static Logger logger = Logger.getLogger(Log4jTest.class.getClass()); //定义日志记录器
/**
* @param args
*/
public static void main(String[] args) {
Properties property = new Properties();
try {
property.load(Log4jTest.class.getResourceAsStream("log4jtest.properties"));//获取属性文件
} catch (IOException e) {
}
PropertyConfigurator.configure(property);
logger.info ("Begin Log4jTest");
for (int i = 0; i < 10; i++)
{
try
{
int j = random(); //返回一个0-9的随机数。
logger.warn (i + "/" + j + "=" + (i/j)); //如果分母为0,抛出异常,记录error;否则记录warn.
} catch (Exception e) {
logger.error ("error:" + e.getMessage());
}
}
logger.info ("End Log4jTest");
}
private static int random() //返回一个0-9的随机数。
{
return (int)(Math.random()*10);
}

}

 

log4jtest.properties属性文件:(应与Log4jTest.class在同一目录中)

 

 

##info指明日志级别,stdout,file是两个不同的输出目的地。

log4j.rootLogger=info, stdout, file

##输出目的地stdout,即标准输出。

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

##定义输出格式:
log4j.appender.stdout.layout.ConversionPattern=[run:%r][date:%d{yyyy-MM-dd HH:mm:ss}][catalog:%c][priority:%p][message:%m][location:%l][thread:%t]%n

##输出目的地file,输出到文件log4jtest.log。

log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=log4jtest.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[run:%r][date:%d{yyyy-MM-dd HH:mm:ss}][catalog:%c][priority:%p][message:%m][location:%l][thread:%t]%n


 

控制台标准输出结果:

 

 

[run:0][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:INFO][message:Begin Log4jTest][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:22)][thread:main]
[run:31][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:WARN][message:0/8=0][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:28)][thread:main]
[run:31][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:WARN][message:1/1=1][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:28)][thread:main]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:WARN][message:2/8=0][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:28)][thread:main]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:ERROR][message:error:/ by zero][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:30)][thread:main]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:WARN][message:4/4=1][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:28)][thread:main]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:ERROR][message:error:/ by zero][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:30)][thread:main]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:WARN][message:6/9=0][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:28)][thread:main]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:WARN][message:7/4=1][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:28)][thread:main]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:WARN][message:8/1=8][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:28)][thread:main]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:WARN][message:9/9=1][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:28)][thread:main]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:INFO][message:End Log4jTest][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:33)][thread:main]

日志文件输出结果:

 

 

[run:0][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:INFO ][message:Begin Log4jTest][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:22)][thread:main]
[run:31][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:WARN][message:0/8=0][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:28)][thread:main]
[run:31][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:WARN][message:1/1=1][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:28)][thread:main]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:WARN][message:2/8=0][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:28)][thread:main]
[run:47][date:2005-10-18 17:16:58 ][catalog:java.lang.Class ][priority:ERROR ][message:error:/ by zero ][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:30 )][thread:main ]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:WARN][message:4/4=1][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:28)][thread:main ]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:ERROR][message:error:/ by zero][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:30)][thread:main ]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:WARN][message:6/9=0][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:28)][thread:main]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:WARN][message:7/4=1][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:28)][thread:main]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:WARN][message:8/1=8][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:28)][thread:main]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:WARN][message:9/9=1][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:28)][thread:main]
[run:47][date:2005-10-18 17:16:58][catalog:java.lang.Class][priority:INFO ][message:End Log4jTest][location:org.eleaf.log4j.Log4jTest.main(Log4jTest.java:33)][thread:main]