skip to Main Content

Here’s what I do in SpringBoot on Windows to read an environment variable (location of log folder).

  1. In Windows Server, I set a System environment variable for "LOG_HOME" with the value with the directory that SpringBoot should use to write logs.

  2. In SpringBoot’s application.properties, I have:

logging.file.name= ${LOG_HOME}/ws.log

Works great!


But in Ubuntu Linux 20.04, the same approach doesn’t work for me at all.

When the WAR file tries to deploy on Ubuntu 20.04 using this similar technique:

  1. (in .bashrc): export LOG_HOME = /home/ubuntu/logs
  2. reboot (to reload the environment for sure)

I get this error in the Tomcat log when trying to deploy the WAR file:

java.lang.IllegalArgumentException: Could not resolve placeholder ‘LOG_HOME’ in value "${LOG_HOME}/ws.log"

So, it seems that Spring doesn’t see the environment variable set in Ubuntu.

I wrote a simple Java program just to check the value of the environment variables and they were all created as expected including the LOG_HOME as shown in Linux "printenv".

If possible, I need a technique that will work on Ubuntu without changing the working SpringBoot implementation on Windows Server.

Thanks in advance for suggestions.

3

Answers


  1. Chosen as BEST ANSWER

    The solution for me posted by the extremely helpful satyesht above, was to edit the Catalina.sh file and add the "-D" name-value pair option under CATALINA_OPTS. Thanks to all who posted. :)


  2. Instead of exporting in shell session like

    export LOG_HOME = /home/ubuntu/logs
    

    try this as -D VM argument in your starup command
    eg:
    java -cp=xxx mainclass -DLOG_HOME=/home/ubuntu/log

    if you are using tomcat then :
    VM args can be added catalina.sh file under CATALINA_OPTS.

    Login or Signup to reply.
  3. For tomcat, add your environment variables to $TOMCAT_HOME/bin/setenv.sh where $TOMCAT_HOME is the directory of your tomcat installation.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search