69

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?

rolve
  • 10,083
  • 4
  • 55
  • 75
Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
  • I suspect that it's only using your home directory as the base because that's the current working directory of the Java process… – Donal Fellows Oct 25 '12 at 10:13

9 Answers9

63

When parsing its configuration file, the expression ${MY_HOME} will be expanded to the value of the system property named MY_HOME, not the system environment variable. There's a difference between the two.

To achieve this in a clean way, you'll have to add something like this to the JVM invocation line:

-DMY_HOME=$MY_HOME

That would define the Java system property MY_HOME to contain the value of the environment variable MY_HOME.

Isaac
  • 16,458
  • 5
  • 57
  • 81
  • Thank you for the reply. Can you please tell me how to add the above statement to JVM invocation line. – Ankur Shanbhag Oct 25 '12 at 10:27
  • You had mentioned that your code is deployed on a Unix machine, as a runnable JAR. Somewhere on your Unix machine there is a shell script (or something of the like) that invokes the JAR file (using `java -jar myapp.jar`, plus parameters); simply add `-DMY_HOME=$MY_HOME` to that line. – Isaac Oct 25 '12 at 10:31
  • 1
    ohh ok. Thank you @Isaac for the help. So that means I dont require any changes in my XML file: Here is the code : – Ankur Shanbhag Oct 25 '12 at 10:47
  • Right. You won't require any changes in the code. (If you do it right...) – Isaac Oct 25 '12 at 17:11
  • May I ask, If I want to debug from Eclipse, how I could pass this variable into code. I tried pass variable from Eclipse -> Run Configuration -> Arguments -> put "DMY_HOME=$MY_HOME" in Program arguments. And I tried to print out "MY_HOME" variable with "System.out.println("System MY_HOME: " + System.getenv("MY_HOME"));" it ends up with an null value. – user454083 Jan 04 '13 at 08:50
  • 2
    Put `-DMY_HOME=$MY_HOME` (don't forget the hyphen) in the *VM Arguments* section, not the *Program arguments* section. – Isaac Jan 04 '13 at 16:16
63

you CAN give it environment variables. Just preppend env: before the variable name, like this:

value="${env:MY_HOME}/logs/message.log"
Daniel Estrada
  • 926
  • 8
  • 9
  • 5
    This is the ultimate answer considering you are using Log4j2 and not the first version. Thank you! – joninx Nov 09 '15 at 12:07
  • Hi @vikingsteve, regarding "This is for log4j 2.X. Doesnt work for 1.2.17", even though I configured to use log 2.8.2, it still did not read from the environment variable, my shell script is a spark_submit job. below is my dependency: org.apache.logging.log4j log4j-core 2.8.2 In the end, I had to use system property approach. – soMuchToLearnAndShare Aug 21 '17 at 16:26
16

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

Shoham
  • 7,014
  • 8
  • 40
  • 40
11

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

James Z
  • 12,209
  • 10
  • 24
  • 44
hmehandi
  • 356
  • 4
  • 11
2

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

Corey Adler
  • 15,897
  • 18
  • 66
  • 80
1

Maybe... :

datestamp=yyyy-MM-dd/HH:mm:ss.SSS/zzz
layout=%d{${datestamp}} ms=%-4r [%t] %-5p %l %n%m %n%n

# infoFile 
log4j.appender.infoFile=org.apache.log4j.RollingFileAppender
log4j.appender.infoFile.File=${MY_HOME}/logs/message.log
log4j.appender.infoFile.layout=org.apache.log4j.PatternLayout
log4j.appender.infoFile.layout.ConversionPattern=${layout}
tux23
  • 215
  • 4
  • 11
  • This will is the code for properties file. Will ${MY_HOME}/logs/message.log work with XML file as well. Because I had tried using similar stuff with XML file, but did not work. – Ankur Shanbhag Oct 25 '12 at 10:22
0

Log4j entry

#- File to log to and log format

log4j.appender.file.File=${LOG_PATH}/mylogfile.log

Java program
String log4jConfPath        = "path/log4j.properties";
File log4jFile              = new File(log4jConfPath);
if (log4jFile.exists()) {
    System.setProperty("LOG_PATH", "c:/temp/");
    PropertyConfigurator.configure(log4jFile.getAbsolutePath());
    logger.trace("test123");
}
-2

To dynamically change a variable you can do something like this:

String value = System.getenv("MY_HOME");
Properties prop = new Properties("log4j.properties"); 
prop.put("MY_HOME", value); // overwrite with value from environment
PropertyConfigurator.configure(prop);
ikaerom
  • 538
  • 5
  • 27
tux23
  • 215
  • 4
  • 11
  • This is for my configuration file. This will work, no issues with it. But what I want is to append the $MY_HOME path to the file appender path, which I am using for logging messages in my XML config file. – Ankur Shanbhag Oct 25 '12 at 10:25
  • May I ask in this case, how I could pass "MY_HOME" variable into my Java Application. At moment, I have the following piece of code "System.out.println("System MY_HOME: " + System.getenv("MY_HOME"));" But it end up print out a "System MY_HOME: null" when I trying to pass "MY_HOME" from Eclipse -> Run Configuration -> Arguments -> Program arguments : -DMY_HOME=$MY_HOME – user454083 Jan 04 '13 at 08:43
  • new Properties(String) doesn't even exist?! So what does that could help for? – Philipp Jun 17 '16 at 02:18
-4

Since you are using unix you can use a path like this.

  /home/Production/modulename/logs/message.log

path should start with /

someone
  • 6,577
  • 7
  • 37
  • 60