Skip to content

Log4j2 configuration is not picking up some properties #22983

@remal

Description

@remal

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:

<Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd HH:mm:ss.SSS</Property>
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>

At the same time, Logback configuration is a bit different:

<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

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?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions