eclipse环境下日志打印输出

时间:2024-01-23 16:38:38

 

1.先将jdk配置一下

选Preferences----

找到自己的jdk所在的位置

2.配置Tomcat

window-----preferences-------

找到自己的tomcat所在位置

 3.项目右键,build path--------->configure  build path

 

4.导入log4j-1.2.14.jar

右键点击log4j-1.2.14.jar文件build path----->add to build path

项目根目录下创建一个log4j.log的文件,同时在src目录下导入log4j.properties文件

log4j.properties文件代码如下

#log4j.rootLogger = DEBUG, a1
log4j.rootLogger = INFO, A1
#log4j.rootLogger = ERROR, A1
#\u8fd9\u91cc\u5b9a\u4e49\u7684\u662ferror\u7ea7\u522b\u7684\uff0c\u5176\u5b9e\u7528info\u7ea7\u7684\u5c31\u53ef\u4ee5\u4e86\uff0c\u53cd\u6b63\u67095\u4e2a\u7ea7\u522b\u4f60\u81ea\u5df1\u770b\u7740\u7528\uff1b
#log4j.appender.A1 = org.apache.log4j.ConsoleAppender
 log4j.appender.A1 = org.apache.log4j.FileAppender
 
 log4j.appender.A1 = org.apache.log4j.RollingFileAppender
 log4j.appender.A1.File =log4j.log
 #\u5b9a\u4e49\u65e5\u5fd7\u7684\u5b58\u653e\u8def\u5f84\u548c\u6587\u4ef6\u540d\uff1b
 log4j.appender.A1.MaxFileSize=500KB
 #\u5f53log\u6587\u4ef6\u5927\u4e8e500K\u7684\u65f6\u5019\u5220\u9664\u91cd\u65b0\u5efa\uff1b
 log4j.appender.A1.MaxBackupIndex=10
 log4j.appender.A1.layout = org.apache.log4j.PatternLayout
#\u4f7f\u7528\u7684\u662f\u81ea\u5b9a\u4e49\u7684\u65e5\u5fd7\u683c\u5f0f\uff0c\u4e0b\u9762\u5c31\u662f\u81ea\u5b9a\u4e49\u7684\u4e00\u4e9b\u53c2\u6570\u54af\uff1b
 #log4j.appender.A1.layout.ConversionPattern = %-4r [%t] %-5p %c %x - %m%n
 log4j.appender.A1.layout.ConversionPattern = [%d] [%t] %m%n

创建一个包 org.jsoft.log

包下面创建一个类 TestLog

package org.jsoft.log;

import org.apache.log4j.*;
/**
 * @author Administrator
 * @version 2018-1-9上午9:14:11
 */
public class TestLog {
    
    public static void main(String[] args) {
        //获得纪录器
        new TestLog().getLog();
    }
    public void getLog(){
        Logger l = Logger.getLogger(TestLog.class);
        l.info("你动了我的人");
    }
}

运行,在log4j.log可以看到日志打印出来了

WARN No appenders could be found for logger (org.jsoft.log.TestLog).

出现这个错误是因为没有导入log4j.properties文件