/**************************************************/
/* HelloWorld */
/**************************************************/
1:把log4j-1.2.8.jar添加到CLASSPATH环境变量
2:编写java代码
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
public class HelloWorld
{
static Logger logger = Logger.getLogger("HelloWorld");
static public void main(String[] args)
{
BasicConfigurator.configure();
logger.debug("Hello World.");
}
}
3:编译执行
/**************************************************/
/* HelloWorldConfiguration */
/**************************************************/
1:把log4j-1.2.8.jar添加到CLASSPATH环境变量
2:编写java代码
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class HelloWorldConfiguration
{
static Logger logger = Logger.getLogger("HelloWorldConfiguration");
static public void main(String[] args)
{
PropertyConfigurator.configure("log4j.properties");
logger.debug("Hello World.");
}
}
3:编写配置文件
# 根logger有一个appender:logfile
log4j.rootLogger=ALL, logfile
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=helloworldconfiguration.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
4:编译执行,会生成helloworldconfiguration.log文件
阅读(938) | 评论(0) | 转发(0) |