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

查看:642
本文介绍了在Log4J2 YAML中使用属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用log4j2.yaml中的属性.等效的XML是这样.

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

我尝试过这个.

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

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

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

我在做什么错了?

推荐答案

如果查看log4j2.json文件,您会发现property键必须具有(再次)键列表的值,值对.转换为YAML看起来像是该文件的开头:

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

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

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

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

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.

${}的扩展必须在加载YAML文件之后通过遍历数据结构来完成,并且不太可能通过不区分大小写的方式匹配原始映射键来完成.

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.

这篇关于在Log4J2 YAML中使用属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆