在Log4J2 YAML中使用属性[英] Using Properties in Log4J2 YAML

本文是小编为大家收集整理的关于在Log4J2 YAML中使用属性的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我试图在log4j2.yaml中使用属性.等效XML是此.

<Configuration>
<Properties>
    <Property name="log-path">logs</Property>
    <Property name="archive">${log-path}/archive</Property>
</Properties>
<Appenders>
. . .

我尝试了.

Configutation:
  name: Default
  properties:
    property:
      name: log-path
      value: "logs"
      name: archive
      value: ${log-path}/archive
  Appenders:

但是属性没有被选中.例如,以下代码创建 $ {log-path} 文件夹以存储日志文件而不是所需的 logs 文件夹.

fileName: ${log-path}/rollingfile.log

我在做什么错?

推荐答案

如果您查看log4j2.json文件,您可以看到property键必须具有(再次)键值对的值.翻译成yaml,这看起来像是该文件的开始:

configuration:
  name: Default
  properties:
    property:
    - name: log-path
      value: logs
    - name: archive
      value: ${log-path}/archive
  appenders:
    Console:
      PatternLayout:
        pattern: '[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n'
      name: Console-Appender
      target: SYSTEM_OUT
    File:
      PatternLayout:
        pattern: '[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n'
      fileName: ${log-path}/logfile.log
      name: File-Appender
    RollingFile:
      DefaultRolloverStrategy:
        max: '30'
      PatternLayout:
        pattern: '[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n'
      Policies:
        SizeBasedTriggeringPolicy:
          size: 1 KB
      fileName: ${log-path}/rollingfile.log
      filePattern: ${archive}/rollingfile.log.%d{yyyy-MM-dd-hh-mm}.gz
      name: RollingFile-Appender
  loggers:
    logger:
      additivity: 'false'
      appender-ref:
      - level: info
        ref: Console-Appender
      - level: error
        ref: File-Appender
      - level: debug
        ref: RollingFile-Appender
      level: debug
      name: guru.springframework.blog.log4j2json
    root:
      appender-ref:
        ref: Console-Appender
      level: debug

(上面使用yaml from-json log4j2.json进行转换,命令是从 ruamel安装. yaml.cmd

当然可以保证这有效,因为有多种方法可以将XML层次结构转换为YAML.但是,YAML和JSON的解析不太可能有所不同.

${}的扩展必须在加载YAML文件后通过行走数据结构来完成,并且不可能通过以情况不敏感的方式匹配原始映射键来完成.

本文地址:https://www.itbaoku.cn/post/1575224.html

问题描述

I am trying to use properties in a log4j2.yaml. The equivalent XML is this.

<Configuration>
<Properties>
    <Property name="log-path">logs</Property>
    <Property name="archive">${log-path}/archive</Property>
</Properties>
<Appenders>
. . .

I tried this.

Configutation:
  name: Default
  properties:
    property:
      name: log-path
      value: "logs"
      name: archive
      value: ${log-path}/archive
  Appenders:

But the properties are not getting picked. For example, the following code creates a ${log-path} folder to store a log file instead of the desired logs folder.

fileName: ${log-path}/rollingfile.log

What am I doing wrong?

推荐答案

If you look at the log4j2.json file you can see that the property key has to have a value that is is list of (again) key-value pairs. Translated to YAML this looks like the beginning of this file:

configuration:
  name: Default
  properties:
    property:
    - name: log-path
      value: logs
    - name: archive
      value: ${log-path}/archive
  appenders:
    Console:
      PatternLayout:
        pattern: '[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n'
      name: Console-Appender
      target: SYSTEM_OUT
    File:
      PatternLayout:
        pattern: '[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n'
      fileName: ${log-path}/logfile.log
      name: File-Appender
    RollingFile:
      DefaultRolloverStrategy:
        max: '30'
      PatternLayout:
        pattern: '[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n'
      Policies:
        SizeBasedTriggeringPolicy:
          size: 1 KB
      fileName: ${log-path}/rollingfile.log
      filePattern: ${archive}/rollingfile.log.%d{yyyy-MM-dd-hh-mm}.gz
      name: RollingFile-Appender
  loggers:
    logger:
      additivity: 'false'
      appender-ref:
      - level: info
        ref: Console-Appender
      - level: error
        ref: File-Appender
      - level: debug
        ref: RollingFile-Appender
      level: debug
      name: guru.springframework.blog.log4j2json
    root:
      appender-ref:
        ref: Console-Appender
      level: debug

(the above was converted using yaml from-json log4j2.json, with the command being installed from ruamel.yaml.cmd

There is of course guarantee that this works, as there are multiple ways to convert an XML hierarchy to YAML. But it is not very likely that parsing of YAML and JSON differ.

The expansion of ${} has to be done after loading the YAML file, by walking the data-structure, and it is unlikely that this is done by matching the original mapping keys in a case-insensitive way.