tonglin0325的个人主页

logback,log4j和log4j2的区别

1.logback的使用,参考:logback的使用和logback.xml详解

pom文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<logback.version>1.1.7</logback.version>
<slf4j.version>1.7.21</slf4j.version>
</properties>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>

代码

1
2
3
4
5
import org.slf4j.Logger
import org.slf4j.LoggerFactory

private static final Logger log = LoggerFactory.getLogger(LoggingDemo.class);

    

2.log4j的使用

pom文件

1
2
3
4
5
6
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

代码

1
2
3
4
import org.apache.log4j.Logger;

private final Logger LOGGER = Logger.getLogger(LoggingDemo.class.getName());

  
 

3.log4j2的使用,参考:浅谈Log4j和Log4j2的区别

pom文件

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.5</version>
</dependency>

代码

1
2
3
4
5
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

private static Logger logger = LogManager.getLogger(LoggingDemo.class.getName());