加载中...

Log4j常用配置及使用


1 添加log4j依赖

maven的pom.xml文件中添加log4j依赖

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>provided</scope>
        </dependency>

2 使用代码配置并调用log

2.1 代码配置LogConfiguration.java

import java.util.Properties;
import org.apache.log4j.PropertyConfigurator;

public class LogConfiguration {    
    public static void initLog(String logFilePath){
        //声明日志文件存储路径以及文件名、格式
        Properties prop = new Properties();
        //配置日志输出的格式
        prop.setProperty("log4j.rootLogger","info, toConsole, toFile");
        prop.setProperty("log4j.appender.file.encoding","UTF-8" );
        
        prop.setProperty("log4j.appender.toConsole","org.apache.log4j.ConsoleAppender");
        prop.setProperty("log4j.appender.toConsole.Target","System.out");
        prop.setProperty("log4j.appender.toConsole.layout","org.apache.log4j.PatternLayout ");
        prop.setProperty("log4j.appender.toConsole.layout.ConversionPattern","[%d{yyyy-MM-dd HH:mm:ss}] [%p] %m%n");    
        
        prop.setProperty("log4j.appender.toFile.file", logFilePath);
        //每天产生一个日志文件
        prop.setProperty("log4j.appender.toFile", "org.apache.log4j.DailyRollingFileAppender");        
        //服务器启动日志是追加,false:服务器启动后会生成日志文件把老的覆盖掉
        prop.setProperty("log4j.appender.toFile.Append","true");
        prop.setProperty("log4j.appender.toFile.Threshold", "info");
        prop.setProperty("log4j.appender.toFile.layout", "org.apache.log4j.PatternLayout");
        prop.setProperty("log4j.appender.toFile.layout.ConversionPattern", "[%d{yyyy-MM-dd HH:mm:ss}] [%p] %m%n");    
        //设置每天生成一个文件名后添加的名称,备份名称:年月日.log
        prop.setProperty("log4j.appender.toFile.DatePattern","'.'yyyy-MM-dd'.log'");

        //使配置生效
        PropertyConfigurator.configure(prop);
    }
}

2.2 调用

import org.apache.log4j.Logger;

public class TestLog {
    static Logger logger = Logger.getLogger(TestLog.class.getName());
    
    public static void main(String[] args)  {        
        LogConfiguration.initLog("D:\\log\\result.log");
        logger.info("log info");
        logger.warn("log warn");
        logger.error("log error");
        
        try {
        Thread.sleep(2000);
        }
        catch(Exception ex)
        {}
        
        logger.info("log1 info");
        logger.warn("log1 warn");
        logger.error("log1 error");        
    }
}

2.3 结果

输出到Ecliplse的Console界面和文件D:\log\result.log中,内容如下:

[2016-12-14 16:03:34] [INFO] log info
[2016-12-14 16:03:34] [WARN] log warn
[2016-12-14 16:03:34] [ERROR] log error
[2016-12-14 16:03:36] [INFO] log1 info
[2016-12-14 16:03:36] [WARN] log1 warn
[2016-12-14 16:03:36] [ERROR] log1 error

另外,由于配置了每天生成一个备份,系统时间切一天后日后,再运行,原先的result.log会变成result.log.2016-12-13.log,新的日志会重新放到新建的日志文件result.log

3 使用Properties文件配置并调用log

3.1 文件配置log4j.properties

log4j.rootLogger=INFO, toConsole, toFile 
log4j.appender.file.encoding=UTF-8

log4j.appender.toConsole=org.apache.log4j.ConsoleAppender 
log4j.appender.toConsole.Target=System.out  
log4j.appender.toConsole.layout=org.apache.log4j.PatternLayout 
log4j.appender.toConsole.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%p] %m%n 
  
log4j.appender.toFile.file=D:\\log\\result.log 
#Daily one log file
log4j.appender.toFile=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.toFile.Append=true  
log4j.appender.toFile.Threshold=info 
log4j.appender.toFile.layout=org.apache.log4j.PatternLayout
log4j.appender.toFile.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%p] %m%n
log4j.appender.toFile.DatePattern='.'yyyy-MM-dd'.log'

3.2 调用

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

public class TestLog {
    static Logger logger = Logger.getLogger(TestLog.class.getName());
    
    public static void main(String[] args)  {        
        PropertyConfigurator.configure( "D:\\Users\\...\\config\\log4j.properties" );
        logger.info("log info");
        logger.warn("log warn");
        logger.error("log error");
        
        try {
        Thread.sleep(2000);
        }
        catch(Exception ex)
        {}
        
        logger.info("log1 info");
        logger.warn("log1 warn");
        logger.error("log1 error");            
    }
}

3.3 结果

和上面一致

4 按输出级别的种类保存内容到不同目的地

从高到低:ERROR、WARN、INFO、DEBUG

  • ERROR 为严重错误 主要是程序的错误
  • WARN 为一般警告,比如session丢失
  • INFO 为一般要显示的信息,比如登录登出
  • DEBUG 为程序的调试信息

4.1 文件配置log4j.properties

log4j.rootLogger=INFO, toConsole 
log4j.appender.file.encoding=UTF-8

log4j.appender.toConsole=org.apache.log4j.ConsoleAppender 
log4j.appender.toConsole.Target=System.out  
log4j.appender.toConsole.layout=org.apache.log4j.PatternLayout 
log4j.appender.toConsole.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%p] %m%n 
  
  
log4j.logger.myError=ERROR, toFile   
log4j.appender.toFile.file=D:\\log\\Errors.log 
#Daily one log file
log4j.appender.toFile=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.toFile.Append=true  
log4j.appender.toFile.Threshold=info 
log4j.appender.toFile.layout=org.apache.log4j.PatternLayout
log4j.appender.toFile.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%p] %m%n
log4j.appender.toFile.DatePattern='.'yyyy-MM-dd'.log'

4.2 调用

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

public class TestLog {
    static Logger logger = Logger.getLogger(TestLog.class.getName());
    static Logger errorLogger=Logger.getLogger("myError");
    
    public static void main(String[] args)  {        
        PropertyConfigurator.configure( "D:\\Users\\...\\config\\log4j.properties" );
        logger.info("log info");
        logger.warn("log warn");
        logger.error("log error");

        errorLogger.info("myError info");
        errorLogger.warn("myError warn");
        errorLogger.error("myError error");
    }
}

4.3 结果

Eclipse的Console界面

[2016-12-13 17:45:06] [INFO] log info
 [2016-12-13 17:45:06] [WARN] log warn
 [2016-12-13 17:45:06] [ERROR] log error
 [2016-12-13 17:45:06] [ERROR] myError error

Errors.log

[2016-12-13 17:45:06] [ERROR] myError error


还没有评论.