6

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?

user2693135
  • 1,206
  • 5
  • 21
  • 44

1 Answers1

11

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.

Anthon
  • 69,918
  • 32
  • 186
  • 246
  • If I have only one name/value pair, things work. But, on adding the second name/value pair, the problem reoccurs. I faced a similar issue using JSON, and JSON array was the solution. – user2693135 Mar 18 '16 at 11:43
  • @user2693135 I didn't notice before: you cannot have the same key in YAML for a mapping. I updated my answer with a possible solution. Could you update your post with a working JSON version example (that uses an array)? – Anthon Mar 18 '16 at 12:55
  • @user2693135 that has slightly different structure from what I guessed. I updated my answer to use the same structure in YAML as in the JSON file – Anthon Mar 21 '16 at 08:05
  • yaml arrays sux :( thank you for the answer, it helped me – Evgeni Atanasov Jan 09 '18 at 12:36