如何将其他属性引用到log4j2 yaml配置中?

问题描述 投票:0回答:1

我正在使用以下 log4j2 yaml 配置:

Configuration:
 Appenders:
    Console:
      name: Console
      target: SYSTEM_OUT
      JSONLayout:
        compact: true
        eventEol: true
        properties: true
        stacktraceAsString: true
        includeTimeMillis: true
        KeyValuePair:
          - key: appName
            value: ${spring.application.name} 
          - key: version
            value: ${spring.application.version} 
          
 Loggers:
    Root:
      level: INFO
      AppenderRef:
        ref: Console
    Logger:
      - name: org.apache
        level: WARN
        AppenderRef:
          ref: Console
      - name: org.springframework.beans.factory
        level: WARN
        AppenderRef:
          ref: Console

但在日志上,

{"instant":{"epochSecond":1705250772,"nanoOfSecond":209226000},"thread":"main","level":"INFO","loggerName":"com.hyperbank.architecture.base.AbstractHyperBankApplication","message":"Hello, it's me, 'config-provider' !!!","endOfBatch":false,"loggerFqcn":"org.apache.logging.slf4j.Log4jLogger","contextMap":{},"threadId":1,"threadPriority":5,"appName":"${spring.application.name}","version":"${spring.application.version}"}

属性 appName 和 version 不会被实际值替换

banner.txt 上有相同的引用,并且显示正确。

  _  _                    ___            _      ___           __ _        ___             _    _         
 | || |_  _ _ __  ___ _ _| _ ) __ _ _ _ | |__  / __|___ _ _  / _(_)__ _  | _ \_ _ _____ _(_)__| |___ _ _ 
 | __ | || | '_ \/ -_) '_| _ \/ _` | ' \| / / | (__/ _ \ ' \|  _| / _` | |  _/ '_/ _ \ V / / _` / -_) '_|
 |_||_|\_, | .__/\___|_| |___/\__,_|_||_|_\_\  \___\___/_||_|_| |_\__, | |_| |_| \___/\_/|_\__,_\___|_|  
       |__/|_|                                                    |___/                                  

Name: config-provider 
Version: 1.0.0
Powered by Spring Boot 3.1.2
java spring-boot yaml log4j2
1个回答
0
投票

你应该使用 Spring Boot Lookup :

Configuration:
 Appenders:
    Console:
      name: Console
      target: SYSTEM_OUT
      JSONLayout:
        compact: true
        eventEol: true
        properties: true
        stacktraceAsString: true
        includeTimeMillis: true
        KeyValuePair:
          - key: appName
            value: ${spring:spring.application.name} 
          - key: version
            value: ${spring:spring.application.version} 
© www.soinside.com 2019 - 2024. All rights reserved.