Skip to content

Log4j2 configuration is not picking up some properties #22983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
remal opened this issue Aug 17, 2020 · 2 comments
Closed

Log4j2 configuration is not picking up some properties #22983

remal opened this issue Aug 17, 2020 · 2 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@remal
Copy link

remal commented Aug 17, 2020

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?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 17, 2020
@philwebb
Copy link
Member

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.

@philwebb philwebb added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 17, 2020
@philwebb philwebb added this to the 2.2.x milestone Aug 17, 2020
@remal
Copy link
Author

remal commented Aug 17, 2020

@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.

@philwebb philwebb changed the title Setting logging.pattern.dateformat property doesn't change date format for log4j2 Log4j2 configuration is not picking up some proeprties Aug 17, 2020
@scottfrederick scottfrederick changed the title Log4j2 configuration is not picking up some proeprties Log4j2 configuration is not picking up some properties Aug 17, 2020
@wilkinsona wilkinsona self-assigned this Sep 21, 2020
wilkinsona added a commit that referenced this issue Sep 21, 2020
@wilkinsona wilkinsona modified the milestones: 2.2.x, 2.2.11 Sep 22, 2020
wilkinsona added a commit to wilkinsona/spring-boot that referenced this issue Sep 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants