Warm tip: This article is reproduced from stackoverflow.com, please click
java lookup variables log4j2

log4J2 Store and use variables / lookup-values

发布于 2020-03-27 15:43:53

In my java program I want to use several log files.

So according to the log4j documentation I would assume that lookup values are use to do that.

The Lookups page describe how to build the configuration files. But has only little information about how to store the values so that the configuration file is retrieving the values.

So I want to have logfilename dynamically filled.

Test with envrimonment works:

<File name="MyFile" fileName="${env:USERERNAME}" immediateFlush="false" append="false">
        <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>

I would assume that System.getProperty("USER") would match the java lookup ${java:USER} but it does not work.

I tested both variables using:

System.out.println("USER: " + System.getProperty("USER"));
System.out.println("USERNAME: " + System.getenv("USERNAME"));

So how to fill the variables for

  • Context Map Lookup
  • Java lookup

Which are the differences?

Questioner
Thorsten Niehues
Viewed
92
Vikas Sachdeva 2017-10-05 09:24

There are various ways of lookup in log4j2. Let me add some details to them -

Context Map Lookup - For putting value using context map, use below code -

org.apache.logging.log4j.ThreadContext.put("logFileName", "app.log");

Variable can be accessed in configuration file using ${ctx:logFileName} or %X{logFileName}.

Context map lookup is generally used in web application where you want some value for each user request i.e. for each thread.

Environment Lookup - Environment lookup is used for looking up system environment variables like USERNAME, PLATFORM. You can print all environment variables in Java using below -

System.out.println(System.getenv());

OR any specific environment variable -

System.out.println(System.getenv("NUMBER_OF_PROCESSORS"));

However, from Java program, you can not set environment variables.

As you mentioned in your code, environment variables can be accessed using ${env:NUMBER_OF_PROCESSORS} syntax.

Java Lookup - Java lookup is for Java environment information like java version, hardware etc. Variables are fixed and can not be set in java program.

Such variables can be accessed using ${java:vm} syntax where vm is java environment related variable name.

System Properties Lookup - It is easy to get and set such properties using below code -

System.setProperty("logFileName", "app.log");
System.getProperty("logFileName");

Variables can be accessed in log4j2 configuration file using ${sys:logFileName} syntax.

Good thing about these system properties is you can pass these properties as VM Arguments to your program like -DlogFileName=app3.log.

There are other lookup mechanism also like Date Lookup, Map Lookup etc which are mostly used. Rest details can be checked here


Since you want to set file name dynamically in your application and if you want to set file name only once, I would suggest to you System property. But ensure that, you set System Property before initializing log4j