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?
Asked
Active
Viewed 3,432 times
1 Answers
5
Here's how I solved this question:
Procedure
- Download the latest Log4j2 libraries from the Apache project site.
- 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
- 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)
- Be sure to set a reference to the copied JAR files in the Netbeans project:
- 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.
- Notice that I intentionally place the log file in the
<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='matthew.pfluger@plexus.com' from='noreply-AgileUserManager@plexus.com' 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>
- In the web.xml file, add the following snippet:
<context-param> <param-name>log4jConfiguration</param-name> <param-value>log4j2.xml</param-value> </context-param>
- 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.
- Notice that I'm intentionally logging and throwing an Exception. I'm doing this because I'm testing my
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; } }
- 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
- how to configure log4j2 webapplication (particularly for which JAR files to include in the WEB-INF/lib)
- Using Log4j 2 in Web Applications (Apache documentation)
- Log4j2 SMTPAppender (in case you wish to email errors from the log)
- Tomcat Context Container Documentation (helped me understand why Tomcat was keeping a lock on the log files)