Using system environment variables in log4j xml configuration

333

Solution 1

This syntax is documented only in log4j 2.X so make sure you are using the correct version. It does not work on the 1.X versions.

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

Solution 2

I tried to do that recently and couldn't get it to work. What I ended up doing is sending a variable at startup. So say you have an environment variable called $LOG_LEVEL:

<level value="${log_level}" />

and at startup...

java -Dlog_level=$LOG_LEVEL your_app

Solution 3

I think this is not supported, but basically you can do two things to bring in your environment variables:

  1. Use System.setProperty before Log4J gets configured

  2. Convert (your) environment variables to system properties in your launcher

The first option basically boils down to this:

for (Map<String,String>.Entry entry : System.getenv().entrySet()) {
  System.setProperty(entry.getKey(), entry.getValue());
}

... but the question is of course where to put this code. In particular if you're running within some sort of Tomcat container or similar, this might be troublesome.

The other largely depends on your environment. Basically if you have a shell script that starts your app, you can write some shell magic to set all environment variables as properties, or just the ones you need, e.g.:

java -DMY_ENV=$MY_ENV -DMY_OTHER_ENV=$MY_OTHER_ENV -cp ... com.example.Main

It's also possible to alter your server startup scripts to support this, e.g. catalina.sh or similar.

Solution 4

You need to put a colon between env and the name of the variable, like this:

<level value="${env:LOG_LEVEL}" />
Share:
333
Laura
Author by

Laura

Updated on July 09, 2022

Comments

  • Laura
    Laura almost 2 years

    I'm migrating a swing application to JavaFX. There is a lot of Code, that abstracts from JComponent.

    public abstract class ComponentLayoutHelper<TComponent extends Componend>{...
    

    componend.setBorder()

    ...}

    Properties like the visibility, border and opaque is set to a Component first and afterwards the properties are extended for specific Components like Labels etc.

    public class LabelLayoutHelper extends ComponendLayoutHelper<JLabel>{...

    label.setText();

    ...}

    In this post: Alternative for JComponent in JavaFX they talked about Node as an equivalent to JComponent. But node doesnt have properties like border or opaque. Does someone have an idea how to migrate JComponent?

  • Derek Lewis
    Derek Lewis over 8 years
    I specifically asked how to do this without having to set them all as -D parameters, so this doesn't answer my question at all.
  • Derek Lewis
    Derek Lewis over 8 years
    Does the syntax you show there, ${APP_LOG_ROOTDIR} look at system environment variables, not just Java system properties? My understanding (and experience) has been that it only looks at Java system properties, not environment variables.
  • tkolleh
    tkolleh over 8 years
    @DerekLewis Yes, the syntax looks at system environment variables. Those variables are available to tomcat at startup, similar to the variables set by tomcat via the setenv.bat script. Note, i've only used this on windows. This isn't an issue for me on *nix so never I had to use this solution.
  • tmortiboy
    tmortiboy almost 6 years
    You can also specify a default using the following syntax ${env:LOG_PATH:-/default/path} where /default/path will be used if LOG_PATH is not set.
  • manikanta nvsr
    manikanta nvsr about 4 years
    Is there any work around to make this work using version 1.x?
  • Purushothaman
    Purushothaman almost 4 years
    ${env:LOG_PATH} did not work for me. Rather ${sys:LOG_PATH} worked.