Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4J2 property substitution - default

I just wonder if there is any way to provide default value for property substitution in LOG4J?

I want to pass file path in java system property and then use it with "${env:mySystemProperty}". But what if developer forgets to set this property? Then I would like to have some meaningful default value defined in log4j2.xml.

Any idea how to achieve this functionality?

EDIT:

The env substitution does not work for me:

standalone.conf

-DoauthLoginLogPath=/path/oauth2.log 

log4j2.xml

<Appender type="File" name="File" fileName="${env:oauthLoginLogPath}" immediateFlush="true"> <Appender type="File" name="File" fileName="${sys:oauthLoginLogPath}" immediateFlush="true"> 

I can see in wildfly console the property, I restarted server but I cannot get it done.

like image 934
Leos Literak Avatar asked Feb 25 '14 23:02

Leos Literak


People also ask

Does log4j properties work with log4j2?

Log4j 2 doesn't support the Log4j v1 ". properties" format anymore (yet, since v2. 4, Log4j supports a Property format, but its syntax is totally different from v1 format). New formats are XML, JSON, and YAML, see the documentation (note: if you used one of these formats in a file called ".

Where do log4j2 properties go?

We should put log4j2. properties anywhere in the application's classpath. Log4j2 will scan all classpath locations to find out this file and then load it. We have put the file in resources folder.

What is JNDI lookup in log4j2?

Log4j uses a lookup feature called Java Naming and Directory Interface (JNDI) that consists of an API (Application Programming Interface) and SPI (Service Provider Interface). Several naming and directory services plug in via the SPI, namely DNS, LDAP, NIS, and more.


2 Answers

Default Property map

Looking at http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution you can specify a default property map in the configuration file. That takes this form:

<Configuration status="debug">   <Properties>     <Property name="oauthLoginLogPath">default/location/of/oauth2.log</Property>   </Properties>   ...   <Appenders>     <Appender type="File" name="File" fileName="${sys:oauthLoginLogPath}">     .... </Configuration 

Then, if you start your app with system property -DoauthLoginLogPath=/path/oauth2.log, the File appender fileName value will first be looked up in system properties, but if that fails, it will fall back to the property defined in the Properties section at the top of the log4j2.xml configuration file.

Inline

A second way is to provide the default value inline:

<Appender type="File" name="File" fileName="${sys:oauthLoginLogPath:-default/location/of/oauth2.log}"> 

Generally all Log4j2 lookups follow this pattern: ${type:key:-defaultValue}.

Env vs sys

By the way, the env prefix is for environment variables (like %PATH% on Windows), and is not related to sys, which is Java system properties. See also http://logging.apache.org/log4j/2.x/manual/lookups.html

like image 105
Remko Popma Avatar answered Sep 28 '22 04:09

Remko Popma


You can use the same ${sys:propName:-default}syntax. Notice the ':-', it is is called "variable default value delimiter". The default value for the "variable default value delimiter" is :-, as in bash and other *nix shells.

You can read more about this in the Log4j 2 documentation for the StrSubstitutor class.

To use the same example:

<Configuration status="debug">   ...     <Appenders>         <Appender type="File" name="File"                   fileName="${sys:oauthLoginLogPath:-default/location/of/oauth2.log}">         ....     </Appenders> </Configuration> 
like image 39
GoGoris Avatar answered Sep 28 '22 04:09

GoGoris