3

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.

user3573403
  • 1,780
  • 5
  • 38
  • 64

2 Answers2

6

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>
Brian Ochs
  • 1,099
  • 1
  • 10
  • 21
  • It doesn't work. I tried with org.apache.logging.log4j and it's the same. No log file created at all. Is my location of log4j2.xml in WEB-INF\classes in the WAR file correct? – user3573403 Mar 04 '16 at 01:49
  • Looks like the problem is with my log4j2.xml file. I re-do it by copying from the example from https://examples.javacodegeeks.com/enterprise-java/log4j/log4j-2-rollingfileappender-example/ and now it is working. – user3573403 Mar 04 '16 at 06:33
  • @Brian why is it needed to add the "" ? – Gaurav May 31 '19 at 11:25
  • 1
    @gaurav This allows you to use the version of these packages bundled with your application and not the packages included with WebLogic which are typically a bit older and sometimes outdated. – Brian Ochs May 31 '19 at 20:39
  • I don't think, this solution works. It could help, if SLF4J is used as a Frontend for Log4j2. But this advise is useless, if Log4J-2 is used natively. – 30thh Dec 27 '21 at 14:15
0

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.

quaff
  • 51
  • 7