9

export logback.configurationFile=123 on mac os x got this: "-bash: export: `logback.configurationFile=123': not a valid identifier"

Tried export logback_configurationFile=123 that worked. but logback does not seem to recognize that env variable. Any suggestions?

Bobo
  • 8,777
  • 18
  • 66
  • 85

3 Answers3

31

-Dlogback.configurationFile is not the same as setting it as an Environment variable.

The -D flag is a Java parameter to your program (accessible by System.getProperty()) while the environment variable defined by export will be accessible by System.getenv(). Looking at the Logback code it looks like it uses System.getProperty() to read the logback.configurationFile value.

So in reality you have to pass the parameter to the JVM on startup, this means that you can set the environment variable to whatever you like and then just use it in when you start the JVM.

export LOGBACK_CONFIG_FILE_LOCATION=/tmp/logback.conf
java -Dlogback.configurationFile=${LOGBACK_CONFIG_FILE_LOCATION}
Peter Svensson
  • 6,105
  • 1
  • 31
  • 31
  • You can also use `System.setEnv(...` FWIW https://stackoverflow.com/questions/21885787/setting-logback-xml-path-programmatically – rogerdpack Feb 01 '19 at 17:26
2

I guess that also that way should work. But I did not realy try it by my own.

export JAVA_OPTS="$JAVA_OPTS -Dlogback.configurationFile=123"

Then you would not need to set the opt on the call of your app. But the drawback would be that all running Java programs would use this setting.

Robert
  • 29
  • 1
1

You can also do the following Export your JVM arguments with logback configurationFile.

export ALC_JVM_ARGS="-Dlogback.configurationFile=logconfig.xml"

Export other things you need like main class and it's arguments

export MAIN_CLASS="Spring"
export MAIN_CLASS_ARGS="beans.xml"

Then execute your program

exec java $ALC_JVM_ARGS $MAIN_CLASS $MAIN_CLASS_ARGS
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289