Introduction
官网:http://logging.apache.org/log4j/2.x/
Installation
Install
See http://logging.apache.org/log4j/2.x/maven-artifacts.html
Maven pom.xml
pom.xml1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.11.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.11.2</version> </dependency>
|
Install Log4j with Slf4j
https://www.slf4j.org/manual.html
pom.xml
1 2 3 4 5
| <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.8.0-beta4</version> </dependency>
|
这会同时包含slf4j-api和log4j-1依赖
Configuration
http://logging.apache.org/log4j/2.x/manual/configuration.html
Configuration of Log4j 2 can be accomplished in 1 of 4 ways:
- Through a configuration file written in XML, JSON, or YAML.
- Programmatically, by creating a ConfigurationFactory and Configuration implementation.
- Programmatically, by calling the APIs exposed in the Configuration interface to add components to the default configuration.
- Programmatically, by calling methods on the internal Logger class.
Usage
日志到标准输出
log4j.properties
1 2 3 4 5 6 7
| log4j.rootLogger=info, STDOUT
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout log4j.appender.STDOUT.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
|
日志到文件
log4j.properties
1 2 3 4 5 6
| log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender log4j.appender.LOGFILE.MaxFileSize=100MB log4j.appender.LOGFILE.MaxBackupIndex=10 log4j.appender.LOGFILE.File=/tmp/xxx.log log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout log4j.appender.LOGFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %p [metric-detection] [%t] %c %x - %m%n
|
指定某些包/类的日志
log4j.properties
1 2 3 4 5
| log4j.logger.org.test=debug,console
log4j.additivity.org.test=false
|