简体   繁体   中英

Log4j2: How to use replace parameter in log4j2.properties file

Using log4j2, I want to replace some strings in log message (say replace foo with bar ). In xml configuration, I can use the following configuration and it works.

<Appenders>
     <Console name="A1" target="SYSTEM_OUT">
         <PatternLayout pattern="[%d{HH:mm:ss}] %m%n">
             <replace regex = "foo" replacement="bar"/>
         </PatternLayout>
     </Console>
</Appenders>

But insted of using XML, I have to use properties file in my project, so I tried something like

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

And I got error like:

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)
...

How can I represent replace in properties file and in log4j2, is it possible to transform everything in xml configuration to properties configuration? I'm using log4j-api-2.14.0.jar and log4j-core-2.14.0.jar.

Thx in advance!

EDIT: I 've also tried replace{pattern}{regex}{substitution} before. And I think this is different from the replace parameter. Say the pattern is

appender.A1.layout.pattern = [%d{HH:mm:ss}][%C::%M] %replace{%m}{foo}{bar}%n

And I will get

[09:28:07][com.foo.MyApp::main] 111 bar 222

but what I want to get is

[09:28:07][com.bar.MyApp::main] 111 bar 222

Using above XML configuration, I can get the correct result but I don't know how to convert it into properties configuration.

For regex replacement use replace{pattern}{regex}{substitution} pattern. appender.A1.layout.replace.* must be removed.

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}

For More Information

Log4j2 - Layouts

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM