I'm using Log4j2 with yaml configuration. Logging to console works well, an external log file is created every time but it has no entries. Thanks for some hints.
Main method at 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();
}
}
log4j2.properties at src/main/java - if I delete / rename it, log4j shows a message that no config was found, so it uses this file:
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
My 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>
If I run it I got this output in my console:
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
The created but empty file is logs/example.log
you can try changing 2 lines at bottom of your log4j2.properties. Hope it helps.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With