Log4j2:如何在Yaml配置中配置多个具有相同类型的Appender

时间:2022-02-16 21:52:12

I'm trying to have 2 RollingRandomAccessFile in the same YAML configuration. I'm able to do it in XML but not in YAML. As a result, I want two files "application.log" and "payload.log".

我正在尝试在同一个YAML配置中使用2个RollingRandomAccessFile。我能用XML完成,但不能用YAML。结果,我想要两个文件“application.log”和“payload.log”。

Here's my working XML configuration :

这是我的工作XML配置:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
    <Appenders>

        <Console name="CONSOLE" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>

        <RollingRandomAccessFile name="PAYLOAD" fileName="logs/payload.log"
                                 filePattern="logs/$${date:yyyy-MM}/payload-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="1 KB"/>
            </Policies>
        </RollingRandomAccessFile>

        <RollingRandomAccessFile name="APPLICATION" fileName="logs/application.log"
                                 filePattern="logs/$${date:yyyy-MM}/application-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="1 KB"/>
            </Policies>
        </RollingRandomAccessFile>

    </Appenders>

    <Loggers>
        <Root level="info" additivity="false">
            <appender-ref ref="CONSOLE" level="trace"/>
            <appender-ref ref="APPLICATION" level="trace"/>
            <appender-ref ref="PAYLOAD" level="trace"/>
        </Root>
    </Loggers>

</Configuration>

Here's my non-working YAML configuration. The second declaration of RollingRandomAccessFile overrides the first one, resulting with only "application.log" :

这是我的非工作YAML配置。 RollingRandomAccessFile的第二个声明会覆盖第一个声明,只有“application.log”:

Configuration:
  status: debug

  Appenders:
    Console:
      name: CONSOLE
      target: SYSTEM_OUT
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
    RollingRandomAccessFile:
      name: PAYLOAD
      fileName: logs/payload.log
      filePattern: "logs/$${date:yyyy-MM}/payload-%d{MM-dd-yyyy}-%i.log.gz"
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
      Policies:
        SizeBasedTriggeringPolicy:
          size: 1 KB
    RollingRandomAccessFile:
      name: APPLICATION
      fileName: logs/application.log
      filePattern: "logs/$${date:yyyy-MM}/application-%d{MM-dd-yyyy}-%i.log.gz"
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
      Policies:
        SizeBasedTriggeringPolicy:
          size: 1 KB

  Loggers:
    Root:
      level: debug
      AppenderRef:
        - ref: CONSOLE
        - ref: PAYLOAD
        - ref: APPLICATION

Here's what I tried using List at the Appender level and with that there's no ouput, even the console :

这是我尝试在Appender级别使用List并且没有输出,甚至是控制台:

Configuration:
  status: debug

  Appenders:
    - Console:
      name: CONSOLE
      target: SYSTEM_OUT
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
    - RollingRandomAccessFile:
      name: PAYLOAD
      fileName: logs/payload.log
      filePattern: "logs/$${date:yyyy-MM}/payload-%d{MM-dd-yyyy}-%i.log.gz"
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
      Policies:
        SizeBasedTriggeringPolicy:
          size: 1 KB
    - RollingRandomAccessFile:
      name: APPLICATION
      fileName: logs/application.log
      filePattern: "logs/$${date:yyyy-MM}/application-%d{MM-dd-yyyy}-%i.log.gz"
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
      Policies:
        SizeBasedTriggeringPolicy:
          size: 1 KB

  Loggers:
    Root:
      level: debug
      AppenderRef:
        - ref: CONSOLE
        - ref: PAYLOAD
        - ref: APPLICATION

If I replace the second RollingRandomAccessFile by a RollingFile, it works perfectly.

如果我用RollingFile替换第二个RollingRandomAccessFile,它可以很好地工作。

2 个解决方案

#1


I finally found the solution, was misusing the list

我终于找到了解决方案,误用了清单

Configuration:
  status: debug

  Appenders:
    Console:
      name: CONSOLE
      target: SYSTEM_OUT
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
    RollingRandomAccessFile:
      - name: PAYLOAD
        fileName: logs/payload.log
        filePattern: "logs/$${date:yyyy-MM}/payload-%d{MM-dd-yyyy}-%i.log.gz"
        PatternLayout:
          Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
        Policies:
          SizeBasedTriggeringPolicy:
            size: 1 KB
      - name: APPLICATION
        fileName: logs/application.log
        filePattern: "logs/$${date:yyyy-MM}/application-%d{MM-dd-yyyy}-%i.log.gz"
        PatternLayout:
          Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
        Policies:
          SizeBasedTriggeringPolicy:
            size: 1 KB

  Loggers:
    Root:
      level: debug
      AppenderRef:
        - ref: CONSOLE
        - ref: PAYLOAD
        - ref: APPLICATION

#2


I had to set different threshold levels for each of the Logger and this is how it looks:

我必须为每个Logger设置不同的阈值级别,这就是它的外观:

---
Configuration:
  status: info
  name: My Application Logger
  thresholdFilter:
    level: debug
  appenders:
    Console:
      name: STDOUT
      PatternLayout:
        Pattern: "%d %p %c{1.} [%t] %m%n"
    RollingRandomAccessFile:
      name: RollingRandomAccessFile
      fileName: logs/app.log
      filePattern: logs/archive/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz
      PatternLayout:
        Pattern: "%d %p %c{1.} [%t] %m%n"
      Policies:
        SizeBasedTriggeringPolicy:
          size: 20 MB
  Loggers:
    logger:
      - name: org.apache.logging.log4j.test2
        level: debug
        additivity: false
        AppenderRef:
          - ref: RollingRandomAccessFile
    Root:
      level: debug
      AppenderRef:
        - ref: RollingRandomAccessFile
          level: warn
        - ref: STDOUT
...

The default log level becomes that configured to Root Logger. If you want to override the level, add the level to each AppenderRef.

默认日志级别将变为配置为Root Logger的日志级别。如果要覆盖级别,请将级别添加到每个AppenderRef。

#1


I finally found the solution, was misusing the list

我终于找到了解决方案,误用了清单

Configuration:
  status: debug

  Appenders:
    Console:
      name: CONSOLE
      target: SYSTEM_OUT
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
    RollingRandomAccessFile:
      - name: PAYLOAD
        fileName: logs/payload.log
        filePattern: "logs/$${date:yyyy-MM}/payload-%d{MM-dd-yyyy}-%i.log.gz"
        PatternLayout:
          Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
        Policies:
          SizeBasedTriggeringPolicy:
            size: 1 KB
      - name: APPLICATION
        fileName: logs/application.log
        filePattern: "logs/$${date:yyyy-MM}/application-%d{MM-dd-yyyy}-%i.log.gz"
        PatternLayout:
          Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
        Policies:
          SizeBasedTriggeringPolicy:
            size: 1 KB

  Loggers:
    Root:
      level: debug
      AppenderRef:
        - ref: CONSOLE
        - ref: PAYLOAD
        - ref: APPLICATION

#2


I had to set different threshold levels for each of the Logger and this is how it looks:

我必须为每个Logger设置不同的阈值级别,这就是它的外观:

---
Configuration:
  status: info
  name: My Application Logger
  thresholdFilter:
    level: debug
  appenders:
    Console:
      name: STDOUT
      PatternLayout:
        Pattern: "%d %p %c{1.} [%t] %m%n"
    RollingRandomAccessFile:
      name: RollingRandomAccessFile
      fileName: logs/app.log
      filePattern: logs/archive/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz
      PatternLayout:
        Pattern: "%d %p %c{1.} [%t] %m%n"
      Policies:
        SizeBasedTriggeringPolicy:
          size: 20 MB
  Loggers:
    logger:
      - name: org.apache.logging.log4j.test2
        level: debug
        additivity: false
        AppenderRef:
          - ref: RollingRandomAccessFile
    Root:
      level: debug
      AppenderRef:
        - ref: RollingRandomAccessFile
          level: warn
        - ref: STDOUT
...

The default log level becomes that configured to Root Logger. If you want to override the level, add the level to each AppenderRef.

默认日志级别将变为配置为Root Logger的日志级别。如果要覆盖级别,请将级别添加到每个AppenderRef。