Log4j2 properties配置文件demo
教程分享
>
Java教程
(4292)
2023-03-28 11:29:14
概述
学习配置log4j2.properties文件以将日志语句输出到控制台,滚动文件等。学习配置
log4j2 appenders, levels 和patterns.
Apache Log4j2是对Log4j 1.x的升级,在性能上进行了重大改进,改进了配置文件的自动重装,Java 8 lambda支持和自定义日志级别,从而对Log4j 1.x进行了重大改进。
1. Log4j2 maven 依赖
要使用Log4j2,请在项目中包括以下maven依赖项。
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.1</version>
</dependency>
2. log4j2.properties – 控制台日志输出
我们可以使用下面的log4j2.properties文件将输出记录到控制台中。 请注意,如果找不到配置文件,则将使用DefaultConfiguration。 Log4j2默认日志记录还会使日志记录输出进入控制台。
$title(log4j2.properties)
status = error
name = PropertiesConfig
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appenders = console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
3. log4j2.properties – 滚动文件输出
我们可以使用下面的log4j2.properties文件将输出记录到基于日期的滚动文件中。
$title(log4j2.properties)
status = error
name = PropertiesConfig
#Make sure to change log file path as per your need
property.filename = C:\\logs\\debug.log
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appenders = rolling
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
loggers = rolling
#Make sure to change the package structure as per your application
logger.rolling.name = com.howtodoinjava
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
4. log4j2.properties 文件位置
5. log4j2 properties 配置使用例子
接下来,让我们编写一个Java类并编写一些日志语句,以验证日志是否也出现在控制台和日志文件中。
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Log4j2HelloWorldExample
{
private static final Logger LOGGER = LogManager.getLogger(Log4j2HelloWorldExample.class.getName());
public static void main(String[] args)
{
LOGGER.debug("Debug Message Logged !!!");
LOGGER.info("Info Message Logged !!!");
LOGGER.error("Error Message Logged !!!", new NullPointerException("NullError"));
}
}
现在,当您运行上述程序时,您将在控制台中获得以下日志。
2016-06-16 13:41:27 DEBUG Log4j2HelloWorldExample:12 - Debug Message Logged !!!
2016-06-16 13:41:27 INFO Log4j2HelloWorldExample:13 - Info Message Logged !!!
2016-06-16 13:41:27 ERROR Log4j2HelloWorldExample:14 - Error Message Logged !!!
java.lang.NullPointerException: NullError
at com.leftso.log4j2.examples.Log4j2HelloWorldExample.main(Log4j2HelloWorldExample.java:14)
[classes/:?]
https://www.leftso.com/article/800.html