在src目录下创建log4j.properties,具体内容为:
log4j.rootLogger=WARN, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%-5p] %l - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%-5p] %l - %m%n
# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN
以上配置是将日志信息输入到控制台和example.log文件中
将log4j-1.2.15.jar和commons-logging-1.1.1.jar(随便哪个版本都可以) 放到WEB-INF/lib下
准备工作完毕,现在开始调用的代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.log4j.Logger;
public class chatService
{
private static final Logger log = Logger.getLogger(chatService.class);
public static void main(String[] args)
{
log.error("hello word");
}
}