I'm using log4j2 for logging and tried to simplify the logging pattern by setting logging.pattern.dateformat to HH:mm:ss.SSS.

build.gradle:

tasks.withType(Test) {
    systemProperty('logging.pattern.dateformat', 'HH:mm:ss.SSS')
}

However, the date format remains unchanged - yyyy-MM-dd HH:mm:ss.SSS.

I tried to debug the application and found out that log4j2.xml file has these lines: https://github.com/spring-projects/spring-boot/blob/0fce2806c4a298a2c5abb01db0e8b869702b9a0f/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml#L7-L8

At the same time, Logback configuration is a bit different: https://github.com/spring-projects/spring-boot/blob/0fce2806c4a298a2c5abb01db0e8b869702b9a0f/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml#L12

When I put my custom log4j.xml file on the classpath, it started working fine:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Properties>
    <Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
    <Property name="LOG_LEVEL_PATTERN">%5p</Property>
    <Property name="CONSOLE_LOG_PATTERN">%clr{%d{${env:LOG_DATEFORMAT_PATTERN:-${sys:LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{%pid}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
  </Properties>
  <!-- ... ->
</Configuration>

Shouldn't CONSOLE_LOG_PATTERN property be defined in a way like this?

I suppose, the same is about LOG_EXCEPTION_CONVERSION_WORD and LOG_LEVEL_PATTERN properties. WDYT?

Comment From: philwebb

There's a class called LoggingSystemProperties that should call System.setProperty to set LOG_DATEFORMAT_PATTERN. It looks like we might be missing the sys: prefix for a few of our variable references.

Comment From: remal

@philwebb exactly!

Seems log4j2.xml should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Properties>
    <Property name="CONSOLE_LOG_PATTERN">%clr{%d{${sys:LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}}{faint} %clr{${sys:LOG_LEVEL_PATTERN:-%5p}} %clr{%pid}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD:-%xwEx}</Property>
    <Property name="FILE_LOG_PATTERN">%d{${sys:LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${sys:LOG_LEVEL_PATTERN:-%5p} %pid --- [%t] %-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD:-%xwEx}</Property>
  </Properties>
  <!-- ... ->
</Configuration>

As you can see, I removed declarations of LOG_EXCEPTION_CONVERSION_WORD, LOG_LEVEL_PATTERN and LOG_DATEFORMAT_PATTERN and put them directly in *_PATTERN properties with default values.