I have an issue in getting to export the log file of log4j2 if I am running it via the command line.
I have an executable jar file that does not contain any other .properties files, other than application.properties (this file is empty). In my case, I am running my command line as follows:
java -Dlog4j2.configurationFile=//file:/C:/myApp/CONF/log4j2.properties -jar myApp-0.0.1-SNAPSHOT.jar
However, in my terminal, no errors are shown, and during the execution, no log file is generated.
Instead, the log file only seems to be exported and generated only if I commented out SpringApplication.run(App.class, args); in my main application.
Even so, I believe I need this line to run the spring framework/ architecture side of things (I'm new to spring stuff).
I have tried placing the same log4j2.properties within the jar file and it does works (ie. generated the log file however this is not what I intended for)
A sample of my program is as follows:
Main File - App.java
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication (exclude (DataSourceAutoConfiguration.class })
public class App implements CommandLineRunner {
private static final Logger logger = LogManager.getLogger(App.class);
public static void main(String[] args) {
// File is generated if only the following line is commented out
// SpringApplication.run(App.class, args);
logger.debug("This is debug");
logger.info("This is info");
logger.error("This is error");
}
@Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
}
}
Pom.xml - To enable the export of log4j2 log file
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId> <version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId>
<exclusions>
<!-- Excluding logback dependencies to use 14j2 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
log4j2.properties
status = error
property.filename = C:\\mytest-logs\\debug.log
filters = threshold
filter.threshold.type = ThresholdFilter filter.threshold.level=debug
appenders = rolling
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = C:\\logs\\Previous\\debug-backup-%d{MM-dd-yy-HH-mm-ss}-xi.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c(1):%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
loggers = rolling
logger.rolling.name = mytesting.test_log4js
logger.rolling.level = debug
logger.rolling.additivity = true
logger.rolling.appenderRef.rolling.ref= RollingFile
I am using Java version 1.8 and spring boot version 2.2.13.
Appreciate in advance for any insights.