-->

Log4j2 not working in WebLogic 12.2.1

2020-07-23 06:13发布

问题:

I am trying to add log4j logging in my web services running under WebLogic 12.2.1 but somehow the logging is not working.

This is log4j2.xml in WEB-INF\classes of my WAR file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
    <Properties>
        <Property name="log-path">E:/MLM/MyDomain/servers/MyAppSrv01/logs</Property>
    </Properties>
    <Appender type="File" name="File" fileName="${log-path}/Services.log" filePattern="${log-path}/Services-%d{yyyy-MM-dd}.log">
        <Layout type="PatternLayout">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Layout>
    </Appender>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="File"/>
        </Root>
    </Loggers>
</Configuration>

Here is a fragment of my web service codes:

@Path("TestWS")
@Consumes("text/plain")
@Produces("text/plain")
public class TestWS {

    static private Logger logger = LogManager.getLogger();

    public TestWS() {}

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("webservicemethod1")
    public String webservicemethod1(@Context HttpServletRequest request) {
        logger.error("In webservicemethod1");
        ....
    }

}

In WEB-INF\lib\ of my WAR file, I have:

log4j-core-2.5.jar
log4j-api-2.5.jar

I can call the web service successfully using a client program. But I don't see the log file getting created at all. What could be the problem?

Thanks in advance.

回答1:

For WebLogic 12.1.3 and onward, I've needed to add the following to the weblogic.xml file to get log4j to work.

<wls:container-descriptor>
        <wls:prefer-application-packages>
            <wls:package-name>org.slf4j</wls:package-name>
            <wls:package-name>log4j</wls:package-name>
        </wls:prefer-application-packages>
</wls:container-descriptor>


回答2:

I think it's caused by weblogic load org.hibernate.validator.internal.util.Version before deploy application, that class will initialize log4j2 LogManager too early, before you want to set the property substitution.