'Log4j2 Async Logger Not Logging Anything
I am trying to implement an Async Logger along with a Spring Boot Application. My log4j2.xml
(I have it under /src/resources/
directory) looks like:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ALL">
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
</Console>
<!-- Generate rolling log for router with per hour interval policy -->
<RollingFile name="ProcessorRollingFile" fileName="C:/logs/">
<PatternLayout>
<pattern>%d{ISO8601} [%t] %p %c %L - %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy max="24" />
</RollingFile>
<!-- Register Async appender -->
<Async name="AsyncRollingFile">
<AppenderRef ref="ProcessorRollingFile" />
</Async>
</Appenders>
<Loggers>
<AsyncLogger name="com.gtt.gcas" level="all" additivity="false" includeLocation="true">
<AppenderRef ref="AsyncRollingFile" />
</AsyncLogger>
</Loggers>
</Configuration>
I have written a generic class, that can be called from other classes to log stuff. I wish to expand on it later, right now its very basic.
package com.gtt.gcas.logging;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class GCasLogging {
private static Level LOG_LEVEL = Level.ALL;
public static Logger logger = LogManager.getLogger(GCasLogging.class);
/**
* @return the lOG_LEVEL
*/
public static Level getLogLevel() {
return LOG_LEVEL;
}
/**
* @param lOG_LEVEL the lOG_LEVEL to set
*/
public static void setLogLevel(Level logLevel) {
LOG_LEVEL = logLevel;
}
}
And I am using the above class like so:
package com.gtt.gcas.preload;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import com.gtt.gcas.employee.repository.EmployeeRepository;
import com.gtt.gcas.logging.NCaasLogging;
import com.gtt.trial.employee.pojo.Employee;
@Component
class LoadDatabase implements CommandLineRunner {
EmployeeRepository employeeRepository;
@Autowired
public LoadDatabase(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
@Bean
void initDatabase() {
Logger logger = NCaasLogging.logger;
Employee employee = new Employee("Bilbo Baggins", "burglar");
logger.info("One Employee Added.");
employeeRepository.save(employee);
employee = new Employee("Frodo Baggins", "thief");
logger.info("Second Employee Added.");
}
@Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
}
}
My dependencies section in the pom.xml
is like:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.lmax/disruptor -->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>4.0.0.RC1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.persistence/persistence-api -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<!-- This has vulnerability CVE-2022-21724 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
But the problem is, it does not create any logs. The folder C:\logs does not even get created, let alone the files under it.
Please help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|