替换RollingFileAppender中log4j2文件名中的字符串

时间:2018-05-22 14:21:59

标签: regex log4j2

我正在尝试替换我在文件名中使用的环境变量中的字符串,如下所示。但输出文件是按字面意思生成的'%replace'。有可能做这样的替换吗?如果没有,还有其他选择吗?基本上,我想取代' abc'在环境变量$ {current.env}中有空白

    <RollingFile name="FileA"
        fileName="${sys:file.path}/%replace{${sys:current.env}}{'abc'}{}-xyz.log"
        filePattern="${sys:file.path}/%replace{${sys:current.env}}{'abc'}{}-xyz-%d{yyyyMMdd-HHmm}.log">
        <PatternLayout>
            <pattern>%m%n</pattern>
        </PatternLayout>
        <Policies>
            <OnStartupTriggeringPolicy />
            <TimeBasedTriggeringPolicy />
        </Policies>
        <DefaultRolloverStrategy max="5" />
    </RollingFile>

1 个答案:

答案 0 :(得分:0)

以下是一些示例代码,演示如何创建能够替换查找结果中的字符串的自定义查找。

首先生成日志记录的类:

package example;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class SomeClass {

    private static final Logger log = LogManager.getLogger();   

    public static void main(String[] args){

        log.info("Here's some info!");
        log.error("Some error happened!");
    }
}

接下来定义自定义查找的类:

package example;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.lookup.StrLookup;

@Plugin(name = "sysWithReplace", category = "Lookup")
public class SystemLookupWithReplace implements StrLookup{
    /**
     * Lookup the value for the key.
     * @param key  the key to be looked up, may be null
     * @return The value for the key.
     */
    public String lookup(String key) {
        if(key == null){
            return null;
        }
        if(!key.contains(";")){
            return System.getProperty(key);
        }
        String[] params = key.split(";");

        String value = System.getProperty(params[0]);
        if(params.length >= 2 && value != null){
            if(params.length < 3){
                return value.replace(params[1], "");
            }
            return value.replace(params[1], params[2]);
        }
        return value;
    }


    /**
     * Lookup the value for the key using the data in the LogEvent.
     * @param event The current LogEvent.
     * @param key  the key to be looked up, may be null
     * @return The value associated with the key.
     */
    public String lookup(LogEvent event, String key) {
        return lookup(key);
    }
}

以下是使用自定义查找的示例log4j2.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %c ${sysWithReplace:os.name;dows 10;doze} - %msg%n" />
        </Console>
    </Appenders>

    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" level="info" />
        </Root>
    </Loggers>
</Configuration>

最后是日志记录输出(我在Windows 10上运行它,所以用“打盹”替换“dows 10”:

14:30:26.580 [main] INFO  example.SomeClass Windoze - Here's some info!
14:30:26.581 [main] ERROR example.SomeClass Windoze - Some error happened!