Log printout in eclipse environment

1. First configure the jdk

Choose Preferences----

Find the location of your own jdk

2. Configure Tomcat

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

Find your own tomcat location

3. Right click the project, build path -------- > configure build path

 

4. Import log4j-1.2.14.jar

Right click log4j-1.2.14.jar file build path ----- > add to build path

Create a log4j.log file in the root directory of the project, and import the log4j.properties file in the src directory

The code of log4j.properties file is as follows

#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

Create a package org.jsoft.log

Create a TestLog class under the package

package org.jsoft.log;

import org.apache.log4j.*;
/**
 * @author Administrator
 * @version 2018-1-9 9:14:11 am
 */
public class TestLog {
    
    public static void main(String[] args) {
        //Acquisition recorder
        new TestLog().getLog();
    }
    public void getLog(){
        Logger l = Logger.getLogger(TestLog.class);
        l.info("You moved my people");
    }
}

Run. You can see that the log is printed out in log4j.log

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

This error occurred because the log4j.properties file was not imported

Keywords: Java log4j Apache JDK Tomcat

Added by hadeosdin on Sat, 02 May 2020 12:22:02 +0300