Reputation: 193
I have implemented Logging in Spring Boot application by using log4j2.properties
file.
Please find the code as in below reference url:
Spring Boot - log4j2.properties creating log files but not writing the logs in file
The console logging is working and also file creating and one of the log is writing into the file but others are not writing into the file.
Please find the differences of loggers as printing in console as below:
[INFO ] 2018-08-06 11:48:05.609 [restartedMain] DemoApplication - Logger enabled: Entering main //this log is writing into file.
2018-08-06 11:48:14.211 INFO 10788 --- [ restartedMain] c.j.c.DemoApplication : **** Demo Application Started *****// this is not writing into file.
but for both using log.info to print the loggers.
Can anyone please help on this.
Upvotes: 1
Views: 1287
Reputation: 206
You have to carefully check your log4j2.properties file. I have tested with your example and following configuration works just fine:
name=PropertiesConfig
appenders = console, file
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=demo.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
loggers=file
logger.file.name=com.testsexample.test5
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
Main class is the following (package name should correspond logger.file.name property in log4j2.properties):
package com.testsexample.test5;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Test5Application {
private final static Logger LOG = LogManager.getLogger(Test5Application.class);
public static void main(String[] args) {
LOG.info("Logger enabled: Entering main \n\n");
SpringApplication.run(Test5Application.class, args);
LOG.info("**** Demo Application Started *****");
}
}
Project structure is:
Strange, but I have reproduced your issue after several relaunches. Please try to replace log4j2.properties with following log4j2.xml - the problem was gone with xml configuration
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Properties>
<Property name="log-path">logs</Property>
<Property name="LOG_PATTERN">
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
<File name="FileAppender" fileName="demo.log">
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="FileAppender"/>
<AppenderRef ref="ConsoleAppender"/>
</Root>
</Loggers>
</Configuration>
For daily rolling appender it will be:
<RollingFile name="FileAppender" fileName="demo.log" filePattern="demo %d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy></DefaultRolloverStrategy>
</RollingFile>
Upvotes: 1