log4j的使用

1.导入log4j的jar包。http://mvnrepository.com/artifact/log4j/log4j

2.添加配置文件log4j.properties:

# Define the root logger with appender file
log = C://Users/Main/Desktop
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out

# Set the append to false, not overwrite
log4j.appender.FILE.Append=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug

# Set the maximum file size before rollover
log4j.appender.FILE.MaxFileSize=5KB

# Set the the backup index
log4j.appender.FILE.MaxBackupIndex=2

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

# @see https://www.yiibai.com/log4j/

3.在类中使用:

package logtest;

import org.apache.log4j.Logger;

/**
 * Created by Main on 2018/4/19.
 */
public class Logfourj {

    static Logger logger = Logger.getLogger(Logfourj.class);

    public static void main(String[] args) {
        logger.info("hello world");
    }
}

官方文档:http://logging.apache.org/log4j/1.2/

你可能感兴趣的:(java,log4j)