Pflugs
Pflugs

Reputation: 814

How to use Log4j2 in a JAX-WS Java Web Service

As documented in a previous StackOverflow question, I am learning how to use JAX-WS (Java API for XML Web Services). I've used Log4j2 in previous executable Java programs, and I'd like to use it for logging web service requests. How can I add Log4j2 to the base code in this JAX-WS tutorial?

Upvotes: 5

Views: 3823

Answers (1)

Pflugs
Pflugs

Reputation: 814

Here's how I solved this question:

Procedure

  1. Download the latest Log4j2 libraries from the Apache project site.
  2. Unzip the Log4j2 compressed folder, and copy the following files to the project's lib folder.
    • log4j-api-2.3.jar
    • log4j-core-2.3.jar
  3. If you wish the log to be emailed, you will also need to copy the following files to the project's lib folder:
    • log4j-web-2.3.jar
    • javax.mail.jar (which may be downloaded here)
  4. Be sure to set a reference to the copied JAR files in the Netbeans project: screenshot of added references
  5. Add a file named log4j2.xml to the Source Packages folder (see screenshot above). The contents of my log4j2.xml file are below to use as a starting point.
    • Notice that I intentionally place the log file in the {$Tomcat}\logs folder. I originally had placed the application's log file in the {$Tomcat}\webapps\HelloWorld folder. However, I learned that Tomcat keeps file locks on the log files that will prevent "hot deploy/undeploy", leading to an incomplete deployment and a web application that doesn't work. For more info, search for antiResourceLocking on this page.
<Configuration status='off'>
    <Properties>
        <Property name='logFilePath'>logs/HelloWorld.log</Property>
    </Properties>
    <Appenders>
        <Console name='Console' target='SYSTEM_OUT'>
            <PatternLayout pattern='%m%n'/>
        </Console>
        <File name='File' fileName='${logFilePath}'>
            <PatternLayout>
                <Pattern>%d %-5p %m%n</Pattern>
            </PatternLayout>
        </File>
        <SMTP name='Email' subject='JAX-WS Hello World Tutorial Web Service' to='[email protected]' from='[email protected]'
              smtpHost='intranet-smtp.plexus.com' smtpPort='25' bufferSize='10000'>
            <PatternLayout>
                <Header>One or more errors occurred!  Please see the full log file at: ${logFilePath}.%n</Header>
                <Pattern>%d %-5p %m%n</Pattern>
            </PatternLayout>
        </SMTP>
    </Appenders>
    <Loggers>
        <Root level='debug'>
            <AppenderRef ref='File'/>
            <AppenderRef ref='Console'/>
            <AppenderRef ref='Email'/>
        </Root>
    </Loggers>
</Configuration>
  1. In the web.xml file, add the following snippet:
<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>log4j2.xml</param-value>
</context-param>
  1. Now all the setup is complete, so it's time to add some logging! You're free to add your own logging steps, but here is how I modified the HelloWorldImpl.java file.
    • Notice that I'm intentionally logging and throwing an Exception. I'm doing this because I'm testing my log4j2.xml configuration to make sure an email is sent when an Exception is logged.
package com.mkyong.ws;

import javax.jws.WebService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

    private static final Logger logger = LogManager.getLogger(HelloWorldImpl.class);

    @Override
    public String getHelloWorldAsString() throws Exception {
        logger.entry();
        logger.info("Request received");
        logger.exit();

        final Exception exception = new Exception("What the heck happened?");
        logger.error(exception);
        throw exception;
    }
}
  1. Finally, compile the application to a WAR file and deploy to your Tomcat server. Then, after submitting a request to the service, the application will now perform logging.

I hope this is helpful! - Pflugs

References

Upvotes: 5

Related Questions