I have used log4j2.properties file with springboot application. Log file was creating but logs are not written into the file.
Please find the details as below:
name=PropertiesConfig
property.filename = C:/Logs
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=${filename}/app.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.java.app //Parent Package name for the application
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
<!-- Logging -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
package com.java.app;
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
private final static Logger log = LogManager.getLogger(DemoApplication.class);
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
public static void main(String[] args) {
log.info("Logger enabled: Entering main \\n\\n");
SpringApplication.run(DemoApplication.class, args);
log.info("**** Demo Application Started *****");
}
}
Logs are appearing in the console but not written into file as i am not getting the issue.
It's strange, parent package logger "Logger enabled: Entering main \n\n" is written into the file and the other parent logger "**** Demo Application Started *****" is not written into the file as the code is shown above. and also checked for the sub package i.e com.java.app.endpoint
loggers even those also not written into the file.
and also identified that the console log is coming like as
2018-08-03 12:55:18.302 INFO 11440 --- [nio-8088-exec-1] c.j.c.e.Classname : logger message
If c.j.c.e. coming as prefix to the class name in logs those are not written into the file why?
I might be doing something wrong. Can anyone please help on this.
I also face with this problem following the tutorial on springframework.guru. After searching on spring boot docs then I config my pom.xml with those dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
and add
logging.config=src/main/resources/log4j2.properties
in the application.properties file. After that I can see the log appear on my log file when I run the application.
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