当前位置: 首页 > 工具软件 > Log4g > 使用案例 >

log4g配置(一)

戚令秋
2023-12-01

一、log4g简单配置控制台配置


以下是对log4j.properties文件配置====

#ConsoleAppender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}%m%n
#FileAppender
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=file.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}%m%n
#RollingFileAppender
log4j.appender.rollfile=org.apache.log4j.RollingFileAppender
log4j.appender.rollfile.File=rollfile.log
log4j.appender.rollfile.MaxBackupIndex=3
log4j.appender.rollfile.MaxFileSize=2KB
log4j.appender.rollfile.layout=org.apache.log4j.PatternLayout
log4j.appender.rollfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}%m%n
#DailyRollingFileAppender
log4j.appender.dailyfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyfile.File=dailyfile.log
log4j.appender.dailyfile.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}%m%n
log4j.appender.dailyfile.DatePattern=’.'yyyy-MM-dd-HH-mm’.log’
#JDBCAppender(DATA)
#log4j.appender.db=org.apache.log4j.jdbc.JDBCAppender
#log4j.appender.db.URL=jdbc:jtds:sqlserver://localhost:1433;databaseName=logdb
#log4j.appender.db.Driver=net.sourceforge.jtds.jdbc.Driver
#log4j.appender.db.User=sa
#log4j.appender.db.Password=123
#log4j.appender.db.Sql=insert into mylog values(‘%d{yyyy-MM-dd HH:mm:ss}’,'%m’)
#log4j.appender.db.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}%m%n
#log4j.appender.db.layout=org.apache.log4j.PatternLayout
log4j.rootLogger=debug,stdout,file,rollfile,dailyfile,db
#JDBCAppender(DATA)
#log4j.appender.db=org.apache.log4j.jdbc.JDBCAppender
#log4j.appender.db.URL=jdbc:jtds:sqlserver://localhost:1433;databaseName=logdb
#log4j.appender.db.Driver=net.sourceforge.jtds.jdbc.Driver
#log4j.appender.db.User=root
#log4j.appender.db.Password=123456
#log4j.appender.db.Sql=insert into mylog values(‘%d{yyyy-MM-dd HH:mm:ss}’,'%m’)
#log4j.appender.db.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}%m%n
#log4j.appender.db.layout=org.apache.log4j.PatternLayout
log4j.properties


以下是Test.java文件========

package com.sec.log4j;
import org.apache.log4j.Logger;
public class Test{

    private static final Logger logger = Logger.getLogger(Test.class);
    public static void main(String[] args){
        for(int i=0;i<1000;i++){
            logger.debug("1111");
        }
    }
}

以下是TestLog4j.java文件======

package com.sec.log4j;
import org.apache.log4j.Logger;
public class TestLog4j{
    private static Logger logger=Logger.getRootLogger();
    public static void main(String[] args){
        logger.debug("debug11");
        logger.info("info11");
        logger.warn("warn11");
        logger.error("error111");
    }
}
 类似资料: