我在属性文件中配置了log4j2。记录器将输出发送到控制台,但不发送到文件。它会创建日志文件,但不会使用内容更新它。我的配置文件中可能缺少什么?
log4j2.properties
status = error
name = PropertiesConfig
property.filename = /usr/local/logs/kit.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
logger.rolling.name = org.kit
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
测试课
package org.kit.log;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class LogTest {
private static final Logger logger = LogManager.getLogger(LogTest.class);
public static void main(String[] args) {
logger.debug("Hello world - debug log");
logger.info("Hello world - info log");
logger.warn("Hello world - warn log");
logger.error("Hello world - error log");
}
}
答案 0 :(得分:0)
您可以尝试将配置中的第一行设置为status = TRACE
,以在控制台上显示Log4j2内部日志记录。这可能有助于确定配置的任何问题。
另外,您可以尝试配置rootLogger而不是命名的记录器吗?
rootLogger.level = info
rootLogger.appenderRef.rolling.ref = RollingFile