Log4j2:如何在 log4j2.properties 文件中使用替换参数
Log4j2: How to use replace parameter in log4j2.properties file
使用 log4j2,我想替换日志消息中的一些字符串(比如将 foo 替换为 bar)。在 xml 配置中,我可以使用以下配置并且它有效。
<Appenders>
<Console name="A1" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{HH:mm:ss}] %m%n">
<replace regex = "foo" replacement="bar"/>
</PatternLayout>
</Console>
</Appenders>
但是没有使用 XML,我必须在我的项目中使用属性文件,所以我尝试了类似
的方法
appender.A1.type = Console
appender.A1.name = A1
appender.A1.layout.type = PatternLayout
appender.A1.layout.pattern = [%d{HH:mm:ss}] %m%n
# Probably BAD code begin
appender.A1.layout.replace.regex = foo
appender.A1.layout.replace.replacement = bar
# Probably BAD code end
我得到了这样的错误:
Exception in thread "main" org.apache.logging.log4j.core.config.ConfigurationException: No type attribute provided for component replace
at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder.createComponent(PropertiesConfigurationBuilder.java:334)
...
如何在属性文件和 log4j2 中表示 replace,是否可以转换 xml 配置中的所有内容到属性配置?
我正在使用 log4j-api-2.14.0.jar 和 log4j-core-2.14.0.jar.
提前致谢!
编辑:
我之前也试过replace{pattern}{regex}{substitution}
。而且我认为这与 replace 参数不同。
假设模式是
appender.A1.layout.pattern = [%d{HH:mm:ss}][%C::%M] %replace{%m}{foo}{bar}%n
我会得到
[09:28:07][com.foo.MyApp::main] 111 bar 222
但我想得到的是
[09:28:07][com.bar.MyApp::main] 111 bar 222
使用上面的XML配置,我可以得到正确的结果,但我不知道如何将它转换成属性配置。
对于正则表达式替换,使用 replace{pattern}{regex}{substitution}
模式。 appender.A1.layout.replace.*
必须删除。
appender.A1.type = Console
appender.A1.name = A1
appender.A1.layout.type = PatternLayout
appender.A1.layout.pattern = [%d{HH:mm:ss}] %replace{%m}{foo}{bar}%n
# whole line
appender.A1.layout.pattern = %replace{[%d{HH:mm:ss}] %m%n}{foo}{bar}
了解更多信息
replace{pattern}{regex}{substitution}
Replaces occurrences of 'regex', a regular expression, with its replacement 'substitution' in the string resulting from evaluation of the pattern. For example, "%replace{%msg}{\s}{}" will remove all spaces contained in the event message.
The pattern can be arbitrarily complex and in particular can contain multiple conversion keywords. For instance, "%replace{%logger %msg}{.}{/}" will replace all dots in the logger or the message of the event with a forward slash.
使用 log4j2,我想替换日志消息中的一些字符串(比如将 foo 替换为 bar)。在 xml 配置中,我可以使用以下配置并且它有效。
<Appenders>
<Console name="A1" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{HH:mm:ss}] %m%n">
<replace regex = "foo" replacement="bar"/>
</PatternLayout>
</Console>
</Appenders>
但是没有使用 XML,我必须在我的项目中使用属性文件,所以我尝试了类似
的方法appender.A1.type = Console
appender.A1.name = A1
appender.A1.layout.type = PatternLayout
appender.A1.layout.pattern = [%d{HH:mm:ss}] %m%n
# Probably BAD code begin
appender.A1.layout.replace.regex = foo
appender.A1.layout.replace.replacement = bar
# Probably BAD code end
我得到了这样的错误:
Exception in thread "main" org.apache.logging.log4j.core.config.ConfigurationException: No type attribute provided for component replace
at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder.createComponent(PropertiesConfigurationBuilder.java:334)
...
如何在属性文件和 log4j2 中表示 replace,是否可以转换 xml 配置中的所有内容到属性配置? 我正在使用 log4j-api-2.14.0.jar 和 log4j-core-2.14.0.jar.
提前致谢!
编辑:
我之前也试过replace{pattern}{regex}{substitution}
。而且我认为这与 replace 参数不同。
假设模式是
appender.A1.layout.pattern = [%d{HH:mm:ss}][%C::%M] %replace{%m}{foo}{bar}%n
我会得到
[09:28:07][com.foo.MyApp::main] 111 bar 222
但我想得到的是
[09:28:07][com.bar.MyApp::main] 111 bar 222
使用上面的XML配置,我可以得到正确的结果,但我不知道如何将它转换成属性配置。
对于正则表达式替换,使用 replace{pattern}{regex}{substitution}
模式。 appender.A1.layout.replace.*
必须删除。
appender.A1.type = Console
appender.A1.name = A1
appender.A1.layout.type = PatternLayout
appender.A1.layout.pattern = [%d{HH:mm:ss}] %replace{%m}{foo}{bar}%n
# whole line
appender.A1.layout.pattern = %replace{[%d{HH:mm:ss}] %m%n}{foo}{bar}
了解更多信息
replace{pattern}{regex}{substitution}
Replaces occurrences of 'regex', a regular expression, with its replacement 'substitution' in the string resulting from evaluation of the pattern. For example, "%replace{%msg}{\s}{}" will remove all spaces contained in the event message.
The pattern can be arbitrarily complex and in particular can contain multiple conversion keywords. For instance, "%replace{%logger %msg}{.}{/}" will replace all dots in the logger or the message of the event with a forward slash.