Log4j2:如何在 Yaml 配置中配置多个相同类型的 Appender
Log4j2 : How to configure multiple Appender with same type in Yaml configuration
我正在尝试在同一个 YAML 配置中使用 2 个 RollingRandomAccessFile。我可以在 XML 中完成,但不能在 YAML 中完成。结果,我想要两个文件 "application.log" 和 "payload.log".
这是我的工作 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>
这是我的非工作 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
这是我在 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
如果我用 RollingFile 替换第二个 RollingRandomAccessFile,它会完美运行。
我终于找到了解决办法,滥用了列表
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
我必须为每个记录器设置不同的阈值级别,这就是它的样子:
---
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
...
默认日志级别变为配置给 Root Logger 的级别。如果要覆盖级别,请将级别添加到每个 AppenderRef
.
我正在尝试在同一个 YAML 配置中使用 2 个 RollingRandomAccessFile。我可以在 XML 中完成,但不能在 YAML 中完成。结果,我想要两个文件 "application.log" 和 "payload.log".
这是我的工作 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>
这是我的非工作 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
这是我在 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
如果我用 RollingFile 替换第二个 RollingRandomAccessFile,它会完美运行。
我终于找到了解决办法,滥用了列表
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
我必须为每个记录器设置不同的阈值级别,这就是它的样子:
---
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
...
默认日志级别变为配置给 Root Logger 的级别。如果要覆盖级别,请将级别添加到每个 AppenderRef
.