Creating an spring-boot app with log4j2 logger and server is wildfly10/jboss7.1. I added "system property": "loglevel" from Configuration: System Properties and able to access this from spring boot application. I called this system property in log4j2.xml to set the log-level. log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="error" monitorInterval="30">
<Properties>
<Property name="basePath">D://propertieslog</Property>
</Properties>
<Appenders>
<RollingFile name="fileLogger" fileName="${basePath}/acweb.log" filePattern="${basePath}/acweb-%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>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
<Jdbc name="databaseAppender" tableName="APPLICATION_LOG">
<Filters>
First deny warn, error and fatal messages
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL" />
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL" />
<ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL" />
<ThresholdFilter level="DEBUG" onMatch="DENY" onMismatch="NEUTRAL" />
<ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL" />
Then accept info, warn, error, fatal and deny debug/trace
<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" />
</Filters>
<ConnectionFactory class="path" method="getDatabaseConnection" />
<Column name="LOGLEVEL" pattern="%level" />
<Column name="LOGGER" pattern="%logger" />
<Column name="MESSAGE" pattern="%message" />
</Jdbc>
</Appenders>
<loggers>
<Root level="${sys:loglevel:-ERROR}">
<appender-ref ref="console" />
<appender-ref ref="fileLogger" />
<appender-ref ref="databaseAppender" />
</Root>
</loggers>
</configuration>
Controller Class:
@GetMapping("/dashboard")
public ModelAndView welcomeMethod(HttpServletRequest request, HttpServletResponse response,
@ModelAttribute("token") final AccessToken accessToken) {
if(null==accessToken.getJwtToken()) {
logger.trace("A TRACE Message");
logger.debug("A DEBUG Message");
logger.info("An INFO Message");
logger.warn("An WARN Message");
logger.warn("Printing system property1:"+ System.getProperty("loglevel"));
logger.error("An ERROR Message");
logger.error("Printing system property1:"+ System.getProperty("loglevel"));
return new ModelAndView("redirect:launchApp");
}
Issue is while changing the system property from wildfly console, it is reflecting in java class logs, but it is not reflecting in log4j2.xml. log level in log4j2.xml remains as the one which was set during application build.
set the loglevel system property set as "WARN" and build the project and deployed it. Logs are below:
[WARN ] 2018-09-26 15:13:05.787 [default task-7] WelcomeController - An WARN Message
[WARN ] 2018-09-26 15:13:05.792 [default task-7] WelcomeController - Printing system property1:WARN
[ERROR] 2018-09-26 15:13:05.793 [default task-7] WelcomeController - An ERROR Message
[ERROR] 2018-09-26 15:13:05.793 [default task-7] WelcomeController - Printing system property1:WARN
After on same deployment changed the loglevel system property to "INFO", but the logs are not changing to INFO level:
[WARN ] 2018-09-26 15:15:34.933 [default task-11] WelcomeController - An WARN Message
[WARN ] 2018-09-26 15:15:34.933 [default task-11] WelcomeController - Printing system property1:INFO
[ERROR] 2018-09-26 15:15:34.935 [default task-11] WelcomeController - An ERROR Message
[ERROR] 2018-09-26 15:15:34.935 [default task-11] WelcomeController - Printing system property1:INFO