2

I wish to be able to specify where my log4j2 logs are going to go with either VM arguments or command line arguments.

This is different than all the questions asking how to specify the log4j properties file path.

I figured I could add -Darbitrary.path="path/to/root/dir" in the VM arguments and then reference it in log4j2.properties:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <File name="file" fileName="${arbitrary.path}/application.log">
            <PatternLayout
                pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
        </File>
    </Appenders>
    <Loggers>
        <Root level="info" additivity="false">
            <AppenderRef ref="file" />
        </Root>
    </Loggers>
</Configuration>

Alas, it appears that this does not work. Is there something I'm doing wrong, or some alternative way of getting this functionality?

[EDIT]

Based on the suggestion from @RC., I also tried adding to the command line arguments, --log-dir "/tmp/logMonitor/logs", and changed the reference to ${main:--log-dir}. This variable gets resolved, however, as "-log-dir" and puts the log file in <project_root>/-log-dir/application.log [/EDIT]

4
  • 1
    Did you read: logging.apache.org/log4j/2.0/manual/lookups.html? Commented Jul 12, 2016 at 15:43
  • Based on the part of that link that I believe you are referring to, I attempted another strategy, but without success. I have added information about this to the question. Commented Jul 12, 2016 at 17:25
  • I would use the "environment" variables, it's way easier Commented Jul 12, 2016 at 18:45
  • @user180100 ${log4j:configParentLocation} worked for my needs, thank you! Commented Apr 14, 2018 at 21:44

2 Answers 2

2

Just to clarify, if you provide the path in VM command line arguments, you would use sys Lookup prefix. This was confusing for me at the beginning when I was trying to use jvmrunargs or env and didn't work.

Sign up to request clarification or add additional context in comments.

Comments

1

Log4j 2 can substitute properties in the configuration. The property values can come from many places: system properties, environment, thread context, resource bundles etc. These are called Lookups and need a prefix. For example, use ${sys:arbitrary.path} to find the value in the system properties. See the Lookups section of the Log4j 2 manual.

The alternative is that you define the arbitrary.path property yourself in the configuration:

<Configuration status="warn">
    <Properties>
        <Property name="arbitrary.path">/some/path/"</Property>
    </Properties>
    ...

1 Comment

I think I see how I would do this now. Thanks for the link to Lookups.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.