3
votes

I have JAX-RS web service running on WebLogic 12.2.1. The web service uses log4j2 for logging, and so far it is able to log to the log file as specified in log4j2.xml, as shown here:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
    <Properties>
        <Property name="log-path">E:/MLM/MLMDomain/servers/MLMAppSrv01/logs</Property>
    </Properties>
    <Appenders>
        <RollingFile name="RollingFile" fileName="${log-path}/MLMServices.log" filePattern="${log-path}/MLMServices-%d{yyyy-MM-dd}-%i.log" >
    ....

Next, instead of hardcoding the managed server name "MLMAppSrv01", I changed it to a property substitution, using "${weblogic.Name}", as shown here:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
    <Properties>
        <Property name="log-path">E:/MLM/MLMDomain/servers/${weblogic.Name}/logs</Property>
    </Properties>
    <Appenders>
        <RollingFile name="RollingFile" fileName="${log-path}/MLMServices.log" filePattern="${log-path}/MLMServices-%d{yyyy-MM-dd}-%i.log" >
    ....

Now, instead of replacing ${weblogic.Name} with the actual WebLogic server name, log4j2 simply treated ${weblogic.Name} as an actual sub-folder name, so I ended up having my log file created in the folder:

E:/MLM/MLMDomain/servers/${weblogic.Name}/logs

In the web service codes, when I use System.getProperty("weblogic.Name"), I was able to get the actual managed server that the application was running on, which is "MLMAppSrv01". So it shows that "weblogic.Name" is indeed defined in the JVM.

When I use the modified log4j2.xml file in a stand-alone program, passing in -Dweblogic.Name=MLMAppSrv01, it works fine and the file is created in:

E:/MLM/MLMDomain/servers/MLMAppSrv01/logs

What could be the reason that the ${} in log4j2.xml is not working in WebLogic?

Thanks in advance.

1

1 Answers

2
votes

If the variable is specified as a system property then you need to use ${sys:weblogic.Name} so that Log4j will use the SystemPropertiesLookup to resolve the variable. Without that you will only be able to access variables declared in the properties section.