我正在使用带有yaml配置的Log4j2。记录到控制台运行良好,每次都会创建一个外部日志文件,但它没有条目。谢谢你的一些提示。
src / main / java / my / example / log /主要方法:
public class App {
private static Logger LOG = LogManager.getLogger(App.class);
public static void main(String[] args) {
LOG.entry();
LOG.info("Start of application");
System.out.println("Hello World!");
LOG.info("End of application");
LOG.exit();
}
}
src / main / java 中的log4j2.properties - 如果我删除/重命名它,log4j会显示一条消息,表明找不到配置,因此它使用此文件:
name = PropertiesConfig
property.filename = logs/example.log
appenders = console, rolling
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{dd.MM.yyyy HH:mm:ss,SSS} %-5p %c{1}:%L - %m%n
appender.rolling.type = RollingFile
appender.rolling.name = roll
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = logs/app/%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{dd.MM.yyyy HH:mm:ss,SSS} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 2
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=100MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5
loggers = rolling
logger.rolling.name = org.apache.logging.log4j.core.appender.rolling
logger.rolling.level = all
logger.rolling.additivity = true
logger.rolling.appenderRefs = rolling
logger.rolling.appenderRef.rolling.ref = roll
rootLogger.level = all
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
我的 Pom.xml :
<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>App</groupId>
<artifactId>ExampleApp</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>ExampleApp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</project>
如果我运行它,我在控制台中输出了这个输出:
21.05.2016 14:17:52,397 TRACE App:13 - entry
21.05.2016 14:17:52,399 INFO App:14 - Start of application
Hello World!
21.05.2016 14:17:52,399 INFO App:16 - End of application
21.05.2016 14:17:52,399 TRACE App:17 - exit
已创建但空的文件为logs/example.log
答案 0 :(得分:3)
您可以尝试更改log4j2.properties底部的2行。 希望它有所帮助。
name = PropertiesConfig
property.filename = logs/example.log
appenders = console, rolling
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{dd.MM.yyyy HH:mm:ss,SSS} %-5p %c{1}:%L - %m%n
appender.rolling.type = RollingFile
appender.rolling.name = roll
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = logs/app/%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{dd.MM.yyyy HH:mm:ss,SSS} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 2
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=100MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5
loggers = rolling
logger.rolling.name = org.apache.logging.log4j.core.appender.rolling
logger.rolling.level = all
logger.rolling.additivity = true
logger.rolling.appenderRefs = rolling
logger.rolling.appenderRef.rolling.ref = roll
rootLogger.level = all
rootLogger.appenderRefs = rolling, stdout
rootLogger.appenderRef.rolling.ref = roll
rootLogger.appenderRef.stdout.ref = STDOUT
答案 1 :(得分:2)
该文件当然是空的。您为新记录器指定了名称org.apache.logging.log4j.core.appender.rolling
。您请求的记录器是App
(或类似的东西)。它不会登录到您的新org.apache.logging.log4j.core.appender.rolling
,但根记录器和根记录器配置为仅记录到STDOUT。这就是发生的一切。
要解决此问题,请将新记录器的名称更改为其他名称(很可能是您的包名称)。不要只是应对用户指南中的所有内容。