1. 程式人生 > >java 日誌 log4j

java 日誌 log4j

span %x figure roo oid fig pac led 下載

1.下載jar:log4j-1.2.15.jar

2.配置文件:log4j.properties

# Priorities are: DEBUG, INFO, WARN, ERROR, or FATAL.
log4j.rootCategory=INFO, CONSOLE, FILE
logs.dir=${catalina.base}/logs/springdemo/
logs.filename=logs
fileBufferSize=20480

#log to File
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE
.File=D:/${number}/10.logs log4j.appender.FILE.encoding=UTF-8 log4j.appender.FILE.Append=true log4j.appender.FILE.LocationInfo=true log4j.appender.FILE.DatePattern=‘.‘yyyy-MM-dd‘.log‘ log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.ConversionPattern=[%d]: %m %x %n log4j.appender.FILE
.bufferSize=${fileBufferSize} # CONSOLE is set to be a ConsoleAppender using a PatternLayout. log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Threshold=DEBUG log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n

3.java類:

package com.log.test;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class DemoTest {

    static Logger logger = Logger.getLogger(DemoTest.class.getName());

    public static void main(String[] args) throws Exception {
        // 格式化時間 年月日
        SimpleDateFormat data = new SimpleDateFormat("YYYYMMdd");
        String format = data.format(new Date());
        // 動態配置路徑
        System.setProperty("number", format);//以日期建文件夾
        // 加載配置文件
        PropertyConfigurator.configure("config/log4j.properties");//配置文件的路徑
        // 輸出日誌信息
        logger.debug("HellosdsdWoasdassdasdasdfdsfrld");
    }
}

java 日誌 log4j