Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Properties in Log4J2 YAML

Tags:

yaml

log4j2

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?

like image 579
user2693135 Avatar asked Mar 18 '16 04:03

user2693135


People also ask

Can we use log4j properties in log4j2?

Log4j 2 doesn't support the Log4j v1 ". properties" format anymore (yet, since v2. 4, Log4j supports a Property format, but its syntax is totally different from v1 format). New formats are XML, JSON, and YAML, see the documentation (note: if you used one of these formats in a file called ".

How do you read values from a properties file and use it in the log4j2 configuration?

properties and put it in the classpath, Log4j2 automatically looks for configuration files in the classpath. We will keep this in src/main/resource folder. In the above Properties file we have specified a Console appender with basic information like name and a pattern to be used for printing log events.

Where do log4j2 properties go?

We should put log4j2. properties anywhere in the application's classpath. Log4j2 will scan all classpath locations to find out this file and then load it. We have put the file in resources folder.

What is status in log4j2 properties file?

Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged. Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined.


1 Answers

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.

like image 105
Anthon Avatar answered Sep 19 '22 05:09

Anthon