欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Log4j 引入项目

程序员文章站 2022-04-26 08:38:59
...
方式一:导入log4j的jar包

方式二:新建一个maven项目,通过maven导入log4j

New Maven project
  create a simple project(skip archetype selection)
Artifact
  Group Id: com.open1111
  Artifact Id: Log4jTest
  Version: 0.0.1-SNAPSHOT
  Packaging: jar


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.open1111</groupId>
  <artifactId>Log4jTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
  </dependencies>
</project>


log4j.rootLogger=DEBUG, Console ,File

#Console  
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n

#File
log4j.appender.File = org.apache.log4j.FileAppender
log4j.appender.File.File = E://log4j-test-log.log
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n


package com.open1111;
import org.apache.log4j.Logger;
public class Test {
    private static Logger logger = Logger.getLogger(Test.class);
    public static void main(String[] args) {
        logger.fatal("严重错误fatal信息");
        logger.error("报错error信息");
        logger.error("报错信息", new IllegalArgumentException("非法参数"));
        logger.warn("警告warn信息");
        logger.info("普通Info信息");
        logger.debug("调试debug信息");
    }
}
运行结果:
控制台打印信息
生产log文件:E://log4j-test-log.log
相关标签: log4j