How to give environmental variable path for file a

2019-01-10 20:42发布

I have a log4j.xml config file. and a RollingFileAppender to which I need to provide file path for storing logs. The problem is my code will be deployed on Unix machine as a runnable jar. So if I pass parameter something like this:

value=logs/messages.log"

it creates folder named logs inside my HOME directory and writes all the messages to file inside this directory.

I have a environmental variable set to some value. I want to use path of that variable and write messages under that path. How can I achieve it?

I had tried using this:

value="${MY_HOME}/logs/message.log"

but this does not work. Can anyone suggest a solution for this problem?

9条回答
手持菜刀,她持情操
2楼-- · 2019-01-10 21:09

I got this working.

  1. In my log4j.properties. I specified

log4j.appender.file.File=${LogFilePath}

  1. in eclipse - JVM arguments

-DLogFilePath=C:\work\MyLogFile.log

查看更多
Lonely孤独者°
3楼-- · 2019-01-10 21:13

java -DLOG_DIR=${LOG_DIR} -jar myjar.jar "param1" "param2" ==> in cmd line if you have "value="${LOG_DIR}/log/clientProject/project-error.log" in xml

查看更多
ら.Afraid
4楼-- · 2019-01-10 21:19

This syntax is documented only in log4j 2.X so make sure you are using the correct version.

    <Appenders>
    <File name="file" fileName="${env:LOG_PATH}">
        <PatternLayout>
            <Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
        </PatternLayout>
    </File>
</Appenders>

http://logging.apache.org/log4j/2.x/manual/lookups.html#EnvironmentLookup

查看更多
登录 后发表回答