Search code examples
javawebspherelog4j2websphere-libertyopen-liberty

Unable to configure log4j in WebSphere Liberty profile for multiple apps


I am deploying multiple webapps in liberty all of which have their own log4j configuration files.The log4j config file in each of these WAR's is located in the WEB-INF/classes folder of the WAR. The web.xml snippet which adds the log4j config is as follows:

<context-param>
                <param-name>log4jConfiguration</param-name>
                <param-value>classpath:portal-log4j2.xml</param-value>
        </context-param>

This seems to work in all other Tomcat and Jboss application servers but not in WebSphere Liberty profile. Even in WebSphere Basic profile the same seems to work. Specifying -Dlog4j.configurationFile in jvm.options wont work for me as i have multiple apps

I looked at Websphere Liberty: How to specify log4j2 configuration location?

and tried the same. For me this resulted in below error

Exception = javax.xml.stream.XMLStreamException
Source = com.ibm.ws.artifact.loose.internal.LooseContainerFactoryHelper
probeid = 126
Stack Dump = javax.xml.stream.XMLStreamException: ParseError at [row,col]:[8,46]
Message: found: CHARACTERS, expected START_ELEMENT or END_ELEMENT
        at java.xml/com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.nextTag(XMLStreamReaderImpl.java:1361)
        at com.ibm.ws.artifact.loose.internal.LooseContainerFactoryHelper.createContainer(LooseContainerFactoryHelper.java:80)

My log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error" name="mbaasportal" packages="com.kony.mobilefabric.logger">
    <!-- Filter to control log level dynamically. The name of the key should match with key in TenantContextFilter.java -->
	<DynamicLogFilter key="KONY_PORTAL_DYNAMIC_LOG_LEVEL" />
	<Appenders>
        <CustomRollingFile name="logfileappender" fileName="${sys:LOG_LOCATION}/mbaasportal.log" append="true" filePattern="${sys:LOG_LOCATION}/mbaasportal.log.%i">
            <MobileFabricPatternLayout>
                <ProductName>console.portal</ProductName>
            </MobileFabricPatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="10 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </CustomRollingFile>

        <Console name="dockerappender" target="SYSTEM_OUT">            
            <JsonLayout compact="true" eventEol="true" properties="true" locationInfo="true">
                <KeyValuePair key="fabricService" value="console.portal"/>
            </JsonLayout>
        </Console>

        <Console name="consoleappender" target="SYSTEM_OUT"> 
            <MobileFabricPatternLayout> 
                <ProductName>console.portal</ProductName>
            </MobileFabricPatternLayout> 
        </Console>

        <Socket name="SYSLOGappender" host="localhost" port="514" protocol="UDP">
            <Rfc3164SyslogLayout charset="ISO-8859-1" ProductName="console.portal"/>
        </Socket>
    </Appenders>

    <Loggers>
    
    	<Logger name="org.hibernate" level="ERROR" additivity="false">
            <AppenderRef ref="${sys:LOG_OPTION}appender"/>
        </Logger>

        <Logger name="org.springframework" level="ERROR" additivity="false">
            <AppenderRef ref="${sys:LOG_OPTION}appender"/>
        </Logger>

        <Logger name="httpclient" level="ERROR" additivity="false">
            <AppenderRef ref="${sys:LOG_OPTION}appender"/>
        </Logger>
        
        <Logger name="com.kony.mbaas.portal.wsclient.RestTemplateWrapper" level="ERROR" additivity="false">
            <AppenderRef ref="${sys:LOG_OPTION}appender"/>
        </Logger>

        <Root level="${sys:LOG_LEVEL}">
            <AppenderRef ref="${sys:LOG_OPTION}appender"/>
        </Root>
    </Loggers>
</Configuration>

Any guidance on the same greatly appreciated


Solution

  • I worked with Brian from IBM support to resolve this issue. Appears the issue is specific to Log4j2 2.8 on Liberty and upgrading to v2.13 resolved the issue. For details refer the following:

    https://developer.ibm.com/answers/questions/526956/log4j-config-file-present-in-webapp-not-loaded-in.html