从log4j2 xml的完整路径中获取文件名[英] Get the file name from complete path in log4j2 xml

本文是小编为大家收集整理的关于从log4j2 xml的完整路径中获取文件名的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

在我的log4j2.xml文件中,我通过传递系统属性

获得了日志文件的完整路径

-dlogfilename =/home/user/logs/logs/pl>

log4j2配置:

<Property name="logFile">${sys:logfilename:-/home/user/logs/server}</Property>

作为另一项要求,我需要从上述属性中获取日志文件的名称,并且无法传递新的系统属性.如何从完整的路径中获得文件的名称?除了用于数据传输外,我没有XML的任何经验.

推荐答案

您正在使用系统属性查找错误.您应该指定查找的查找和钥匙:${sys:logfilename}

这是一个简单的示例:

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!");
    }
}

这是log4j2.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <File name="File" fileName="logs/${sys:myProperty}"
            immediateFlush="false" append="false">
            <PatternLayout
                pattern="%-5level %logger{36} - %msg%n" />
        </File>
    </Appenders>

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

当我使用以下JVM参数运行此代码时:

-DmyProperty=myFile.log

它以" myfile.log"的名称生成一个文件,该文件包含以下输出:

INFO  example.SomeClass - Here's some info!

本文地址:https://www.itbaoku.cn/post/1575166.html

问题描述

In my log4j2.xml file, I get the complete path of the logfile by passing a system property

-Dlogfilename=/home/user/logs/server

Log4j2 configuration:

<Property name="logFile">${sys:logfilename:-/home/user/logs/server}</Property>

As an added requirement, I need to get the name of the log file from the above property and I cannot pass a new system property. How can I get just the name of the file from the complete path? I dont have any experience with XML other than its use for data transport.

推荐答案

You're using the System Properties Lookup incorrectly. You should specify the lookup and the key you wish to look up like this: ${sys:logfilename}

Here's a simple example:

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!");
    }
}

Here is the log4j2.xml config file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <File name="File" fileName="logs/${sys:myProperty}"
            immediateFlush="false" append="false">
            <PatternLayout
                pattern="%-5level %logger{36} - %msg%n" />
        </File>
    </Appenders>

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

When I run this code with the following JVM parameter:

-DmyProperty=myFile.log

it generates a file with the name "myFile.log" and the file contains the following output:

INFO  example.SomeClass - Here's some info!