My LOG4J2 logs are visible in development, but no longer when the application is installed via MSI in production. The Hibernate logs are recorded in both cases, and in the correct file (the path is specified in the configuration file).
Here is my log4j2.xml configuration file:
<Configuration status="INFO" name="2024Log4j2Config">
<Properties>
<Property name="LOG_DIR">${sys:logs}</Property>
</Properties>
<Appenders>
<RollingFile
name="RollingFile"
fileName="${LOG_DIR}/app.log"
filePattern="${LOG_DIR}/app.%d{yyyy-MM-dd--HH-mm}--%i.log.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="5 MB"/>
<TimeBasedTriggeringPolicy/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${LOG_DIR}">
<IfFileName glob="app.*.log.gz"/>
<IfLastModified age="P30D"/>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="RollingFile"/>
</Root>
<Logger name="org.hibernate" level="DEBUG" additivity="false">
<Appender-ref ref="RollingFile" level="DEBUG"/>
</Logger>
<Logger level="INFO" name="fr.bla.fx2024" additivity="false">
<Appender-ref ref="RollingFile" level="INFO"/>
</Logger>
</Loggers>
</Configuration>
My Java Log class code :
/**
* Set the default logs' path inside a logs system property
*/
private void setCurrentDefaultLogsPathInALog4j2SystemProperty() {
System.setProperty("logs", logsFolderPath);
}
/**
* Set the current Log4j2 configuration file's path
*/
private void setCurrentLog4j2ConfigurationFilePath() {
String log4j2ConfigurationPath = Objects.requireNonNull(this.getClass().getResource(ResourcesPaths.getCONFIG_LOG4J2_PATH())).toExternalForm();
Configurator.initialize("2024Log4j2Config", log4j2ConfigurationPath);
}
I noticed that the getCONFIG_LOG4J2_PATH() path is different in development and in production, but I don't understand how Hibernate finds it and writes its data to it, and especially how to resolve this point:
Dev : file:/C:/Users/MyUser/.../log4j2.xml
Prod : jar:file:/C:/Program%20Files/MyOrganisation/app/MyApp.jar!/fr/bla/fx2024/config/log4j2.xml
@jewelsea
I have the same thing when launching the fat jar generated with maven-shade, so that's not a jpackage issue.
I moved to Logback ans I used the ClassLoader.getResourceAsStream() method to load the configuration file as a stream. This method is designed to load resources from the classpath, including resources inside jar files.
try (InputStream inputStream = Log.class.getResourceAsStream(ResourcesPaths.getCONFIG_LOGBACK_PATH())) {
...